springmvc与swagger2
首先呢我们导入相关的jar包文件
为了方便copy我copy一份
<!-- 导入java ee jar 包 -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<!-- spring核心 jar -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
<!-- jacksion -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${swagger.version}</version>
</dependency>
以上jar包有 spring的ioc的bean 上下文的context 还有springmvc的web 以集webmc swagger2的整合還有swagger2返回的json数据用到的jar包
接下来我们分层编写
我嫩以User用户为案例 编写实体类
package cn.bdqn.beans;
/**
* 实体类
* @author ljn
* @date 2017-10-21
* @vesion 1.0
* @detail
*/
public class User {
protected Integer id;
protected String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
public User() {
}
}
接下来我们编写 SwaggerConfig类文件
copy 如下:
package cn.bdqn.swagger;
import io.swagger.annotations.BasicAuthDefinition;
import io.swagger.annotations.SwaggerDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* 启动@EnableSwagger2 swagger2 @EnableWebMvc mvc项目 扫描我的controller类 @componentScan
* 配置变量
* @author ljn
* @date 2017-10-21
* @vesion 1.0
* @detail
*/
@Configuration
@EnableSwagger2
@EnableWebMvc
@ComponentScan(basePackages={"cn.bdqn.controller"})
public class SwaggerConfig {
@Bean
public Docket getDocket(){
return new Docket(DocumentationType.SWAGGER_2).apiInfo(getApiInfo());
}
public ApiInfo getApiInfo(){
ApiInfoBuilder ab=new ApiInfoBuilder();
ab.title("title");
ab.description("描述");
return ab.build();
}
}
最后我们编写controller
以下是代码
@Controller("userController")
@Api(value = "干嘛的类", description = "描述")
@RequestMapping("user")
public class UserController {
@RequestMapping("getLists")
@ApiOperation(value = "查询集合", response = User.class, httpMethod = "POST")
public List<User> getLists(@ApiParam(value = "title") String title) {
List<User> lists = new ArrayList<User>();
User user = new User();
user.setName(title);
user.setId(1);
User user1 = new User();
user1.setName(title);
user1.setId(1);
lists.add(user);
lists.add(user1);
return lists;
}
}
还有编写 springmvc.xml 文件
copy 过来 如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:ctx="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 开启注解 -->
<mvc:annotation-driven />
<!-- 扫描controller -->
<ctx:component-scan base-package="cn.bdqn.controller.UserController" />
<!-- 生成bean的swagger文件 -->
<bean class="cn.bdqn.swagger.SwaggerConfig"></bean>
<!-- 视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/META-INF/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- swagger的静态文件放行 -->
<mvc:resources location="classpath:/META-INF/resources/"
mapping="swagger-ui.html" />
<mvc:resources location="classpath:/META-INF/resources/webjars/"
mapping="/webjars/**" />
</beans>
最后编写 web.xml文件
然后我们启动服务器tomcat 访问
localhost:8080/bokeyuan/swagger-ui.html 这是swagger api的路径