Spring MVC中使用 Swagger2 构建Restful API
0.Spring MVC配置文件中的配置
- <!-- 设置使用注解的类所在的jar包,只加载controller类 -->
- <span style="white-space:pre"> </span><context:component-scan base-package="com.jay.plat.config.controller" />
- <!-- 使用 Swagger Restful API文档时,添加此注解 -->
- <mvc:default-servlet-handler />
1.maven依赖
- <!-- 构建Restful API -->
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger2</artifactId>
- <version>2.4.0</version>
- </dependency>
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger-ui</artifactId>
- <version>2.4.0</version>
- </dependency>
2.Swagger配置文件
- package com.jay.plat.config.util;
- 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 org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
- 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;
- /*
- * Restful API 访问路径:
- * http://IP:port/{context-path}/swagger-ui.html
- * eg:http://localhost:8080/jd-config-web/swagger-ui.html
- */
- @EnableWebMvc
- @EnableSwagger2
- @ComponentScan(basePackages = {"com.<span style="font-family:Arial, Helvetica, sans-serif;">jay.</span>plat.config.controller"})
- @Configuration
- public class RestApiConfig extends WebMvcConfigurationSupport{
- @Bean
- public Docket createRestApi() {
- return new Docket(DocumentationType.SWAGGER_2)
- .apiInfo(apiInfo())
- .select()
- .apis(RequestHandlerSelectors.basePackage("com.jay.plat.config.controller"))
- .paths(PathSelectors.any())
- .build();
- }
- private ApiInfo apiInfo() {
- return new ApiInfoBuilder()
- .title("Spring 中使用Swagger2构建RESTful APIs")
- .termsOfServiceUrl("http://blog.csdn.net/he90227")
- .contact("逍遥飞鹤")
- .version("1.1")
- .build();
- }
- }
配置说明:
- @Configuration 配置注解,自动在本类上下文加载一些环境变量信息
- @EnableWebMvc
- @EnableSwagger2 使swagger2生效
- @ComponentScan("com.myapp.packages") 需要扫描的包路径
3.Controller中使用注解添加API文档
- package com.jay.spring.boot.demo10.swagger2.controller;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RestController;
- import com.jay.spring.boot.demo10.swagger2.bean.User;
- import io.swagger.annotations.ApiImplicitParam;
- import io.swagger.annotations.ApiImplicitParams;
- import io.swagger.annotations.ApiOperation;
- @RestController
- @RequestMapping(value = "/users") // 通过这里配置使下面的映射都在/users下,可去除
- public class UserController {
- static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long, User>());
- @ApiOperation(value = "获取用户列表", notes = "")
- @RequestMapping(value = { "" }, method = RequestMethod.GET)
- public List<User> getUserList() {
- List<User> r = new ArrayList<User>(users.values());
- return r;
- }
- @ApiOperation(value = "创建用户", notes = "根据User对象创建用户")
- @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
- @RequestMapping(value = "", method = RequestMethod.POST)
- public String postUser(@RequestBody User user) {
- users.put(user.getId(), user);
- return "success";
- }
- @ApiOperation(value = "获取用户详细信息", notes = "根据url的id来获取用户详细信息")
- @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
- @RequestMapping(value = "/{id}", method = RequestMethod.GET)
- public User getUser(@PathVariable Long id) {
- return users.get(id);
- }
- @ApiOperation(value = "更新用户详细信息", notes = "根据url的id来指定更新对象,并根据传过来的user信息来更新用户详细信息")
- @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long"),
- @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User") })
- @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
- public String putUser(@PathVariable Long id, @RequestBody User user) {
- User u = users.get(id);
- u.setName(user.getName());
- u.setAge(user.getAge());
- users.put(id, u);
- return "success";
- }
- @ApiOperation(value = "删除用户", notes = "根据url的id来指定删除对象")
- @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
- @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
- public String deleteUser(@PathVariable Long id) {
- users.remove(id);
- return "success";
- }
- }
4.效果展示
- Restful API 访问路径:
- * http://IP:port/{context-path}/swagger-ui.html
- * eg:http://localhost:8080/jd-config-web/swagger-ui.html
Spring MVC中使用 Swagger2 构建Restful API相关推荐
- dubbo2.5-spring4-mybastis3.2-springmvc4-mongodb3.4-redis3(十)之Spring MVC中使用 Swagger2 构建Restful API...
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u010046908/article/details/55047193 1.Swagger2是什么? ...
- Spring Boot中使用Swagger2构建RESTful APIs
关于 Swagger Swagger能成为最受欢迎的REST APIs文档生成工具之一,有以下几个原因: Swagger 可以生成一个具有互动性的API控制台,开发者可以用来快速学习和尝试API. S ...
- Spring Boot 中使用 Swagger2 构建强大的 RESTful API 文档
项目现状:由于前后端分离,没有很好的前后端合作工具. 由于接口众多,并且细节复杂(需要考虑不同的HTTP请求类型.HTTP头部信息.HTTP请求内容等),高质量地创建这份文档本身就是件非常吃力的事,下 ...
- Spring Boot中使用Swagger2构建强大的RESTful API文档
由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这 ...
- java Spring Boot中使用Swagger2构建API文档
1.添加Swagger2的依赖 在pom.xml中加入Swagger2的依赖 <dependency><groupId>io.springfox</groupId> ...
- springboot集成swagger2构建RESTful API文档
在开发过程中,有时候我们需要不停的测试接口,自测,或者交由测试测试接口,我们需要构建一个文档,都是单独写,太麻烦了,现在使用springboot集成swagger2来构建RESTful API文档,可 ...
- Spring Boot 集成Swagger2生成RESTful API文档
Swagger2可以在写代码的同时生成对应的RESTful API文档,方便开发人员参考,另外Swagger2也提供了强大的页面测试功能来调试每个RESTful API. 使用Spring Boot可 ...
- 整合swagger2生成Restful Api接口文档
整合swagger2生成Restful Api接口文档 swagger Restful文档生成工具 2017-9-30 官方地址:https://swagger.io/docs/specificati ...
- Spring Boot构建RESTful API与单元测试
首先,回顾并详细说明一下在快速入门中使用的@Controller.@RestController.@RequestMapping注解.如果您对Spring MVC不熟悉并且还没有尝试过快速入门案例,建 ...
最新文章
- 奔图打印机显示未连接_手机连接奔图打印机,无法打印的解决方法
- python怎么导入视频-python 给视频添加马赛克
- 微信小程序安卓机使用uploadfile提示undefined错误原因
- Tableau系列之与R语言结合
- log4j2常见配置
- Unity 读取资源(图片)
- BZOJ[1051]受欢迎的牛
- 【TensorFlow】学习资源汇总以及知识总结
- 如何使用Puppeteer从任何网站创建自定义API
- python中sqrt(4)*sqrt(9)_【单选题】Python表达式sqrt(4)*sqrt(9)的值为
- oracle从子表取出前几行数据:
- The engine “node“ is incompatible with this module
- Kepware欧姆龙驱动连接选型大全
- idea导出war包并部署在tomact
- CNKI知网论文下载工具
- typora 自动添加标题序号
- 高德地图添加导航依赖冲突 com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex
- 用Dockerfile构建MySQL镜像并实现容器启动过程中MySQL数据库系统的初始化
- SparkSteaming使用
- html5怎么导出表格,《网页 导出到 excel表格数据》 如何将网页表格导出到excel
热门文章
- java timezone id_java.util.TimeZone.setID()方法实例
- envs\TensorFlow2.0\lib\site-packages\tensorflow\python\framework\dtypes.py:516: FutureWarning 解决方案
- c语言 sqlite_SQLite与C语言
- 系统固件升级_固件和操作系统之间的差异
- Java——集合(HashMap与Hashtable的区别)
- uva 1617——Laptop
- 解释性语言和汇编性语言对比
- C++ this指针初步使用,与链式编程
- python 多人连接mysql 进行事务操作 对mysql加锁与释放锁
- QT使用SQLite数据库实现登录功能