How to integrate SwaggerUI with SpringBoot Framework
This article mainly introduces the SpringBoot framework how to integrate SwaggerUI related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this SpringBoot framework how to integrate SwaggerUI article will have a harvest, let's take a look.
Integrate swagger for module testing
Note: in order to facilitate SpringBoot to better integrate Swagger, it needs to be placed in a module (maven sub-project)
Create common modules, integrate swagger, and use them for all modules
Common/pom.xml, import related dependencies
Org.springframework.boot spring-boot-starter-web provided com.baomidou mybatis-plus-boot-starter org.projectlombok lombok io.springfox springfox-swagger2 springfox-swagger-ui spring-boot-starter-data-redis
Create a module under the common module, such as service_base
Create a configuration class under this module (you need to follow the SpringBoot specification, the code is fixed)
Package com.xsha.servicebase;import com.google.common.base.Predicates;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import springfox.documentation.builders.ApiInfoBuilder;import springfox.documentation.builders.PathSelectors;import springfox.documentation.service.ApiInfo;import springfox.documentation.service.Contact;import springfox.documentation.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;import springfox.documentation.swagger2.annotations.EnableSwagger2 @ Configuration@EnableSwagger2public class SwaggerConfig {@ Bean public Docket webApiConfig () {return new Docket (DocumentationType.SWAGGER_2) .groupName ("webApi") .apiInfo (webApiInfo ()) .select () .notify (Predicates.not (PathSelectors.regex ("/ admin/.*")) .admin/.* (Predicates.not (PathSelectors. Regex ("/ error.*")) .build () } private ApiInfo webApiInfo () {return new ApiInfoBuilder () .title ("website title") .description ("description of the API document") .version ("1.0") .contact (new Contact ("java", "http://www.baidu.com"," 1234567890@qq.com "))} usage
Just introduce the above module into the pom.xml of other modules (preferably the outermost)
Com.xsha service_base 0.0.1-SNAPSHOT
Add ComponentScan annotations to the startup class of the module to specify the packages to be scanned. For example: @ ComponentScan (basePackages= {"com.xsha"})
Then start, access address: http://127.0.0.1:8001/swagger-ui.html
Unify the format of the returned results (custom results)
Create a module under the common module, such as common-utils
Create an interface dedicated to managing status codes
Public interface ResultCode {/ / define two status codes public static int SUCCESS = 20000; public static int ERROR = 40000;} define the return format (relatively fixed) package com.xsha.commonutils;import io.swagger.annotations.ApiModelProperty;import lombok.Data;import java.util.HashMap;import java.util.Map;// unified return result class @ Datapublic class R {@ ApiModelProperty (value = "successful") private Boolean success @ ApiModelProperty (value = "return code") private Integer code; @ ApiModelProperty (value = "return message") private String message; @ ApiModelProperty (value = "return data") private Map data = new HashMap (); / / make the constructor private private R () {} / / successful static method public static R ok () {R r = new R (); r.setSuccess (true) R.setCode (ResultCode.SUCCESS); r.setMessage (success); return r;} / / failure static method public static R error () {r.setSuccess (false); r.setCode (ResultCode.ERROR); r.setMessage (failure); public R success (Boolean success) {this.setSuccess (success); return this Public R message (String message) {this.setMessage (message); public R code (Integer code) {this.setCode (code); public R data (String key, Object value) {this.data.put (key, value); public R data (Map map) {this.setData (map);} usage
Just introduce the above module into the pom.xml of other modules (preferably the outermost)
Com.xsha common_utils 0.0.1-SNAPSHOT
The type of the result returned each time must be a custom return format class type
/ / please use rest style// 1.select all teachers data@ApiOperation (value = "all data lists") @ GetMapping ("findAll") public R findAllTeachers () {List teachers = teacherService.list (null); return R.ok () .data ("results", teachers) } / / request path mast have variable id// 2.logically delete teacher@ApiOperation (value= "logical data deletion") @ DeleteMapping ("{id}") public R logicDeleteTeacher (@ ApiParam (name= "id", value= "instructor ID", required = true) @ PathVariable String id) {boolean flag = teacherService.removeById (id); return flag? R.ok (): R.error ();}
Finally, just test it in swagger.
This is the end of the article on "how the SpringBoot Framework integrates SwaggerUI". Thank you for reading! I believe you all have a certain understanding of "SpringBoot framework how to integrate SwaggerUI" knowledge. If you want to learn more knowledge, you are welcome to follow the industry information channel.