天天看点

SpringBoot -- Swagger2Swagger2

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
SpringBoot -- Swagger2Swagger2

代码

代码请移步 Github参考地址

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

SpringBoot -- Swagger2Swagger2

继续阅读