隨着先後端的分離,藉口文檔變的尤爲重要,今天咱們來講一說用SWAGGER2,來風騷的生成api文檔。配置很簡單,廢話少說,直接上代碼:java
build.gradlegit
dependencies { compile('org.springframework.boot:spring-boot-starter') compile('org.springframework.boot:spring-boot-starter-web') compile('io.springfox:springfox-swagger2:2.2.2') compile('io.springfox:springfox-swagger-ui:2.2.2') testCompile('org.springframework.boot:spring-boot-starter-test') }
SwaggerConfig.javagithub
@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket testApi() { return new Docket(DocumentationType.SWAGGER_2) .groupName("test") .genericModelSubstitutes(DeferredResult.class) .useDefaultResponseMessages(false) .forCodeGeneration(true) .pathMapping("/")// base,最終調用接口後會和paths拼接在一塊兒 .select() .apis(RequestHandlerSelectors.basePackage("com.example")) .build(); } }
DemoApplication.javaweb
@SpringBootApplication @RestController public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @ApiOperation("test") @RequestMapping(value = "/test", method = RequestMethod.GET) public String test() { return "success!"; } @ApiOperation("test2") @RequestMapping(value = "/test2", method = RequestMethod.GET) public String test2(@RequestParam String param) { return "Running : " + param; } }
對,就是這麼簡單,這個就能知足正常的開發須要了,但SWAGGER的強大不單單是這些,有須要能夠查看官方文檔http://swagger.io/spring
可是我也發現有個坑,就是若是url帶加密協議,swagger就萌逼了,目前還沒找到解決方案。後端