• spring boot 集成 swagger2 四部曲


    1.引入依赖的jar包(这里面有坑,exclusion默认,引入1.5.21,不然ApiImplicitParam dataType="long" 报错,亲历 )

    <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
    <exclusions>
    <exclusion>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-models</artifactId>
    </exclusion>
    </exclusions>
    </dependency>
    <dependency>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-models</artifactId>
    <version>1.5.21</version>
    </dependency>
    <!--swagger-ui-->
    <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
    </dependency>

    2.application.properties 新增 spring.swagger2.enabled=true
    3.增加 SwaggerConfig 配置文件,内容如下:
        
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig extends WebMvcConfigurationSupport {

    @Value(value = "${spring.swagger2.enabled}")
    private Boolean swaggerEnabled;

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("swagger-ui.html")
    .addResourceLocations("classpath:/META-INF/resources/");

    registry.addResourceHandler("/webjars/**")
    .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Bean
    public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2)
    .apiInfo(apiInfo())
    .enable(swaggerEnabled)
    .select()
    .apis(RequestHandlerSelectors.basePackage("com.xinnet.knowledgecms.web"))
    .paths(PathSelectors.any())
    .build();
    }

    private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
    .title("接口文档")
    .description("新知接口文档")
    .termsOfServiceUrl("http://xxxx.com:8065/")
    .version("1.0")
    .build();
    }
    }

    4.配置注解
    @Api(description = "测试接口")
    @Controller
    public class IndexController {

    @ApiOperation("添加用户")
    @AllowAccess
    @GetMapping("/addUser")
    @ApiImplicitParams({
    @ApiImplicitParam(name="id",value="用户id",dataType="long", paramType = "query",defaultValue ="1000"),
    @ApiImplicitParam(name="name",value="用户名",dataType="string", paramType = "query",defaultValue="zhangsan")
    })
    public String test(HttpServletRequest request,long id ,String name) {
    return "index1.html";
    }

    5。访问 swagger

    http://xxxx/swagger-ui.html

  • 相关阅读:
    数据结构-二叉树--二叉搜索树
    Django之Model操作
    Django之Form组件
    Django之url(路由)配置
    Django框架之模板继承和静态文件配置
    Django框架之第三篇模板语法(重要!!!)
    Django ORM操作及进阶
    django 之 ORM操作多表联查总结
    人生苦短,我学python之python xml数据解析
    人生苦短,我学python之python re正则表达式
  • 原文地址:https://www.cnblogs.com/virgree/p/12924967.html
Copyright © 2020-2023  润新知