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 ...
随机推荐
- js 第二课
=赋值 ==比较 ===绝对比较 &&且 || 或 !取反 a?1:0 a=ture a?1:0 function LeyBc() { var a={d:11,b:22,c:&quo ...
- git从已有分支拉新分支开发
开发过程中经常用到从master分支copy一个开发分支,下面我们就用命令行完成这个操作: 1. 切换到被copy的分支(master),并且从远端拉取最新版本 $git checkout maste ...
- ubuntu下设置mysql自启
- 洛谷试炼场 - 关卡2-1 - 简单的模拟 - (Done)
最近这段时间感冒外加一些乱七八糟的事情,导致脑子严重僵化……只好刷刷基础(水)题巩固巩固基础(混混题数). 目录 P1003 铺地毯 P1067 多项式输出 P1540 机器翻译 P1056 排座椅 ...
- HTTP Get Post究竟有哪些区别
get在浏览器回退时是无害的,而post会再次提交请求. get产生的url地址可以被bookmark,而post不可以. get请求会被浏览器主动cache,而post不会,除非手动设置. get请 ...
- 【Python全栈-后端开发】Django进阶之Model操作复习
Django进阶之Model操作复习 一.字段 AutoField(Field) - int自增列,必须填入参数 primary_key=True BigAutoField(AutoField) - ...
- ionic3.x版本-实现点击tab导航栏判断是否已经登陆然后加载不同页面,和退出登录功能。
html代码: <ion-tabs #myTabs> <ion-tab [root]="tab1Root" tabTitle="首页" tab ...
- python pynput监听键盘
"""小白随笔,大佬勿喷""" #键盘输入 from pynput.keyboard import Key,Controller,Liste ...
- 2018-2019-1 20189203《Linux内核原理与分析》第五周作业
第一部分 课本学习 用户态.内核态和中断 1.内核态:处于高的执行级别下,代码可以执行特权指令,访问任意的物理地址,这时的CPU就对应内核态,对所有的指令包括特权指令都可以执行. 2.用户态:处于低的 ...
- cocos2d-x 编译 安卓(android)apk文件
摘要: 一.下载Android环境 搭建Android环境需要用到Android SDK.NDK.Ant和JDK: 下载Android SDK 下载Android NDk 下载Android JD ...