spring Boot 分享(2)--Swagger2的集成
Swagger™的目标是为REST APIs 定义一个标准的,与语言无关的接口,使人和计算机在看不到源码或者看不到文档或者不能通过网络流量检测的情况下能发现和理解各种服务的功能。当服务通过Swagger定义,消费者就能与远程的服务互动通过少量的实现逻辑。类似于低级编程接口,Swagger去掉了调用服务时的很多猜测。
浏览 Swagger-Spec 去了解更多关于Swagger 项目的信息,包括附加的支持其他语言的库。
代码示例:
https://github.com/wangjiuong/SpringBoot/tree/master/SpringBootSwagger2
需要增加的maven配置如下:
<dependencies> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.4.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.4.0</version> </dependency> </dependencies>
启动类定义如下:
package com.nuaa; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Created by wangjiuyong on 2017/8/22. */ @SpringBootApplication public class ConfigMain { public static void main(String[] args) throws Exception { SpringApplication.run(ConfigMain.class, args); } }
Swagger2的配置类如下:
package com.nuaa; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @EnableSwagger2 @Configuration public class RestApiConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.nuaa.web")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Spring 中使用Swagger2构建RESTful APIs") .termsOfServiceUrl("http://blog.csdn.net") .version("1.1") .build(); } }
在com.nuaa.web的package下面定义controller如下:
package com.nuaa.web; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; /** * Created by wangjiuyong on 2017/8/22. */ @RestController @RequestMapping(value="/Hello") public class Controller { @ApiOperation(value = "Hello", notes = "Hello") @ApiImplicitParam(name = "Hello", value = "Hello", required = true, dataType = "Hello") @RequestMapping(value="/helloworld",method = RequestMethod.GET) @ResponseBody String home() { return "Hello World!"; } }
使用mvn install后,执行java -jar ./target/***.jar 访问页面 http://127.0.0.1:8080/swagger-ui.html#!/controller/homeUsingGET,就会显示swagger


浙公网安备 33010602011771号