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;
}