springMVC integration swagger (personally tested fully available)

What swagger is: 


[Plain] Copy Plain View 
 
Swagger is a RESTFUL document interface automatically generates online + functional test software functionality. This article briefly describes how integrated swagger in the project and some common problems. If you want in-depth analysis of project source code to learn more, see Resources.  
Swagger is a standardized and complete framework for generating, description, and visualization calls for RESTful Web services. The overall objective is to make the client and the file system as a server at the same rate updates. Methods, parameters and models are tightly integrated into the file server-side code that allows the API to always be in sync. Swagger allow management to deploy and use powerful API has never been easier.  

The swagger springmvc integrated into the project to go: 
First, add a swagger dependence, the authors use maven management: 

[HTML] View Plain Copy
 
 <! - swagger ->   
<dependency>     
    <groupId> com.mangofactory </ groupId>     
    <artifactId> SpringMVC-Swagger </ the artifactId>     
    <Version> 1.0 . 2 </ Version>     
</ dependency>   
<dependency>  
    <groupId>com.fasterxml.jackson.core</groupId>  
    <artifactId>jackson-core</artifactId>  
    <version>2.5.1</version>  
</dependency>  
<dependency>  
    <groupId>com.fasterxml.jackson.core</groupId>  
    <artifactId>jackson-databind</artifactId>  
    <version>2.5.1</version>  
</dependency>  
<dependency>  
    <groupId>com.fasterxml.jackson.core</groupId>  
    <artifactId>jackson-annotations</artifactId>  
    <version>2.5.1</ Version>  
Create custom swagger initial configuration file:
</ dependency>   


[java] view plain copy
 
package com.yrok.swagger;  
  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.context.annotation.Bean;  
  
import com.mangofactory.swagger.configuration.SpringSwaggerConfig;  
import com.mangofactory.swagger.models.dto.ApiInfo;  
import com.mangofactory.swagger.plugin.EnableSwagger;  
import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin;  
  
@EnableSwagger  
public class SwaggerConfig {  
  
    private SpringSwaggerConfig springSwaggerConfig;  
  
    /** 
     * Required to autowire SpringSwaggerConfig 
     */  
    @Autowired  
    public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig)  
    {  
        this.springSwaggerConfig = springSwaggerConfig;  
    }  
  
    /** 
     * Every SwaggerSpringMvcPlugin bean is picked up by the swagger-mvc 
     * framework - allowing for multiple swagger groups i.e. same code base 
     * multiple swagger resource listings. 
     */  
    @Bean  
    public SwaggerSpringMvcPlugin customImplementation()  
    {  
        return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)  
                .apiInfo(apiInfo())  
                .includePatterns(".*?");  
    }  
  
    private ApiInfo apiInfo()  
    {  
        ApiInfo apiInfo = new ApiInfo(  
                "springmvc搭建swagger",  
                "spring-API swagger测试",  
                "My Apps API terms of service",  
                "[email protected]",  
                "web app",  
                "My Apps API License URL");  
        return apiInfo;  
    }  
}  
The type and configuration dependent swagger SpringSwaggerConfig spring loaded into a container: 

[HTML] Plain View Copy
 
 
Import org.springframework.stereotype.Controller;   <- Enable MVC annotation -!>   
: <MVC Annotation-Driven />   
<! - spring loaded into the container springSwaggerConfig ->   
<the bean class = " com.mangofactory.swagger.configuration.SpringSwaggerConfig " />   
<-! custom configuration class of swagger spring loaded into the container ->   
<the bean class = " com.yrok.swagger.SwaggerConfig " />   
Related to the configuration controller API: 

[Java] Copy Plain View 
 
Package com.yrok.controller;   
  
Import javax.annotation.Resource;   
  
Import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RequestParam;  
import org.springframework.web.bind.annotation.ResponseBody;  
  
import com.wordnik.swagger.annotations.Api;  
import com.wordnik.swagger.annotations.ApiOperation;  
import com.wordnik.swagger.annotations.ApiParam;  
import com.yrok.entity.User;  
import com.yrok.service.UserService;  
@Api(value="user")  
@Controller  
@RequestMapping(value="/user")  
public class UserController {  
  
    @Resource  
    UserService userService;  
  
    @RequestMapping(value = "/getUser")  
    @ResponseBody  
    @ApiOperation(value="根据ID获取用户信息",httpMethod="GET",notes="get user by id",response=User.class)  
    public User getUser(@ApiParam(required=true,value="用户ID",name="userId")@RequestParam(value="userId")Integer userId) {  
        return userService.getUserByID(userId);  
    }  
      
}  
And Swagger UI Integration: 
download on GitHub will swaggerui, address: HTTPS: // github.com/swagger-api/swagger-ui 

unzip the dist folder of all the files are copied to the webapp / Swagger Swagger is the author here you can write custom directory that you have created. 

Modify index.html in HTTP: // petstore.swagger.wordnik.com/v2/swagger.json modify for their own project path + api-docs, for example: 

HTTP: // localhost: 8080 / SSMSwagger / API-docs: SSMSwagger project name. 

In the spring filter out swagger- -mvc.xml in ui files: 


[HTML] View Plain Copy
 
 <- - static resource files, Spring MVC will not be intercepted!>   
<MVC: Resources Mapping = " / Swagger / ** " LOCATION = " / swagger / " />   
here is substantially complete and integrated springmvc swagger, the following test results:
Access in the browser: HTTP: // localhost: 8080 / SSMSwagger / Swagger / index.html # / the User / getUser 



this, this article has ended, and share other information refer to the following: 

Reference Address: 
official website: HTTP: / / swagger.io/ 
GitHub: 
Swagger -springmvc: HTTPS: // github.com/martypitt/swagger-springmvc 
Swagger-ui: HTTPS: // github.com/swagger-api/swagger-ui 
Swagger-Core: HTTPS: / / github.com/swagger-api/swagger-core 
Swagger-spec: HTTPS: // github.com/swagger-api/swagger-spec

 

Guess you like

Origin www.cnblogs.com/Jeely/p/11313230.html