SpringBoot整合Swagger2的步驟詳解
swagger是一個流行的API開發框架,這個框架以“開放API聲明”(OpenAPI Specification,OAS)為基礎, 對整個API的開發周期都提供了相應的解決方案,是一個非常龐大的項目(包括設計、編碼和測試,幾乎支持所有語言)。
springfox大致原理:springfox的大致原理就是,在項目啟動的過種中,spring上下文在初始化的過程, 框架自動跟據配置加載一些swagger相關的bean到當前的上下文中,并自動掃描系統中可能需要生成api文檔那些類, 并生成相應的信息緩存起來。如果項目MVC控制層用的是springMvc那么會自動掃描所有Controller類,并生成對應的文檔描述數據.該數據是json格式,通過路徑:項目地址/ v2/api-docs可以訪問到該數據,然后swaggerUI根據這份數據生成相應的文檔描述界面。 因為我們能拿到這份數據,所以我們也可以生成自己的頁面.
SpringBoot整合Swagger2引入依賴<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version></dependency><dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version></dependency>
注意:jdk1.8以上才能運行swagger2
編寫配置類配置Swagger@Configuration@EnableSwagger2public class SwaggerConfig{ @Bean public Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage('org.example.yourproject'))//這里填寫項目package.paths(PathSelectors.any()).build(); }//springfox為我們提供了一個Docket(摘要的意思)類,我們需要把它做成一個Bean注入到spring中, 顯然,我們需要一個配置文件,并通過一種方式(顯然它會是一個注解)告訴程序,這是一個Swagger配置文件。 private ApiInfo apiInfo() {return new ApiInfoBuilder().title('Spring Boot中使用Swagger2構建RESTful API').description('rest api 文檔構建利器').termsOfServiceUrl('https://www.cnblogs.com/yrxing/').contact('xing').version('1.0').build(); } }//springfox允許我們將信息組合成一個ApiInfo的類,作為構造參數傳給Docket
訪問:http://localhost:{your_server_port}/swagger-ui.html
Swagger2常用注解使用@RestController@RequestMapping(value = '/user', produces = APPLICATION_JSON_VALUE) //配置返回值 application/json@Api(tags = '用戶管理')public class HelloController { ArrayList<User> users = new ArrayList<>(); @ApiOperation(value = '獲取用戶列表', notes = '獲取所有用戶信息') @RequestMapping(value = {''}, method = RequestMethod.GET) public List<User> hello() {users.add(new User('邏輯', 'luoji'));users.add(new User('葉文杰', 'yewenjie'));return users; }}@ApiModel()、@ApiModelProperty()
@ApiModel(description = '用戶',value = '用戶')public class User { private String id; @ApiModelProperty(value = '用戶名')//value屬性指明了該字段的含義(描述 Description) private String username; @ApiModelProperty(hidden = true)//此注解可以作用在字段或者方法上,只要 hidden 屬性為 true ,該字段或者方法就不會被生成api文檔. private String password; private String email; private Integer age; private Boolean enabled; }@ApiParam()
@ApiOperation(value = '獲取用戶詳細信息', notes = '根據url的id來獲取用戶詳細信息') @RequestMapping(value = 'getUser/{id}', method = RequestMethod.GET) public User getUser(@ApiParam(naeme = 'id',value = '用戶id', required = true) @PathVariable(value = 'id') String id) { return new User(id, 'itguang', '123456'); }//@ApiParam這個注解,需要注意的是,這個注解方法的參數前面,不能直接用在方法上面. @ApiImplicitParams()、@ApiImplicitparam()
··· @Api('測試用例1') @Controller public class swaggerTestUse(){ @ApiOperation(value = 'apiOperationSwaggerTest', notes = 'apiOperationSwagger測試') @ApiImplicitParams({@ApiImplicitParam(name = 'id', value = 'id入參', required = true, dataType = 'Integer', paramType = 'query'),@ApiImplicitParam(name = 'brand', value = 'brand', required = true, dataType = 'BRAND', paramType = 'body') }) public void apiOperationSwaggerTest(Integer id, Brand band){ } }
以上就是SpringBoot整合Swagger2的步驟詳解的詳細內容,更多關于SpringBoot整合Swagger2的資料請關注好吧啦網其它相關文章!
相關文章: