Spring boot Swagger configuration, springbootswagger

Source: Internet
Author: User

Spring boot Swagger configuration, springbootswagger

Swagger is a tool used to describe and test restful interfaces. You only need to add some class and method description annotations when defining restful interfaces, through simple configuration, you can get a page that displays the interface definition, or set the parameter submission test interface on the page (replacing some postman functions ).

 

After the interface is modified, you do not need to modify the description document separately. swagger automatically generates the interface document. The following describes how to build a simple swagger test Demo.

1. Create a maven project for SpringBoot

For details about how to create a project, refer to my blog Spring Boot restful service release. The directory after the project is created is as follows:

 

 

2. Add the swagger dependent package to the pom. xml file after creation.
 1       <dependency> 2  3           <groupId>io.springfox</groupId> 4  5           <artifactId>springfox-swagger2</artifactId> 6  7           <version>2.8.0</version> 8  9         </dependency>10 11       <dependency>12 13           <groupId>io.springfox</groupId>14 15           <artifactId>springfox-swagger-ui</artifactId>16 17           <version>2.8.0</version>18 19       </dependency>

 

Directly use the appache Repository:

 

1         <repository>  2             <id>springfox-swagger</id>  3             <url>https://mvnrepository.com/artifact/io.springfox/springfox-swagger2</url>  4         </repository>5         <repository>  6             <id>springfox-swagger-ui</id>  7             <url>https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui</url>  8         </repository>
3. Add the swagger configuration loading class (Swagger2Config. java)
1 package com. elon. springbootdemo. config; 2 3 import org. springframework. context. annotation. bean; 4 import org. springframework. context. annotation. configuration; 5 import org. springframework. web. servlet. config. annotation. webMvcConfigurerAdapter; 6 7 import springfox.doc umentation. builders. apiInfoBuilder; 8 import springfox.doc umentation. builders. pathSelectors; 9 import springfox.doc umentation. builders. requestHandlerSelectors; 10 import springfox.doc umentation. service. apiInfo; 11 import springfox.doc umentation. spi. documentationType; 12 import springfox.doc umentation. spring. web. plugins. docket; 13 import springfox.doc umentation. swagger2.annotations. enableSwagger2; 14 15 @ Configuration16 @ EnableSwagger217 public class Swagger2Config extends webmvcjavaseradapter {18 @ Bean19 public Docket api () {20 return new Docket (DocumentationType. SWAGGER_2) 21. select () 22. apis (RequestHandlerSelectors. basePackage ("com. elon. springbootdemo. ws ") 23. paths (PathSelectors. any () 24. build () 25. apiInfo (getApiInfo (); 26} 27 28 private ApiInfo getApiInfo () 29 {30 ApiInfo apiInfo = new ApiInfoBuilder (). title ("user management module") 31. description ("interface for adding, deleting, and modifying user data") 32. termsOfServiceUrl ("http://www.cnblogs.com/elon") 33. version ("1.0") 34. build (); 35 return apiInfo; 36} 37}
4. Add the restful API for testing (WSUserSwagger. java)
1 package com. elon. springbootdemo. ws; 2 3 import org. springframework. web. bind. annotation. pathVariable; 4 import org. springframework. web. bind. annotation. requestBody; 5 import org. springframework. web. bind. annotation. requestHeader; 6 import org. springframework. web. bind. annotation. requestMapping; 7 import org. springframework. web. bind. annotation. requestMethod; 8 import org. springframework. web. bind. annotation. requestParam; 9 import org. springframework. web. bind. annotation. restController; 10 11 import io. swagger. annotations. api; 12 import io. swagger. annotations. apiOperation; 13 14 @ RestController15 @ RequestMapping (value = "swagger-demo") 16 @ Api (value = "WSUserSwagger", description = "User Information Management ") 17 public class WSUserSwagger {18 19 @ ApiOperation (value = "Add user", notes = "Add user") 20 @ RequestMapping (value = "/v1/user ", method = RequestMethod. POST) 21 public String addUser (@ RequestBody String userInfo) {22 return "Add user:" + userInfo; 23} 24 25 @ ApiOperation (value = "query users by name ", notes = "query users by name") 26 @ RequestMapping (value = "/v1/user", method = RequestMethod. GET) 27 public String queryUserByName (@ RequestParam ("name") String name, @ RequestHeader ("age") int age) {28 return name + age; 29} 30 31 @ ApiOperation (value = "delete user", notes = "delete user") 32 @ RequestMapping (value = "/v1/user/{name }", method = RequestMethod. DELETE) 33 public String deleteUser (@ PathVariable ("name") String name) {34 return "delete" + name; 35} 36} 
5. Test After startup

Enter http: // localhost: 8080/swagger-ui.html in your browser. On the page, you can see the defined interface:

 

 

Test the GET method: click "Try it out" and enter a parameter. Click "execute" to run the command to view the returned results after the interface is executed.

 

 

 

Related Article

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.