Pom文件添加Swagger2坐标
<!-- swagger2核心包 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- swagger-ui 可视化界面 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!--或者使用swagger-bootstrap-ui界面 /doc.html -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
添加配置类
@Configuration //定义配置类注解
@EnableSwagger2 //开启Swagger的使用
public class SwaggerConfig {
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo()) //关于文档的各种信息
.enable(true) //使Swagger生效
.groupName("霍聚豪GroupNmae")
.select()//选择扫描的接口
.apis(RequestHandlerSelectors.basePackage("com.huojuhao.controller"))//指定扫描的包
.build();
}
//为当前包下controller生成API文档
// .apis(RequestHandlerSelectors.basePackage("com.troila"))
//为有@Api注解的Controller生成API文档
// .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
//为有@ApiOperation注解的方法生成API文档
// .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
//为任何接口生成API文档
// .apis(RequestHandlerSelectors.any())
public ApiInfo apiInfo(){
Contact contact = new Contact("霍聚豪", "www.baidu.com", "1293754739@qq.com");
//return new ApiInfo("霍聚豪的文档","霍聚豪的开发文档","1","2","3","4","5");
return new ApiInfoBuilder()
.title("Spring Boot中使用Swagger2构建RESTful APIs")
.description("更多请关注http://www.baidu.com")
.termsOfServiceUrl("http://www.baidu.com")
// .contact("sunf")
.version("1.0")
.build();
}
}
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中,一般用于表达一个错误的响应信息
code:数字,例如400
message:信息,例如"请求参数没填好"
response:抛出异常的类
@ApiModel:描述一个Model的信息(一般用在请求参数无法使用@ApiImplicitParam注解进行描述的时候)
@ApiModel( ) //主要用来标注返回的实体类
@ApiModelProperty( ) //主要用来标注实体类中的属性
详细注解说明 https://blog.csdn.net/xiaojin21cen/article/details/78654652