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的路径
springmvc与swagger2的更多相关文章
- 快速构建springmvc+spring+swagger2环境
快速构建springmvc+spring+swagger2环境 开发工具:Intellij idea jdk: 1.8 开发步骤: 1.创建maven工程,如图建立工程结构 ...
- 利用Swagger2自动生成对外接口的文档
一直以来做对外的接口文档都比较原始,基本上都是手写的文档传来传去,最近发现了一个新玩具,可以在接口上省去不少麻烦. swagger是一款方便展示的API文档框架.它可以将接口的类型最全面的展示给对方开 ...
- SpringMVC+JWT+Swagger UI+RestFul
前言: 其实很早就想写这篇文章了,因为我觉得这会对很多新手有指引作用,当初自己也是瞎子过河的摸索着过来的.目前后台开发比较流行的MVC框架中使用Spring MVC还是比较多的,当然还有Spring ...
- SpringMVC中使用Swagger2整合
Swagger2是什么 Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件. Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 W ...
- MP实战系列(十)之SpringMVC集成SpringFox+Swagger2
该示例基于之前的实战系列,如果公司框架是使用JDK7以上及其Spring+MyBatis+SpringMVC/Spring+MyBatis Plus+SpringMVC可直接参考该实例. 不过建议最好 ...
- springmvc+swagger2
一.swagger2依赖 <!--swagger--> <dependency> <groupId>io.springfox</groupId> < ...
- 03、Swagger2和Springmvc整合详细记录(爬坑记录)
时间 内容 备注 2018年6月18日 基本使用 spirngmvc整合swagger2 开始之前这个系列博文基本是,在项目的使用中一些模块的内容记录,但是后期逐渐优化,不单单是整合内容. swagg ...
- 使用Swagger2构建SpringMVC项目中的Restful API文档
使用Swagger自动生成API文档,不仅增加了项目的可维护性,还提高了API的透明度更利于快速测试等工作,便于更快地发现和解决问题. 本篇文章只记录整合过程,关于Security Configura ...
- 基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建
基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建 前言 最近做回后台开发,重新抓起以前学过的SSM(Spring+Sp ...
随机推荐
- Python-SSH批量登陆并执行命令
Python-SSH批量登陆并执行命令 #!/usr/bin/env python #-*- coding:utf-8 -*- import paramiko from time import cti ...
- left join on和where 限制查询的区别在于
left join on: 会显示前表的所有数据,不满足显示为null或者为0 . 而where显示的为满足条件的记录,不满足但是存在的数据不显示. 做统计数据的时候,用join on比较合理.
- [LC] 221. Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and re ...
- OpenCVSharp对图像进行颜色分割
使用OpencvSharp的InRange函数对图像进行RGB颜色的分割. using System; using OpenCvSharp; using OpenCvSharp.Extensions; ...
- caffe之数据集介绍
数据集:http://bigdata.51cto.com/art/201702/531276.htm 计算机视觉 MNIST: 最通用的健全检查.25x25 的数据集,中心化,B&W 手写数字 ...
- Python实现线程交替打印字符串
import threading con = threading.Condition() word = u"12345上山打老虎" def work(): global word ...
- Python时间操作所相关
相关模块:time,datetime,calendar(日历模块) 获取当前时间: # 获取当前10位时间戳,默认返回为float类型 print int(time.time()) # output: ...
- 测试用例设计经典面试题之电梯、杯子、笔、桌子、洗衣机、椅子、ATM等
测试用例设计经典面试题之电梯.杯子.笔.桌子.洗衣机.椅子.ATM等 1.测试项目:电梯 需求测试:查看电梯使用说明书.安全说明书等 界面测试:查看电梯外观 功能测试:测试电梯能否实现正常的上升和下降 ...
- Center Loss - A Discriminative Feature Learning Approach for Deep Face Recognition
URL:http://ydwen.github.io/papers/WenECCV16.pdf这篇论文主要的贡献就是提出了Center Loss的损失函数,利用Softmax Loss和Center ...
- JAVA补0--->String.format()的使用
详细地址:http://blog.csdn.net/lonely_fireworks/article/details/7962171/ 标 志 说 明 示 例 结 果 + 为正 ...