SpringBoot -- Swagger2

来源:互联网 发布:孙俪等花开杂货铺淘宝 编辑:程序博客网 时间:2024/04/29 22:41

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@EnableSwagger2public 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

@Controllerpublic 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

如果只需指定的method,配置httpMethod


代码

代码请移步 Github参考地址

如有疑问请加公众号(K171),如果觉得对您有帮助请 github start
公众号_k171

0 0