springboot + swagger2 生成api文档

直接贴代码:

1
2
3
4
5
6
7
8
9
10
<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.8.0</version>
        </dependency>

  config

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import io.swagger.annotations.ApiOperation;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
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;
 
 
//ConditionalOnProperty 注解,是为了根据配置文件的配置来指定swagger,是否生效。如果需要生效,就在application文件中配置 swagger.enable=true
@ConditionalOnProperty(prefix = "swagger",value = {"enable"},havingValue = "true")
@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurerAdapter {
 
    @Override
    public 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)
                .forCodeGeneration(true)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .select()
//              .apis(RequestHandlerSelectors.basePackage(""))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }
 
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(" API文档")
                .description(" API文档")
                .version("1.0")
                .build();
    }
}

  返回model

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
 
/**
 * 封装返回数据javabean
 *
 **/
@ApiModel(value = "Result",description = "通用返回对象")
public class Result {
    public final static String MSG_SUCCESS = "success";
 
    @ApiModelProperty(value = "调用是否正常",name = "isok")
    private boolean isok;
    @ApiModelProperty(value = "接口返回msg,如果isok是false,那么msg是错误信息",name = "msg")
    private String msg;
    @ApiModelProperty(value = "调用成功,返回的数据",name = "data")
    private Object data;
 
    public Result() {
    }
 
    public Result(boolean isok, String msg) {
        this.isok = isok;
        this.msg = msg;
    }
 
    public Result(boolean isok, String msg, Object data) {
        this.isok = isok;
        this.msg = msg;
        this.data = data;
    }
 
    public boolean isIsok() {
        return isok;
    }
 
    public void setIsok(boolean isok) {
        this.isok = isok;
    }
 
    public String getMsg() {
        return msg;
    }
 
    public void setMsg(String msg) {
        this.msg = msg;
    }
 
    public Object getData() {
        return data;
    }
 
    public void setData(Object data) {
        this.data = data;
    }
}

  controller  上注解:

@Api(value = "/XXX",tags = "查询接口")

method 上注解 示例:

1
2
3
4
5
@ApiOperation(value = "XXX",notes = "XXXX")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "methodName",value = "XXX",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "params",value = "XXXXX",required = true,paramType = "body")
    })
public Result selectServiceRun(String methodName,
@RequestBody Map<String, Object> params,
HttpServletRequest request) {

 

说明:

  • @Api:用在类上,说明该类的作用
  • @ApiOperation:用在方法上,说明方法的作用
  • @ApiImplicitParams:用在方法上包含一组参数说明
  • @ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
    • paramType:参数放在哪个地方
      • header-->请求参数的获取:@RequestHeader
      • query-->请求参数的获取:@RequestParam
      • path(用于restful接口)-->请求参数的获取:@PathVariable
      • body(不常用)
      • form(不常用)
    • name:参数名
    • dataType:参数类型
    • required:参数是否必须传
    • value:参数的意思
    • defaultValue:参数的默认值
  • @ApiResponses:用于表示一组响应
  • @ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
    • code:数字,例如400
    • message:信息,例如"请求参数没填好"
    • response:抛出异常的类
  • @ApiModel:描述一个Model的信息(这种一般用在post创建的时候,使用@RequestBody这样的场景,请求参数无法使用@ApiImplicitParam注解进行描述的时候)
    • @ApiModelProperty:描述一个model的属性

原文地址:https://www.cnblogs.com/yanqin/p/9145895.html

posted @ 2019-08-31 12:55  星朝  阅读(356)  评论(0编辑  收藏  举报