Springswagger2 is an example of using Swagger2 in the Spring Boot project.

Source: Internet
Author: User
Tags neo4j

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.