Swagger是属于比较推荐的一种。
<!-- 文档自动生成 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency>
@Configuration @EnableSwagger2 public class Swagger2 { @Bean public Docket createRestApi(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo(){ return new ApiInfoBuilder() .title("专题页api文档") .description("专题页api文档") .termsOfServiceUrl("http://terms-of-services.url") .version("1.0") .build(); } }
apis(RequestHandlerSelectors.basePackage("com.xjj.web.controller"))
@RestController @RequestMapping("/topic/") public class TopicController { protected final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired TopicService topicService; @RequestMapping(value="getTopic", method = RequestMethod.GET) @ApiOperation(value="接口描述。。。", response = TopicResult.class) @ApiImplicitParams({ @ApiImplicitParam(name = "sn", value = "编号", required = true, dataType = "String", paramType = "query"), @ApiImplicitParam(name = "token", value = "用户token", required = true, dataType = "String", paramType = "query") }) public JsonResult getTopicBySn(HttpServletRequest request, @RequestParam String sn, @RequestParam String token){ JsonResult result = null; try { Topic topic = topicService.getTopic(sn); if(topic == null){ result = new JsonResult(ReturnCode.PARAMS_ERROR, "错误"); }else { result = new JsonResult(ReturnCode.SUCCESS, "成功", topic); } }catch (Exception e){ logger.error("getTopicBySn Exception", e); result = new JsonResult(ReturnCode.EXCEPTION); } return result; } }
@ApiModel public class JsonResult { @ApiModelProperty(value = "状态码", example="40001", required = true, position=-2) private String code; @ApiModelProperty(value = "返回消息", example="恭喜,成功!", required = true, position=-1) private String message; @ApiModelProperty(value = "具体数据") private Object data; //constructors, getters, setters 略... }
@ApiModel public class TopicResult extends JsonResult { @ApiModelProperty(value = "专题详情", required = true) private Topic data; //constructors, getters, setters 略... }
@RestController @RequestMapping("/apply/") public class ApplyController { protected final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired ApplyService applyService; @RequestMapping(value="store-mgr-setup", method = RequestMethod.POST) @ApiOperation(value="接口描述") public JsonResult applyStoreMgrSetup(HttpServletRequest request, @ApiParam(value = "参数描述", required = true) @RequestBody DianApply dianApply){ JsonResult result = null; //其他代码略... return result; } }
测试:填入相应的参数后,点击下方“Try it out!”按钮,即可完成了一次请求调用!