Swagger is used to define API documentation.
Benefits: The front-end separation Development API documentation is very explicit when testing without the need to use the URL input browser to access the controller traditional input URL test method for the POST request is more cumbersome to pass the parameter (of course, you can use the browser plug-in such as postman) The integration of Spring-boot and swagger a simple force
First, the dependency is introduced in the project Pom, as follows,
<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>
Next, create the Application.java in Springboot, as below,
Package com.xxx.firstboot;
Import org.springframework.boot.SpringApplication;
Import org.springframework.boot.autoconfigure.SpringBootApplication;
Import Springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication //same as @Configuration + @EnableAutoConfiguration + @ComponentScan
@EnableSwagger2 //Start swagger Annotation Public
class application {public
static void Main (string[] args) {
Springapplication.run (Application.class, args);
}
}
Description: An annotation @enableswagger2 was introduced to launch 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")
Next, create the Usercontroller.java, as below,
Package com.xxx.firstboot.web;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.web.bind.annotation.RequestHeader;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestMethod;
Import Org.springframework.web.bind.annotation.RequestParam;
Import Org.springframework.web.bind.annotation.RestController;
Import Com.xxx.firstboot.domain.User;
Import Com.xxx.firstboot.service.UserService;
Import Io.swagger.annotations.Api;
Import Io.swagger.annotations.ApiImplicitParam;
Import Io.swagger.annotations.ApiImplicitParams;
Import io.swagger.annotations.ApiOperation;
Import Io.swagger.annotations.ApiResponse;
Import io.swagger.annotations.ApiResponses; @RestController @RequestMapping ("/user") @Api ("Usercontroller related APIs") public class Usercontroller {@Autowired pri
Vate UserService UserService;
@Autowired//Private myredistemplate myredistemplate; @ApiOperation ("Get user Information") @ApiImplicitParams ({@ApiImplicitParam (paramtype= "header", name= "username", datatype= "String", Required=true,va Lue= "User's name", defaultvalue= "Zhaojigang"), @ApiImplicitParam (paramtype= "Query", name= "password", datatype= "String", required=true,value= "User's password", defaultvalue= "Wangna")}) @ApiResponses ({@ApiResponse (code=400,message= "Request parameter not Fill "), @ApiResponse (code=404,message=" Request path not or page jump path not ")}) @RequestMapping (value="/getuser ", method=requestm Ethod.
GET) public User getUser (@RequestHeader ("username") string username, @RequestParam ("password") string password) {
Return Userservice.getuser (Username,password); }//@RequestMapping ("/testjediscluster")//Public User Testjediscluster (@RequestParam ("username") String username ) {//String value = Myredistemplate.get (Myconstants.user_forward_cache_prefix, username);//if (stringutil S.isblank (value)) {//Myredistemplate.set (Myconstants.user_forward_cache_prefiX, username, json.tojsonstring (GetUser ()));
return null;
}//Return Json.parseobject (value, User.class); // }
}
Description
For specific other annotations, see:
Https://github.com/swagger-api/swagger-core/wiki/Annotations#apimodel
Then, in the browser input "http://localhost:8080/swagger-ui.html", you can see the following interface,
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:
Other references:
==http://www.cnblogs.com/java-zhao/p/5348113.html==
Https://github.com/swagger-api/swagger-core/wiki/Annotations#apimodel
Http://www.jianshu.com/p/8033ef83a8ed
Http://www.cnblogs.com/softidea/p/6251249.html