Pom文件添加Swagger2坐标

    1. <!-- swagger2核心包 -->
    2. <dependency>
    3. <groupId>io.springfox</groupId>
    4. <artifactId>springfox-swagger2</artifactId>
    5. <version>2.9.2</version>
    6. </dependency>
    7. <!-- swagger-ui 可视化界面 -->
    8. <dependency>
    9. <groupId>io.springfox</groupId>
    10. <artifactId>springfox-swagger-ui</artifactId>
    11. <version>2.9.2</version>
    12. </dependency>
    13. <!--或者使用swagger-bootstrap-ui界面 /doc.html -->
    14. <dependency>
    15. <groupId>com.github.xiaoymin</groupId>
    16. <artifactId>swagger-bootstrap-ui</artifactId>
    17. <version>1.9.6</version>
    18. </dependency>

    添加配置类

    1. @Configuration //定义配置类注解
    2. @EnableSwagger2 //开启Swagger的使用
    3. public class SwaggerConfig {
    4. @Bean
    5. public Docket docket(){
    6. return new Docket(DocumentationType.SWAGGER_2)
    7. .apiInfo(apiInfo()) //关于文档的各种信息
    8. .enable(true) //使Swagger生效
    9. .groupName("霍聚豪GroupNmae")
    10. .select()//选择扫描的接口
    11. .apis(RequestHandlerSelectors.basePackage("com.huojuhao.controller"))//指定扫描的包
    12. .build();
    13. }
    14. //为当前包下controller生成API文档
    15. // .apis(RequestHandlerSelectors.basePackage("com.troila"))
    16. //为有@Api注解的Controller生成API文档
    17. // .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
    18. //为有@ApiOperation注解的方法生成API文档
    19. // .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
    20. //为任何接口生成API文档
    21. // .apis(RequestHandlerSelectors.any())
    22. public ApiInfo apiInfo(){
    23. Contact contact = new Contact("霍聚豪", "www.baidu.com", "1293754739@qq.com");
    24. //return new ApiInfo("霍聚豪的文档","霍聚豪的开发文档","1","2","3","4","5");
    25. return new ApiInfoBuilder()
    26. .title("Spring Boot中使用Swagger2构建RESTful APIs")
    27. .description("更多请关注http://www.baidu.com")
    28. .termsOfServiceUrl("http://www.baidu.com")
    29. // .contact("sunf")
    30. .version("1.0")
    31. .build();
    32. }
    33. }

    Swagger使用的注解及其说明:
    @Api:用在类上,说明该类的作用。
    @ApiOperation:注解来给API增加方法说明。
    @ApiImplicitParams : 用在方法上包含一组参数说明。
    @ApiImplicitParam:用来注解来给方法入参增加说明。

    |

    paramType:指定参数放在哪个地方 | header:请求参数放置于Request Header,使用@RequestHeader获取
    query:请求参数放置于请求地址,使用@RequestParam获取
    path:(用于restful接口)–>请求参数的获取:@PathVariable
    body:(不常用)
    form(不常用)
    [

    ](https://blog.csdn.net/github_38823514/article/details/88970607) | | —- | —- | | name:参数名 | | | dataType:参数类型 | | | required:参数是否必须传 | true 或 false | | value:说明参数的意思 | | | defaultValue:参数的默认值 | |

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

    1. code:数字,例如400
    2. message:信息,例如"请求参数没填好"
    3. response:抛出异常的类

    @ApiModel:描述一个Model的信息(一般用在请求参数无法使用@ApiImplicitParam注解进行描述的时候)
    @ApiModel( ) //主要用来标注返回的实体类
    @ApiModelProperty( ) //主要用来标注实体类中的属性

    详细注解说明 https://blog.csdn.net/xiaojin21cen/article/details/78654652