好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

解决springboot引入swagger2不生效问题

今天遇到跟同事遇到一个由于失误导致的问题,也可以说比较难发现了.在此记录一下(我们用的springboot是2.0.3,swagger是2.2.2)

问题描述:

swagger修改title,description等都不生效。并且启动springboot,没有有去加载swagger的配置类。(在debug模式启动)

经过不断的查找,发现了原因是:swagger的配置类的注解加错了。@Configuration不小心写成了@Configurable.

还有就是@EnableSwagger2注解只需要加在swagger配置类上

springboot引入swagger2的步骤:

①引入依赖

?

1

2

3

4

5

6

7

8

9

10

11

<!--  引入swagger包 -->

         < dependency >

             < groupId >io.springfox</ groupId >

             < artifactId >springfox-swagger2</ artifactId >

             < version >2.2.2</ version >

         </ dependency >

         < dependency >

             < groupId >io.springfox</ groupId >

             < artifactId >springfox-swagger-ui</ artifactId >

             < version >2.2.2</ version >

         </ dependency >

②编写Swagger2的配置类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

@Configuration

@EnableSwagger2

public class Swagger2Config {

     @Bean

     public Docket api(){

         return new Docket(DocumentationType.SWAGGER_2)

                 .apiInfo(getApiInfo())

                 .select()

                .apis(RequestHandlerSelectors.basePackage( "com.xx.controller" ))

                 .paths(PathSelectors.any())

                 .build();

     }

     private ApiInfo getApiInfo(){

         return new ApiInfoBuilder()

                 .title( "Swagger2...." )

                 .description( "Swagger2" )

                 .version( "1.0" )

                 .license( "Apache 2.0" )

                 .licenseUrl( "http://HdhCmsTestapache.org/licenses/LICENSE-2.0" )

                 .build();

     }

}

③在controller中添加注解:按需添加注解

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

@Controller

@RequestMapping ( "/user" )

@Api (tags = "我的接口模块" )

public class UserController {

     @Autowired

     private UserService userService;

     //注意这个注解跟请求对应的@XxxMapping,要不然这个接口会生成好多方法

     @GetMapping (value = "/getUserById" )

     @ResponseBody

     @ApiOperation (value = "根据ID查询User" )

     public User getUserById( @RequestParam (value = "id" ) int id){

         return userService.getUserById(id);

     }

}

④在model(pojo)上加注解,按需添加

?

1

2

3

4

5

6

7

8

9

10

@ApiModel (value = "用户对象" )

public class User {

     @ApiModelProperty (value = "用户ID" , name = "userId" )

     private Integer userId;

     @ApiModelProperty (value = "用户姓名" ,name = "userName" )

     private String userName;

     @ApiModelProperty (value = "用户密码" ,name = "password" )

     private String password;

     @ApiModelProperty (value = "用户手机号" ,name = "phone" )

     private String phone;

一些注解的使用

@Api:一般用于Controller中,用于接口分组

@ApiOperation:接口说明,用于api方法上。

@ApiImplicitParams:用在方法上包含一组参数说明

@ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面

paramType:参数放在哪个地方

header 请求参数的获取:@RequestHeader

query 请求参数的获取:@RequestParam

path(用于restful接口) 请求参数的获取:@PathVariable

body(不常用)

form(不常用)

name:参数名

dataType:参数类型

required:参数是否必须传

value:参数的意思

defaultValue:参数的默认值

@ApiResponses:用于表示一组响应

@ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息

code:数字,例如400

message:信息&#xff0c;例如]请求参数没填好]

response:抛出异常的类

@ApiModel:描述一个Model的信息(这种一般用在post创建的时候,使用@RequestBody这样的场景,请求参数无法使用@ApiImplicitParam注解进行描述的时候)表明这是一个被swagger框架管理的model,用于class上

@ApiModelProperty :使用在实体类上的成员变量上,描述成员变量的含义。

以上就是解决springboot引入swagger2不生效问题的详细内容,更多关于springboot引入swagger2的资料请关注其它相关文章!

原文链接:https://blog.csdn.net/weixin_42884584/article/details/81538605

查看更多关于解决springboot引入swagger2不生效问题的详细内容...

  阅读:124次