Spring Boot Swagger2使用方法過程解析
阿新 • • 發佈:2020-08-20
1.新增Swagger2依賴
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.2.2</version> </dependency>
2配置類
@Configuration @EnableSwagger2 public class Swagger2 { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.bs.swaggertest.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("微信專案介面對接文件") .description("測試環境介面除錯") .contact("陶海軍") .version("1.0") .build(); } }
3.新增文件內容
@RestController @RequestMapping(value="/users") public class UserController { @ApiOperation(value="刪除使用者",notes="根據url的id來指定刪除物件") @ApiImplicitParam(name = "id",value = "使用者ID",required = true,dataType = "String") @RequestMapping(value="/{id}",method=RequestMethod.DELETE) public String deleteUser(@PathVariable String id) { return "success"; } }
4.測試
http://localhost:8080/swagger-ui.html
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。