springBoot配置Swagger2(踩坑记二)

本文记录了在SpringBoot项目中配置Swagger2的过程,包括在pom.xml添加依赖,创建SwaggerConfig类,启用@EnableSwagger2注解,并分享了在配置过程中遇到的问题及解决方法。通过配置,可以在http://localhost:8083/swagger-ui.html访问Swagger界面进行接口测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

配置swagger2
在开发过程中,配置swagger可以让我们开发的接口一目了然,便于测试,在配置过程中踩了几次坑,特将这个过程记录下来

  1. pom.xml中加入依赖
  <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.4.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.4.0</version>
        </dependency>

在与application文件同级的地方加入swaggerConfig类,我的项目结构如下
这里写图片描述
SwaggerConfig.class

package com.tg.parkmana;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@EnableSwagger2
@Configuration
public class SwaggerConfig {
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.tg.parkmana.controller"))
                .paths(PathSelectors.any())
                .build();

    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("后端测试接口")
                .description("停车场后台配置管理系统后端测试接口")
                .termsOfServiceUrl("none")
                .version("1.0").build();
    }




}

在工程的application类(在我的工程中就是parkmanaApplication)中加入注解@EnableSwagger2

@MapperScan("com.tg.parkmana.dao")
@SpringBootApplication
@EnableSwagger2
public class ParkmanaApplication {

    public static void main(String[] args) {
        SpringApplication.run(ParkmanaApplication.class, args);
    }
}

此时运行工程,输入网址就可以看到swagger页面了。我的appliction.yml文件中的配置的是

server:
  port: 8083

所以我访问的网址是http://localhost:8083/swagger-ui.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值