Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。Swagger 让部署管理和使用功能强大的API从未如此简单。
jeestie配置步骤
1.maven依赖
pom.xml 添加swagger的依赖
io.springfox springfox-swagger2 2.4.0 io.springfox springfox-swagger-ui 2.4.0
2.Swagger配置文件
定义一个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(); }}.
3、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) { Mapj = new HashMap (); 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 repairStationList = new ArrayList (); RepairStation repairStation =new RepairStation(); repairStation.setStName(station.trim().toUpperCase()); if (pageNo != null && pageSize != null && !"".equals(pageNo) && !"".equals(pageSize)) { // 分页查询 Page paramPage = new Page (); paramPage.setPageNo(Integer.parseInt(pageNo)); paramPage.setPageSize(Integer.parseInt(pageSize)); Page 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 ()); } 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中红色部分)
/** * 维修站表Entity * @author lxm * @version 2017-03-14 */@ApiModelpublic 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