Spring MVC中使用 Swagger2 构建Restful API
1.Spring MVC配置文件中的配置
- <!-- 设置使用注解的类所在的jar包,只加载controller类 -->
- <context:component-scan base-package="com.jay.plat.config.controller" />

- <mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>
- <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>
2.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>
3.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.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") 需要扫描的包路径

4.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";
- }
- }


5.效果展示
- 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的更多相关文章
- Spring Boot中使用Swagger2构建RESTful API文档
在开发rest api的时候,为了减少与其他团队平时开发期间的频繁沟通成本,传统做法我们会创建一份RESTful API文档来记录所有接口细节,然而这样的做法有以下几个问题: 1.由于接口众多,并且细 ...
- Spring Boot中使用Swagger2生成RESTful API文档(转)
效果如下图所示: 添加Swagger2依赖 在pom.xml中加入Swagger2的依赖 <!-- https://mvnrepository.com/artifact/io.springfox ...
- Spring Boot中使用Swagger2构建RESTful APIs
关于 Swagger Swagger能成为最受欢迎的REST APIs文档生成工具之一,有以下几个原因: Swagger 可以生成一个具有互动性的API控制台,开发者可以用来快速学习和尝试API. S ...
- Spring Boot中使用Swagger2构建RESTful APIs介绍
1.添加相关依赖 <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <depen ...
- Spring MVC 中使用 Swagger2 构建动态 RESTful API
当多终端(WEB/移动端)需要公用业务逻辑时,一般会构建 RESTful 风格的服务提供给多终端使用. 为了减少与对应终端开发团队频繁沟通成本,刚开始我们会创建一份 RESTful API 文档来记录 ...
- Spring Boot (21) 使用Swagger2构建restful API
使用swagger可以与spring mvc程序配合组织出强大的restful api文档.它既可以减少我们创建文档的工作量,同时说明内容又整合入现实代码中,让维护文档和修改代码整合为一体,可以让我们 ...
- Spring Boot中使用Swagger2构建强大的RESTful API文档
由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这 ...
- Spring Boot 中使用 Swagger2 构建强大的 RESTful API 文档
项目现状:由于前后端分离,没有很好的前后端合作工具. 由于接口众多,并且细节复杂(需要考虑不同的HTTP请求类型.HTTP头部信息.HTTP请求内容等),高质量地创建这份文档本身就是件非常吃力的事,下 ...
- Spring Boot 入门系列(二十二)使用Swagger2构建 RESTful API文档
前面介绍了如何Spring Boot 快速打造Restful API 接口,也介绍了如何优雅的实现 Api 版本控制,不清楚的可以看我之前的文章:https://www.cnblogs.com/zha ...
随机推荐
- DEV GridControl.TableView FocusedRow选中行背景颜色
上次修改了TableView.RowStyle,导致了一个问题:覆盖了GridControl默认的选中行颜色. 于是需要重写选中行的颜色. 刚开始的想法是: <dxg:TableView> ...
- JfreeChart的使用
前提:导入需要的2个jar文件,jcommon-版本号.jar,jfreechart-版本号.jar.可以去官网下载:http://sourceforge.net/projects/jfreechar ...
- java中重载、覆盖和隐藏三者的区别分析
重载:方法名相同,但参数不同的多个同名函数 注意:1.参数不同的意思是参数类型.参数个数.参数顺序至少有一个不同 2.返回值和异常以及访问修饰符,不能作为重载的条件(因为对于匿名调用,会出现歧义,eg ...
- 回顾CSS布局易混淆的概念
一.浮动模型 元素默认是static的,不能浮动,但可以用CSS样式设置为浮动 浮动模型只有两个值 float:left 和 float:right ,可以让块状元素同行显示 二.层模型 top/bo ...
- mybitis学习的页面
http://mybatis.github.io/mybatis-3/zh/configuration.html
- F2工作流引擎之 工作流运转模型(三)
1流程单起点单终止模型 单起点:一个流程定义必须有且唯一起点 单结束点:一个流程定义必须有且唯一结束点. 约定:提单与结束是每个流程必须有的活动,且唯一只有一个提单和结束. 2串行模型 描述:串行(S ...
- PR和VV的分类与区别
Adobe Premiere是一款常用的视频编辑软件,由Adobe公司推出.现在常用的有CS4.CS5.CS6.CC.CC 2014及CC 2015版本.是一款编辑画面质量比较好的软件,有较好的兼容性 ...
- 2D 《触不可及》
这是一个有着特殊意义的夜晚(2014.9.19),孙红雷的电影这场电影<触不可及>也有着不一般的涵义,于我,在万达商场,遂留此一笔,仅供回忆.
- C++编程优化心得(持续更新)
1. 对齐原则.比如64位总线,每次寻址读取8B.编程时注意变量地址,尽量消耗总线最少的寻址次数.堆内存申请时,系统严格按照对齐原则分配,故而使用时候也尽量不要跨寻址边界. 2. 需要的时候,可为了效 ...
- Rhel6-cacti+nagios+ganglia(nginx)配置文档
(lnmp平台) 系统环境: rhel6 x86_64 iptables and selinux disabled 主机: 192.168.122.185 server85.example.com 1 ...