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 框架