Note: This document is referenced from
Http://www.jianshu.com/p/0465a2b837d2
Swagger is used to define API documentation.
Benefits:
- Front-end separation development
- API documentation is very clear
- You do not need to use the URL input browser to access the controller when testing
- The traditional way to test input URLs is cumbersome for post requests (of course, you can use a browser plugin like postman)
- The integration of Spring-boot and swagger a simple force
1. Project structure
As in the previous section, there is no change.
2, Pom.xml
Two jars were introduced.
1 <Dependency>2 <groupId>Io.springfox</groupId>3 <Artifactid>Springfox-swagger2</Artifactid>4 <version>2.2.2</version>5 </Dependency>6 <Dependency>7 <groupId>Io.springfox</groupId>8 <Artifactid>Springfox-swagger-ui</Artifactid>9 <version>2.2.2</version>Ten </Dependency>
View Code
3, Application.java
1 PackageCom.xxx.firstboot;2 3 Importorg.springframework.boot.SpringApplication;4 Importorg.springframework.boot.autoconfigure.SpringBootApplication;5 6 ImportSpringfox.documentation.swagger2.annotations.EnableSwagger2;7 8@SpringBootApplication//Same as @[email Protected][email protected]9@EnableSwagger2//Start swagger AnnotationsTen Public classApplication { One A Public Static voidMain (string[] args) { -Springapplication.run (Application.class, args); - } the -}
View Code
Description
- An annotation @EnableSwagger2 is introduced to start the swagger annotation. (Starting the annotation makes the swagger annotation in the controller take effect, and the scope of coverage is specified by the configuration of @componentscan, which is specified by default as all controllers under the root path "Com.xxx.firstboot")
4, Usercontroller.java
1 PackageCom.xxx.firstboot.web;2 3 Importorg.springframework.beans.factory.annotation.Autowired;4 ImportOrg.springframework.web.bind.annotation.RequestHeader;5 Importorg.springframework.web.bind.annotation.RequestMapping;6 ImportOrg.springframework.web.bind.annotation.RequestMethod;7 ImportOrg.springframework.web.bind.annotation.RequestParam;8 ImportOrg.springframework.web.bind.annotation.RestController;9 Ten ImportCom.xxx.firstboot.domain.User; One ImportCom.xxx.firstboot.service.UserService; A - ImportIo.swagger.annotations.Api; - ImportIo.swagger.annotations.ApiImplicitParam; the ImportIo.swagger.annotations.ApiImplicitParams; - Importio.swagger.annotations.ApiOperation; - ImportIo.swagger.annotations.ApiResponse; - Importio.swagger.annotations.ApiResponses; + - @RestController +@RequestMapping ("/user") A@Api ("Usercontroller related APIs") at Public classUsercontroller { - - @Autowired - PrivateUserService UserService; - - //@Autowired in //private myredistemplate myredistemplate; - to@ApiOperation ("Get user Information") + @ApiImplicitParams ({ -@ApiImplicitParam (paramtype= "header", name= "username", datatype= "String", required=true, value= "User's name", defaultvalue= "Zhaojigang"), the@ApiImplicitParam (paramtype= "Query", name= "password", datatype= "String", required=true, value= "User's password", defaultvalue= "Wangna") * }) $ @ApiResponses ({Panax Notoginseng@ApiResponse (code=400,message= "request parameter not completed"), -@ApiResponse (code=404,message= "Request path not or page jump path is not correct") the }) +@RequestMapping (value= "/getuser", method=requestmethod.get) A PublicUser GetUser (@RequestHeader ("username") String username, @RequestParam ("Password") String password) { the returnUserservice.getuser (Username,password); + } - $ //@RequestMapping ("/testjediscluster") $ //Public User Testjediscluster (@RequestParam ("username") String username) { - //String value = Myredistemplate.get (Myconstants.user_forward_cache_prefix, username); - //if (Stringutils.isblank (value)) { the //Myredistemplate.set (myconstants.user_forward_cache_prefix, username, json.tojsonstring (GetUser ())); - //return null;Wuyi // } the //return Json.parseobject (value, user.class); - // } Wu -}
View Code
Description
- @Api: Used on a class to illustrate the role of the class
- @ApiOperation: Use the method to illustrate the role of the method
- @ApiImplicitParams: Use the method to include a set of parameter descriptions
- @ApiImplicitParam: Use in @apiimplicitparams Annotations to specify aspects of a request parameter
- Paramtype: Where to put the parameters
- Header--> Request Parameter acquisition: @RequestHeader
- Query--> Request Parameter acquisition: @RequestParam
- Path (for restful interfaces)--Get the request parameter: @PathVariable
- Body (not used)
- Form (not commonly used)
- Name: Name of parameter
- DataType: Parameter types
- Required: Whether the parameter must be passed
- Value: The meaning of the parameter
- DefaultValue: Default values for parameters
- @ApiResponses: Used to represent a set of responses
- @ApiResponse: Used in @apiresponses, generally used to express a wrong response information
- Code: number, e.g. 400
- Message: Information, such as "request parameters not filled out."
- Response: class that throws an exception
These are some of the most commonly used annotations.
For specific other annotations, see:
Https://github.com/swagger-api/swagger-core/wiki/Annotations#apimodel
Test:
Start service, browser input "http://localhost:8080/swagger-ui.html"
Top One red box: @Api
Get red box: Method=requestmethod. GET
Red box on the right: @ApiOperation
parameter Red Frame:@ApiImplicitParams series annotations
Response Messages Red Frame: @ApiResponses series annotations
After entering the parameters, click "Try it out!" to view the response content:
Fourth Chapter Springboot + swagger