This article introduces an example of using Swagger2 in Spring Boot project. It is shared with you. The details are as follows:
Add Swagger2 dependencies
Add Swagger2 dependencies in pom.xml
<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>
Create a Swagger2 configuration class
Create Swagger2's configuration class Swagger2 in the Application.java simultaneous level.
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.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;@Configuration@EnableSwagger2public class Swagger2 { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("Your own external interface package name")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Neo4j RESTful APIs") .description("The Neo4j RESTful APIs description/") .termsOfServiceUrl("") .contact("Li Qinghai") .version("5.0") .build(); }}Add document content
After completing the above configuration, the document content can actually be produced, but such documents are mainly aimed at the request itself, and the description mainly comes from the naming of functions and other functions, which is not user-friendly. We usually need to add some instructions to enrich the document content.
import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;import io.swagger.annotations.ApiParam;/** * System User Controller * * @author Li Qinghai* */@Api(value = "System User Interface", tags = "System Management")@RestController@RequestMapping("/v3/edu/users")public class UserController { @Autowired private UserService userService; /** * Add user, register * * @param loginName * Log in to account* @param userName * User name* @param password * Login password* @param roleId * User role* @return * @throws ResourceExistsException */ @ApiOperation(value = "Add user") @PostMapping("/") public JsonResult create( @ApiParam(name = "loginName", value = "Login account", required = true) @RequestParam(required = true) @RequestBody String loginName, @ApiParam(name = "userName", value = "User name", required = true) @RequestParam(required = true) @RequestBody String userName, @ApiParam(name = "password", value = "Log in password", required = true) @RequestParam(required = true) @RequestBody String password, @ApiParam(name = "roleId", value = "User Role Number", required = true) @RequestParam(required = true) @RequestBody String roleId) throws ResourceExistsException { boolean exists = this.userService.exists(loginName); if (exists) { throw new ResourceExistsException(loginName); } User user = userService.create(loginName, password, userName, roleId); return new JsonResult(user); }}View API
Start Spring Boot program and visit: http://localhost:8080/swagger-ui.html
API Document Access and Debugging
In addition to viewing the interface function, Swagger also provides debugging and testing functions. We can click on the Model Schema (yellow area: it indicates the data structure) on the right in the picture above. At this time, there is a template for the user object in the Value. We only need to modify it slightly, click Try it out below! Button, you can complete a request call! Several GET requests can be used to verify that the previous POST request is correct.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.