SpringMVC使用Swagger
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。
我们这里使用SpringBoot来快速搭建一个MVC,同时使用Swagger插件。
pom.xml,主要是引用swagger2
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath />
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Swagger2配置类
package cn.duanjt.config; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration
@EnableSwagger2
public class Swagger2 {
/**
* 创建API应用
* apiInfo() 增加API相关信息
* 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
* 本例采用指定扫描的包路径来定义指定要建立API的目录。
*
* @return
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("cn.duanjt.controller"))//指定要扫描的包名
.paths(PathSelectors.any())
.build();
} /**
* 创建该API的基本信息(这些基本信息会展现在文档页面中)
* 访问地址:http://项目实际地址/swagger-ui.html
* @return
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot中使用Swagger2构建RESTful APIs")
.description("更多请关注http://www.baidu.com")
.termsOfServiceUrl("http://www.baidu.com")
.contact("段江涛")
.version("1.0")
.build();
}
}
控制器StudentController
package cn.duanjt.controller; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import cn.duanjt.pojo.Student;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation; @RestController
@RequestMapping("/student")
@Api(description = "学生信息")
public class StudentController { @RequestMapping(path = "/getAll", method = RequestMethod.GET)
@ApiOperation(value = "获取所有用户信息", notes = "备注:获取所有用户信息")
public Student getAll() {
Student stu = new Student(1, "zhangsan", 20);
return stu;
} @RequestMapping(path = "/getById", method = RequestMethod.GET)
@ApiOperation(value = "根据Id查询用户信息", notes = "备注:根据Id查询用户信息")
@ApiImplicitParam(paramType = "query", name = "id", value = "用户id", required = true, dataType = "Integer")
public Student getById(int id) {
Student stu = new Student(id, "zhangsan", 20);
return stu;
} @RequestMapping(path = "/insert", method = RequestMethod.POST)
@ApiOperation(value = "新增用户信息", notes = "备注:新增用户信息")
public String insert(Student student){
System.out.println(student.toString());
return "成功";
}
}
说明:
1.需要在配置类Swagger2中指定需要扫描的包名
2.@Api标记在类上面,用于表示该类需要被Swagger扫描
3.@ApiOperation标记方法
4.@ApiImplicitParam标记方法,描述参数信息
运行之后,在浏览器输入:http://localhost:8080/swagger-ui.html

原文参考:https://blog.csdn.net/sanyaoxu_2/article/details/80555328
SpringMVC使用Swagger的更多相关文章
- 一步步完成Maven+SpringMVC+SpringFox+Swagger整合示例
本文给出一个整合Maven+SpringMVC+SpringFOX+Swagger的示例,并且一步步给出完成步骤. 本人在做实例时发现 http://blog.csdn.net/zth1002/art ...
- spring-mvc集成 swagger
问题1:spring-mvc集成 swagger, 配置好后界面 404, 原因: dispatcher-servlet.xml 文件中, 要在这上面 <!-- 启用spring mvc 注解 ...
- SpringMVC集成Swagger插件以及Swagger注解的简单使用
一.简介 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新 .接口的方法,参数和模型 ...
- springMVC整合swagger(亲自试验完全可用)
swagger是什么: [plain] view plain copy Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件.本文简单介绍了在项目中集成swagger的方法和一 ...
- SpringMVC+JWT+Swagger UI+RestFul
前言: 其实很早就想写这篇文章了,因为我觉得这会对很多新手有指引作用,当初自己也是瞎子过河的摸索着过来的.目前后台开发比较流行的MVC框架中使用Spring MVC还是比较多的,当然还有Spring ...
- springmvc整合swagger
前言 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新.文件的方法,参数和模型紧密集 ...
- Maven+SpringMVC+SpringFox+Swagger整合示例
查考链接:https://my.oschina.net/wangmengjun/blog/907679 coding地址:https://git.coding.net/conding_hjy/Spri ...
- SpringMVC融合Swagger UI使用
相信大家都很熟悉springmvc,在用其进行开发工作的时候,有没有遇到几个小问题?比如: 1.前后端分离的模式下,前端开发人员如何得知后端的开发进度,有哪些接口可用? 2.后端开发人员在测试自己的接 ...
- springmvc使用swagger生成rest api文档
pom.xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-s ...
随机推荐
- 推举算法 AdaBoost 哥德尔奖 Godel Prize
推举算法 AdaBoost 2003年理论计算机科学界最高奖 哥德尔奖 Godel Prize
- Web开发——HTML基础(图像、音频和视频内容)
参考: 参考:HTML中的图像 参考:视频和音频内容 目录: 1.HTML中的图像 1.1 我们如何在网页上放置图像? (1)替代文字(alt) (2)宽度和高度 (3)图片标题 1.2 用图形和图形 ...
- 用maven创建一个web项目
下面所使用的Eclipse开发工具为Eclipse Java EE IDE 版本. 1.创建一个maven项目,如图所示: 选择“maven-archetype-webapp”,如图所示: 后面几步按 ...
- python数据结构-如何实现用户的历史记录功能
如何实现用户的历史记录功能 使用collections中的deque from collections import deque dq = deque([], 5) dq.append(1) dq.a ...
- VC++运行库 集32位/64位整合版
运行程序时,win7/win10(x86和x64)常会遇到缺少什么缺少msvc***.dll问题 安装下面链接提供的程序,安装后,便可解决. [2016-10-10]Microsoft Visual ...
- idea 更换svn地址
右键项目-->Subversion-->Relocate 上面是旧的SVN地址,下面填入你要更换的目标SVN地址
- git忽略某些文件的几种方法
不知道为什么我记得我写过这个内容但是又找不到了,只好照着大致记忆写一下以备留存. 1.首先肯定是.gitignore文件 .gitignore文件适合在文件尚未被追踪时加入,将其忽略便可以不上传到远程 ...
- 正则表达式中test,match,exec区别
testtest 返回 Boolean,查找对应的字符串中是否存在模式.var str = "1a1b1c";var reg = new RegExp("1." ...
- Template模板
目标 模板介绍 模板变量 常用标签 常用过滤器 自定义过滤器 模板结构 加载静态文件 一 模板介绍 在之前的章节中,视图函数只是直接返回文本,而在实际生产环境中其实很少这样用,因为实际的页面大多是带 ...
- Pandas截取列的一部分
以股票代码为例: 型式为:6位数字+"."+交易所代码,如600028.SH 如只需保留前6位: pattern = '(\w+)(?:.SZ|.SH)$' df['股票代码'] ...