Swagger 是一個規範和完整的框架,用於生成、描述、調用和可視化 RESTful 風格的 Web 服務。整體目標是使客戶端和文件系統做爲服務器以一樣的速度來更新。文件的方法,參數和模型緊密集成到服務器端的代碼,容許API來始終保持同步。Swagger 讓部署管理和使用功能強大的API從未如此簡單。html
jeestie配置步驟java
1.maven依賴web
pom.xml 添加swagger的依賴spring
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.4.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.4.0</version> </dependency>
2.Swagger配置文件api
定義一個Swagger2.java類,瀏覽器
@Configuration @EnableWebMvc @EnableSwagger2 @ComponentScan("com.weichai.modules.rest.web") //配置須要掃描的rest接口路徑 public class Swagger2 extends WebMvcConfigurationSupport { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.weichai.modules.rest.web"))//配置須要掃描的rest接口路徑 .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("維修app管理系統中使用Swagger2構建RESTful APIs") .termsOfServiceUrl("http://blog.didispace.com/") .contact("tepusoft") .version("1.0") .build(); } }.
三、Controller中使用註解添加API文檔服務器
@ApiOperation(value = "查詢維修站接口", notes = "查詢維修站信息,能夠根據維修站協議號或服務站名模糊查詢")
@ApiImplicitParams(value = {
@ApiImplicitParam(name = "station", value = "維修站協議號或服務站名", paramType = "query"),
@ApiImplicitParam(name = "pageNo", value = "當前頁碼", paramType = "query"),
@ApiImplicitParam(name = "pageSize", value = "頁面大小", paramType = "query")})
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK", reference = "{code:'',message:'',data:'{}'}"),
@ApiResponse(code = 201, message = "RepairStation", response = RepairStation.class),
@ApiResponse(code = 500, message = "Server error") })
@RequestMapping(value = "getRepairStation", method = RequestMethod.POST)
public ApiReponseData getRepairStation(HttpServletResponse response, @RequestBody Map map) {
Map<String, Object> j = new HashMap<String, Object>();
try {
if (map == null) {
return new ApiReponseData.Builder(FAIl).message("參數不能爲空").build();
}
String station = map.get("station") == null ? "" : map.get("station").toString();
String pageNo = map.get("pageNo") == null ? "" : map.get("pageNo").toString();
String pageSize = map.get("pageSize") == null ? "" : map.get("pageSize").toString();
List<RepairStation> repairStationList = new ArrayList<RepairStation>();
RepairStation repairStation =new RepairStation();
repairStation.setStName(station.trim().toUpperCase());
if (pageNo != null && pageSize != null && !"".equals(pageNo) && !"".equals(pageSize)) {
// 分頁查詢
Page paramPage = new Page<RepairStation>();
paramPage.setPageNo(Integer.parseInt(pageNo));
paramPage.setPageSize(Integer.parseInt(pageSize));
Page<RepairStation> page = repairManageService.getRepairStationPage(paramPage,repairStation);
if (page.getList() != null && (paramPage.getPageNo() - 1) * paramPage.getPageSize()
+ page.getList().size() <= page.getCount()) {
j.put("repairStationList", page.getList());
} else {
j.put("repairStationList", new ArrayList<RepairStation>());
}
if (page.isLastPage()) {
j.put("lastPage", true);
} else {
j.put("lastPage", false);
}
}else{
repairStationList = repairManageService.getRepairStation(repairStation);
j.put("repairStationList",repairStationList);
}
return new ApiReponseData.Builder(SUCCESS).message(SUCESS_TIP).data(j).build();
} catch (Exception e) {
logger.error("查詢維修站接口", e);
return new ApiReponseData.Builder(FAIl).message("系統異常").build();
}
}
4.返回的實體類中也能夠加說明(3中紅色部分)app
/** * 維修站表Entity * @author lxm * @version 2017-03-14 */ @ApiModel public class RepairStation implements Serializable { private static final long serialVersionUID = 1L; @ApiModelProperty(name="stNumber",value="服務站協議號") @JsonProperty(value="stNumber") private String stNumber; // 服務站協議號 @ApiModelProperty(name="stName",value="維修站名稱") @JsonProperty(value="stName") private String stName; // 維修站名稱
5.瀏覽器訪問 http://IP:port/{context-path}/swagger-ui.html 框架