springboot2.x 配置swagger2的坑

其实配置起来很简单,直接上代码
pom

#版本过低可能导致错误
 		<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

导入pom文件后上配置类

package com.example.demo2;

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;
 
/**
 * @author Administrator
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                为当前包路径
                .apis(RequestHandlerSelectors.basePackage("com.example.demo2.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot中使用Swagger2构建RESTful APIs")
                .version("1.0")
                .description("描述信息:测试swagger2")
                //可以在这里直接.出来 按照下面的参数进行你想要的配置
                .build();
    }

}

附上swagger1 的配置

#配置swagger
#标题
title
#描述
description
#版本
version
#许可证
license
#许可证路径
licenseUrl
#服务条款URL
termsOfServiceUrl
#维护人名称
contact
#维护人url
contact.url
#维护人邮箱
contact.email
#swagger扫描的基础包,默认:全扫描
swagger.base-package=com.example.demo2
#需要处理的基础URL规则,默认:/**
swagger.base-path=/**

swagger2中部分没有,这里只是方便理解

好的,到这里就配置完成了
接下来启动项目,访问
http://localhost:8080/swagger-ui.html

是不是发现404?
springMVC把你的地址当成了请求,去controller里面找,没找到肯定404了

我们只需要配置一下springboot的静态资源管理器
上代码

package com.example.demo2.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * @author Administrator
 */
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations(
                "classpath:/static/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }
}

再次重启项目,访问,就可以啦.
常用的注解
1、@Api():用在请求的类上,表示对类的说明,也代表了这个类是swagger2的资源

参数:

tags:说明该类的作用,参数是个数组,可以填多个。
value="该参数没什么意义,在UI界面上不显示,所以不用配置"
description = "用户基本信息操作"

2、@ApiOperation():用于方法,表示一个http请求访问该方法的操作

参数:

value="方法的用途和作用"    
notes="方法的注意事项和备注"    
tags:说明该方法的作用,参数是个数组,可以填多个。
格式:tags={"作用1","作用2"} 
(在这里建议不使用这个参数,会使界面看上去有点乱,前两个常用)

3、@ApiModel():用于响应实体类上,用于说明实体作用

参数:

description="描述实体的作用"  

4、@ApiModelProperty:用在属性上,描述实体类的属性

参数:

value="用户名"  描述参数的意义
name="name"    参数的变量名
required=true     参数是否必选

5、@ApiImplicitParams:用在请求的方法上,包含多@ApiImplicitParam

6、@ApiImplicitParam:用于方法,表示单独的请求参数

参数:

name="参数ming" 
value="参数说明" 
dataType="数据类型" 
paramType="query" 表示参数放在哪里
    · header 请求参数的获取:@RequestHeader
    · query   请求参数的获取:@RequestParam
    · path(用于restful接口) 请求参数的获取:@PathVariable
    · body(不常用)
    · form(不常用) 
defaultValue="参数的默认值"
required="true" 表示参数是否必须传

7、@ApiParam():用于方法,参数,字段说明 表示对参数的要求和说明

参数:

name="参数名称"
value="参数的简要说明"
defaultValue="参数默认值"
required="true" 表示属性是否必填,默认为false

8、@ApiResponses:用于请求的方法上,根据响应码表示不同响应

一个@ApiResponses包含多个@ApiResponse

9、@ApiResponse:用在请求的方法上,表示不同的响应

参数:

code="404"    表示响应码(int型),可自定义
message="状态码对应的响应信息"   

10、@ApiIgnore():用于类或者方法上,不被显示在页面上

11、@Profile({“dev”, “test”}):用于配置类上,表示只对开发和测试环境有用

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值