Springswagger2 is an example of using Swagger2 in the Spring Boot project.
This article describes the examples of using Swagger2 in the Spring Boot project and shares them with you as follows:
Add Swagger2 dependency
Add Swagger2 dependency to 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 the Swagger2 configuration class at the Application. java level.
Import org. springframework. context. annotation. bean; import org. springframework. context. annotation. configuration; import springfox.doc umentation. builders. apiInfoBuilder; import springfox.doc umentation. builders. pathSelectors; import springfox.doc umentation. builders. requestHandlerSelectors; import springfox.doc umentation. service. apiInfo; import springfox.doc umentation. spi. documentationType; import springfox.doc umentation. spring. web. plugins. docket; import springfox.doc umentation. swagger2.annotations. enableSwagger2; @ Configuration @ EnableSwagger2public class Swagger2 {@ Bean public Docket createRestApi () {return new Docket (DocumentationType. SWAGGER_2 ). apiInfo ()). select (). apis (RequestHandlerSelectors. basePackage ("your own external interface package name ")). paths (PathSelectors. any ()). build ();} private ApiInfo apiInfo () {return new ApiInfoBuilder (). title ("net 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, you can actually produce the document content. However, such a document mainly targets the request itself, and the description mainly comes from the generation of functions and other names, which is unfriendly to users, we usually need to add 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 a user, register ** @ param loginName * Logon account * @ param userName * User name * @ param password * logon password * @ param roleId * User Role * @ return * @ throws ResourceExistsException */@ apiOperation (value = "Add User ") @ PostMapping ("/") public JsonResult create (@ ApiParam (name = "loginName", value = "Logon 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 = "Logon 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 User = userService. create (loginName, password, userName, roleId); return new JsonResult (user );}}
View API
Start the Spring Boot program and access: http: // localhost: 8080/swagger-ui.html
API document access and debugging
In addition to viewing interface functions, Swagger also provides debugging and testing functions. You can click Model Schema on the right side of the page (yellow area: it specifies the data structure ), in this case, the user object template is available in the Value. We only need to modify it slightly. Click Try it out below! Button to complete a request call! You can use several GET requests to verify whether the previous POST request is correct.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.