SpringBoot配置
- 加入maven支持
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
- 添加配置
SpringFoxConfig.java
@Configuration
@EnableSwagger2
public class SpringFoxConfig {
@Bean
public Docket apiDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("项目包名"))
.paths(PathSelectors.any())
.build();
}
}
WebConfig.java
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
@Override
protected 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);
}
}
以上,配置完成。
注解说明
@Api:用在请求的类上,说明该类的作用
例子:
@Api(tags="APP用户注册Controller")
@ApiOperation:用在请求的方法上,说明方法的作用
例子:
@ApiOperation(value="用户注册",notes="方法的备注说明")
@ApiImplicitParams:用在请求的方法上,包含一组参数说明
例子:
@ApiImplicitParams({
@ApiImplicitParam(name="mobile",value="手机号",required=true,paramType="body"),
@ApiImplicitParam(name="password",value="密码",required=true,paramType="body"),
@ApiImplicitParam(name="age",value="年龄",required=true,paramType="body",dataType="Integer")
})
@ApiImplicitParam属性说明:
属性名 | 说明 |
---|---|
name | 参数名 |
value | 参数的说明 |
required | 参数是否是必须 |
paramType | 参数的获取途径。有以下几种值: ・header:通过@RequestHeader获取 ・query:通过@RequestParam获取 ・path:通过@PathVariable获取 ・body:通过@RequestBody获取 |
dataType | 参数类型,默认是String |
defaultValue | 参数的默认值 |
@ApiResponses:用于请求的方法上,表示一组响应
例子:
@ApiOperation(value = "select1请求",notes = "多个参数,多种的查询参数类型")
@ApiResponses({
@ApiResponse(code=400,message="请求参数没填好"),
@ApiResponse(code=404,message="请求路径没有或页面跳转路径不对")
})
@ApiModel:用于响应类上,表示一个返回响应数据的信息
例子:
@ApiModel(description= "返回响应数据")
public class RestMessage{
@ApiModelProperty(value = "是否成功")
private boolean success=true;
@ApiModelProperty(value = "返回对象")
private Object data;
@ApiModelProperty(value = "错误编号")
private Integer errCode;
@ApiModelProperty(value = "错误信息")
private String message;
}