SpringBoot整合系列-整合Swagger2
原創作品,可以轉載,但是請標註出處地址:ofollow,noindex" target="_blank">https://www.cnblogs.com/V1haoge/p/9959844.html
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>
第二步:新增必要的配置
一般無配置項,必要時可以新增自定義配置項,在配置類中讀取
第三步:新增配置類(重點)
// swagger2的配置內容僅僅就是需要建立一個Docket例項 @Configuration @EnableSwagger2 //啟用swagger2 public class Swagger2Config { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .pathMapping("/") .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.example.springbootdemo")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("springboordemo") .description("Springboot整合Demo") .version("0.0.1") .build(); // 這部分資訊其實可以自定義到配置檔案中讀取 } }
通過@Configuration註解,讓Spring-boot來載入該類配置。再通過@EnableSwagger2註解來啟用Swagger2Configuration。
再通過buildDocket函式建立Docket的Bean之後,
buildApiInfo()用來建立該Api的基本資訊(這些基本資訊會展現在文件頁面中)。
select() 函式返回一個 ApiSelectorBuilder 例項用來控制哪些介面暴露給Swagger2來展現。
一般採用指定掃描的包路徑來定義
Swagger會掃描該包下所有Controller定義的API,併產生文件內容(除了被@ApiIgnore指定的請求)
第四步:在Controller和Bean上新增Swagger註解
@RestController @RequestMapping("/user") @Log4j2 @Api(description = "使用者介面") public class UserApi { @Autowired private UserService service; @ApiOperation(value = "新增使用者", notes = "根據給定的使用者資訊新增一個新使用者",response = ResponseEntity.class,httpMethod = "PATCH") @RequestMapping(value = "/addUser",method = RequestMethod.PATCH) public ResponseEntity<User> addUser(final User user) { log.info("執行新增使用者操作"); return service.addUser(user); } @ApiOperation(value = "更新使用者", notes = "根據給定的使用者資訊修改使用者",response = ResponseEntity.class,httpMethod = "POST") @RequestMapping(value = "/updateUser", method = RequestMethod.POST) public ResponseEntity<User> updateUser(final User user) { log.info("執行修改使用者操作"); return service.updateUser(user); } @ApiOperation(value = "刪除使用者", notes = "根據給定的使用者ID刪除一個使用者",response = ResponseEntity.class,httpMethod = "DELETE") @RequestMapping(value = "/deleteUser", method = RequestMethod.DELETE) public ResponseEntity<User> deleteUser(final int useId) { log.info("執行刪除使用者操作"); return service.deleteUser(useId); } @ApiOperation(value = "查詢使用者", notes = "根據給定的使用者ID獲取一個使用者",response = ResponseEntity.class,httpMethod = "GET") @RequestMapping(value = "getUser", method = RequestMethod.GET) public ResponseEntity<User> getUser(final int useId) { log.info("執行查詢單個使用者操作"); return service.getUser(useId); } @ApiOperation(value = "查詢使用者", notes = "根據給定的使用者資訊查詢使用者",response = ResponseEntity.class,httpMethod = "POST") @RequestMapping(value = "getUsers", method = RequestMethod.POST) public ResponseEntity<List<User>> getUsers(final User user) { log.info("根據條件查詢使用者"); return service.getUsers(user); } }
@ApiModel(value = "使用者模型") public class User { @ApiModelProperty("使用者ID") private int useId; @ApiModelProperty("使用者姓名") private String useName; @ApiModelProperty("使用者性別") private UseSex useSex; @ApiModelProperty("使用者年齡") private int useAge; @ApiModelProperty("使用者身份證號") private String useIdNo; @ApiModelProperty("使用者手機號") private String usePhoneNum; @ApiModelProperty("使用者郵箱") private String useEmail; @ApiModelProperty("建立時間") private LocalDateTime createTime; @ApiModelProperty("修改時間") private LocalDateTime modifyTime; @ApiModelProperty("使用者狀態") private UseState useState; }
第五步:啟動應用,瀏覽器請求
http://localhost:8080/swagger-ui.html