Springboot整合swagger

1:pom引入swagger的包

     <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
        </dependency>

2:添加swagger配置

package com.text.swagger.demo.config;


import org.springframework.beans.factory.annotation.Value;
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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    /**
     * 读取配置文件中的开关
     */
    @Value("${swagger.enable:false}")
    private boolean enable;


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("swagger测试")
                .description("")
                .contact(new Contact("swagger测试", "www.baidu.com", "1111111@qq.com"))
                .version("1.0.1")
                .build();
    }
    
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(enable)
                .apiInfo(apiInfo())
                .select()
                //swagger扫描的包路径,注意:如果写的swagger API注解不在如下包内,则swagger页面不会显示  后面会添加controller进行演示
                .apis(RequestHandlerSelectors.basePackage("com.text.swagger.demo.controller"))
                .paths(PathSelectors.any())
                .build();
    }


}

3:在配置文件中,把swagger开关打开,

swagger.enable=true

如果不打开,swagger访问页面将会报错,如图

 

 

 

 

 

 

4:在controller中添加注解:包路径:com.text.swagger.demo.controller;

package com.text.swagger.demo.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@Api(tags = "swagger测试模块")//标题
@RequestMapping("/swagger")
public class SwaggerTextController {



    @ApiOperation("getSwaggerStr测试") //方法标题
    @ApiImplicitParams({
            @ApiImplicitParam(value = "值", name = "key", dataTypeClass = String.class, required = true) //方法参数注释
    })
    @GetMapping("/get")
    @ResponseBody
    public Object getSwaggerStr(@RequestParam String key){
        return "传入的值是:" + key;
    }
}

 

5:再添加一个包,不在swagger指定的com.text.swagger.demo.controller这个包下,包路径:com.text.swagger.demo.api;

 

package com.text.swagger.demo.api;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("noswagger")
@Api(tags = "swagger不会显示的接口信息")
public class NoSwaggerController {



    @ApiOperation("不显示swagger接口信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "值", name = "key", dataTypeClass = String.class, required = true)
    })
    @GetMapping("/get")
    @ResponseBody
    public Object getSwaggerStr(@RequestParam String key){
        return "传入的值是:" + key;
    }

}

 

6:打开swagger地址:http://localhost:8016/swagger-ui.html#/

 

 

 7:如果整合过程中有如下报错

Failed to start bean 'documentationPluginsBootstrapper'; nested.....

解决思路是,改变springboot和swagger的版本,他们存在一些依赖关系

对应关系大概如下:

springboot:2.6.x  对应swagger 2..9.2版本

springbot:2.6.5 对应swagger:3.0.0

 

8:最后点击测试:成功响应

 

posted @ 2022-05-27 12:18  鸭猪是的念来过倒  阅读(102)  评论(0编辑  收藏  举报