Spring 3 + Swagger 搭建
Maven 依赖:
<dependencies>
<dependency><groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>0.9.5</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</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>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>
</dependencies>
所需 jar 包
swagger-springmvc-1.0.2.jar
swagger-annotations-1.3.11.jar
guava-15.0.jar
classmate-1.1.0.jar
slf4j-api-1.7.7.jar
joda-time-2.5.jar
spring-web-3.2.9.RELEASE.jar
spring-aop-3.2.9.RELEASE.jar
aopalliance-1.0.jar
spring-beans-3.2.9.RELEASE.jar
spring-context-3.2.9.RELEASE.jar
spring-core-3.2.9.RELEASE.jar
commons-logging-1.1.3.jar
spring-webmvc-3.2.9.RELEASE.jar
spring-expression-3.2.9.RELEASE.jar
swagger-models-1.0.2.jar
commons-fileupload-1.3.1.jar
commons-io-2.2.jar
jackson-core-2.5.1.jar
jackson-databind-2.5.1.jar
jackson-annotations-2.5.1.jar
fastjson-1.2.7.jar
1. SwaggerConfig 类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;
@Configuration
@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;
}
}
2. UserController 类
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
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.ResponseBody;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
@Api(value = "user")
@Controller
@RequestMapping(value = "/user")
public class UserController {
@ApiOperation(value = "根据ID获取用户信息", httpMethod = "GET", notes = "根据username获取用户信息", response = List.class)
@ResponseBody
@RequestMapping(value = "/queryUser", method = RequestMethod.GET)
public List<User> queryUser(@RequestParam(required = false, defaultValue = "a") String username) {
List<User> userList = new ArrayList<User>();
User u = new User();
u.age = 11;
u.nickName = "aa";
u.username = "123123";
userList.add(u);
return userList;
}
}
class User {
public String username;
public String nickName;
public int age;
}
3. spring-mvc.xml 添加配置
<mvc:resources mapping="/swagger/**" location="/swagger/" />
<bean class="com.mangofactory.swagger.configuration.SpringSwaggerConfig" />
<!-- 将自定义的swagger配置类加载到spring容器 -->
<bean class="com.test.spring.config.SwaggerConfig" />
4. webapp 目录添加swagger-ui
去 github 找 3.0以下,2.0以上的ui 包,建议2.2.8以上 ;
找到之后解压将 dist里面所有文件 放在 webapp/swagger/ 下面, 和 spring-mvc.xml 里的<mvc:resources location="/xxxxx/" 保持一致;
替换 index.html 里面的 “http://petstore.swagger.io/v2/swagger.json”为你当前项目路径“http://主机名或IP:端口/项目名/api-docs”,比如:http://localhost:8080/spring-swagger/api-docs 。
5. 启动Tomcat容器访问
http://localhost:8080/spring-swagger/swagger/index.html