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.
Press CTRL + C to copy the code<textarea></textarea>Press CTRL + C to copy the code
3, Application.java
1 package com.xxx.firstboot; 2 3 import org.springframework.boot.SpringApplication; 4 Import Org.springframework.boot.autoconfigure.SpringBootApplication; 5 6 Import springfox.documentation.swagger2.annotations.EnableSwagger2; 7 8 @SpringBootApplication // Same as @[email Protected][email protected] 9 @EnableSwagger2 //Start Swagger Annotations public class application {one-by-one pub Lic static void Main (string[] args) { springapplication.run (application.class, args); }15 16}
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 package com.xxx.firstboot.web; 2 3 Import org.springframework.beans.factory.annotation.Autowired; 4 Import Org.springframework.web.bind.annotation.RequestHeader; 5 Import org.springframework.web.bind.annotation.RequestMapping; 6 Import Org.springframework.web.bind.annotation.RequestMethod; 7 Import Org.springframework.web.bind.annotation.RequestParam; 8 Import Org.springframework.web.bind.annotation.RestController; 9 Import com.xxx.firstboot.domain.user;11 Import com.xxx.firstboot.service.userservice;12 Import IO.SWAGGER.ANNOTATIONS.API;14 Import io.swagger.annotations.apiimplicitparam;15 Import IO.SWAGGER.ANNOTATIONS.APIIMPLICITPARAMS;16 Import io.swagger.annotations.apioperation;17 Import IO.SWAGGER.ANNOTATIONS.APIRESPONSE;18 Import io.swagger.annotations.apiresponses;19 @RestController21 @ Requestmapping ("/user") @Api ("Usercontroller related API") * public class Usercontroller {@Autowired26 private Us Erservice userservice;27//@Autowired29//PrivaTe myredistemplate myredistemplate;30 @ApiOperation ("Get user Information") @ApiImplicitParams ({@ApiImplicitPara M (paramtype= "header", name= "username", datatype= "String", required=true,value= "user's name", defaultvalue= "Zhaojigang"), @ApiImplicitParam (paramtype= "Query", name= "password", datatype= "String", required=true,value= "user's password", Defaultvalue= "Wangna") @ApiResponses ({PNs @ApiResponse (code=400,message= "request parameter not completed"), @ApiRe Sponse (code=404,message= "Request path does not have or page jump path is not")) (value= "/getuser", Method=requestmethod.get) 41 Public User GetUser (@RequestHeader ("username") string username, @RequestParam ("password") string password) {$ re Turn Userservice.getuser (username,password);}44//@RequestMapping ("/testjediscluster")//Public Us Er testjediscluster (@RequestParam ("username") string username) {//String value = Myredistemplate.get (myconstant S.user_forward_cache_prefix, username); 48// if (Stringutils.isblank (value)) {Myredistemplate.set (Myconstants.user_forward_cache_prefix, username, JS On.tojsonstring (GetUser ()));//return null;51//}52//Return Json.parseobject (value, user.cl ); 53//}54 55}
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
- @ApiModel: Describes the information of a model (this is typically used when post creation, using a scenario such as @requestbody, where request parameters cannot be described using @apiimplicitparam annotations)
- @ApiModelProperty: Describe the properties of a model
These are some of the most commonly used annotations.
It is important to note that:
- Apiimplicitparam This annotation is not just an annotation, it also affects the run-time program, as in the following example:
If the apiimplicitparam in the phone paramtype is a query, it is not injected into the rest path, and if it is path, it is not necessary to configure Apiimplicitparam, even if configured, where the value= "Mobile phone number" will not be displayed in Swagger-ui.
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:
Http://www.cnblogs.com/java-zhao/p/5348113.html
Fourth Chapter Springboot + swagger