Swagger2
- SpringMvc配合Swagger2可以生成可讀性和好的API文檔
- 在團隊合作中這點尤爲重要
- Swagger2生成的爲Restful API
- Swagger2可以直接測試接口
在FeignServer的基礎上進行集成
build.gradle中引入swagger2
build.gradle
compile ('io.springfox:springfox-swagger2:'+swagger2Version)
compile ('io.springfox:springfox-swagger-ui:'+swagger2Version)
創建swagger2配置類
Swagger2Config.java
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.bootcwenao.feignserver.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot Swagger2 test Restful API")
.description("更多內容請詳見代碼")
.termsOfServiceUrl("http://blog.csdn.net/cwenao")
.contact("cwenao")
.version("0.5.0")
.build();
}
}
在controller上配置swagger2, httpMethod 如果不寫會是所有的method
@Controller
public class FeignController {
@Autowired
FeignServer feignServer;
@ApiOperation(value = "/testFeign",notes = "測試Feign",httpMethod = "GET")
@ApiParam(name = "content",value = "參數:content")
@RequestMapping("/testFeign")
@ResponseBody
public void testFeign(String content) {
String ribbonStr = feignServer.testRealRibbon(content);
System.out.println(ribbonStr);
}
}
測試
- 依次啓動 discovery、configserver、apigateway、feignserver
- 瀏覽器: http://localhost:10002/servers/swagger-ui.html
代碼
代碼請移步 Github參考地址
如有疑問請加公衆號(K171),如果覺得對您有幫助請 github start