0.Spring MVC配置文件中的配置

[java] view plain copy
  1. <!-- 设置使用注解的类所在的jar包,只加载controller类 -->
  2. <span style="white-space:pre">    </span><context:component-scan base-package="com.jay.plat.config.controller" />
[java] view plain copy
  1. <!-- 使用 Swagger Restful API文档时,添加此注解 -->
  2. <mvc:default-servlet-handler />

1.maven依赖

[html] view plain copy
  1. <!-- 构建Restful API -->
  2. <dependency>
  3. <groupId>io.springfox</groupId>
  4. <artifactId>springfox-swagger2</artifactId>
  5. <version>2.4.0</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>io.springfox</groupId>
  9. <artifactId>springfox-swagger-ui</artifactId>
  10. <version>2.4.0</version>
  11. </dependency>

2.Swagger配置文件

[java] view plain copy
  1. package com.jay.plat.config.util;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.ComponentScan;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  6. import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
  7. import springfox.documentation.builders.ApiInfoBuilder;
  8. import springfox.documentation.builders.PathSelectors;
  9. import springfox.documentation.builders.RequestHandlerSelectors;
  10. import springfox.documentation.service.ApiInfo;
  11. import springfox.documentation.spi.DocumentationType;
  12. import springfox.documentation.spring.web.plugins.Docket;
  13. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  14. /*
  15. * Restful API 访问路径:
  16. * http://IP:port/{context-path}/swagger-ui.html
  17. * eg:http://localhost:8080/jd-config-web/swagger-ui.html
  18. */
  19. @EnableWebMvc
  20. @EnableSwagger2
  21. @ComponentScan(basePackages = {"com.<span style="font-family:Arial, Helvetica, sans-serif;">jay.</span>plat.config.controller"})
  22. @Configuration
  23. public class RestApiConfig extends WebMvcConfigurationSupport{
  24. @Bean
  25. public Docket createRestApi() {
  26. return new Docket(DocumentationType.SWAGGER_2)
  27. .apiInfo(apiInfo())
  28. .select()
  29. .apis(RequestHandlerSelectors.basePackage("com.jay.plat.config.controller"))
  30. .paths(PathSelectors.any())
  31. .build();
  32. }
  33. private ApiInfo apiInfo() {
  34. return new ApiInfoBuilder()
  35. .title("Spring 中使用Swagger2构建RESTful APIs")
  36. .termsOfServiceUrl("http://blog.csdn.net/he90227")
  37. .contact("逍遥飞鹤")
  38. .version("1.1")
  39. .build();
  40. }
  41. }

配置说明:

[html] view plain copy
  1. @Configuration 配置注解,自动在本类上下文加载一些环境变量信息
  2. @EnableWebMvc
  3. @EnableSwagger2 使swagger2生效
  4. @ComponentScan("com.myapp.packages") 需要扫描的包路径

3.Controller中使用注解添加API文档

[java] view plain copy
  1. package com.jay.spring.boot.demo10.swagger2.controller;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import org.springframework.web.bind.annotation.PathVariable;
  8. import org.springframework.web.bind.annotation.RequestBody;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RequestMethod;
  11. import org.springframework.web.bind.annotation.RestController;
  12. import com.jay.spring.boot.demo10.swagger2.bean.User;
  13. import io.swagger.annotations.ApiImplicitParam;
  14. import io.swagger.annotations.ApiImplicitParams;
  15. import io.swagger.annotations.ApiOperation;
  16. @RestController
  17. @RequestMapping(value = "/users") // 通过这里配置使下面的映射都在/users下,可去除
  18. public class UserController {
  19. static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long, User>());
  20. @ApiOperation(value = "获取用户列表", notes = "")
  21. @RequestMapping(value = { "" }, method = RequestMethod.GET)
  22. public List<User> getUserList() {
  23. List<User> r = new ArrayList<User>(users.values());
  24. return r;
  25. }
  26. @ApiOperation(value = "创建用户", notes = "根据User对象创建用户")
  27. @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
  28. @RequestMapping(value = "", method = RequestMethod.POST)
  29. public String postUser(@RequestBody User user) {
  30. users.put(user.getId(), user);
  31. return "success";
  32. }
  33. @ApiOperation(value = "获取用户详细信息", notes = "根据url的id来获取用户详细信息")
  34. @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
  35. @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  36. public User getUser(@PathVariable Long id) {
  37. return users.get(id);
  38. }
  39. @ApiOperation(value = "更新用户详细信息", notes = "根据url的id来指定更新对象,并根据传过来的user信息来更新用户详细信息")
  40. @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long"),
  41. @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User") })
  42. @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
  43. public String putUser(@PathVariable Long id, @RequestBody User user) {
  44. User u = users.get(id);
  45. u.setName(user.getName());
  46. u.setAge(user.getAge());
  47. users.put(id, u);
  48. return "success";
  49. }
  50. @ApiOperation(value = "删除用户", notes = "根据url的id来指定删除对象")
  51. @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
  52. @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
  53. public String deleteUser(@PathVariable Long id) {
  54. users.remove(id);
  55. return "success";
  56. }
  57. }

4.效果展示

访问路径:

[java] view plain copy
  1. Restful API 访问路径:
  2. * http://IP:port/{context-path}/swagger-ui.html
  3. * eg:http://localhost:8080/jd-config-web/swagger-ui.html
参考:
http://www.cnblogs.com/yuananyun/p/4993426.html
http://www.jianshu.com/p/8033ef83a8ed

Spring MVC中使用 Swagger2 构建Restful API相关推荐

  1. dubbo2.5-spring4-mybastis3.2-springmvc4-mongodb3.4-redis3(十)之Spring MVC中使用 Swagger2 构建Restful API...

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u010046908/article/details/55047193 1.Swagger2是什么? ...

  2. Spring Boot中使用Swagger2构建RESTful APIs

    关于 Swagger Swagger能成为最受欢迎的REST APIs文档生成工具之一,有以下几个原因: Swagger 可以生成一个具有互动性的API控制台,开发者可以用来快速学习和尝试API. S ...

  3. Spring Boot 中使用 Swagger2 构建强大的 RESTful API 文档

    项目现状:由于前后端分离,没有很好的前后端合作工具. 由于接口众多,并且细节复杂(需要考虑不同的HTTP请求类型.HTTP头部信息.HTTP请求内容等),高质量地创建这份文档本身就是件非常吃力的事,下 ...

  4. Spring Boot中使用Swagger2构建强大的RESTful API文档

    由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这 ...

  5. java Spring Boot中使用Swagger2构建API文档

    1.添加Swagger2的依赖 在pom.xml中加入Swagger2的依赖 <dependency><groupId>io.springfox</groupId> ...

  6. springboot集成swagger2构建RESTful API文档

    在开发过程中,有时候我们需要不停的测试接口,自测,或者交由测试测试接口,我们需要构建一个文档,都是单独写,太麻烦了,现在使用springboot集成swagger2来构建RESTful API文档,可 ...

  7. Spring Boot 集成Swagger2生成RESTful API文档

    Swagger2可以在写代码的同时生成对应的RESTful API文档,方便开发人员参考,另外Swagger2也提供了强大的页面测试功能来调试每个RESTful API. 使用Spring Boot可 ...

  8. 整合swagger2生成Restful Api接口文档

    整合swagger2生成Restful Api接口文档 swagger Restful文档生成工具 2017-9-30 官方地址:https://swagger.io/docs/specificati ...

  9. Spring Boot构建RESTful API与单元测试

    首先,回顾并详细说明一下在快速入门中使用的@Controller.@RestController.@RequestMapping注解.如果您对Spring MVC不熟悉并且还没有尝试过快速入门案例,建 ...

最新文章

  1. 奔图打印机显示未连接_手机连接奔图打印机,无法打印的解决方法
  2. python怎么导入视频-python 给视频添加马赛克
  3. 微信小程序安卓机使用uploadfile提示undefined错误原因
  4. Tableau系列之与R语言结合
  5. log4j2常见配置
  6. Unity 读取资源(图片)
  7. BZOJ[1051]受欢迎的牛
  8. 【TensorFlow】学习资源汇总以及知识总结
  9. 如何使用Puppeteer从任何网站创建自定义API
  10. python中sqrt(4)*sqrt(9)_【单选题】Python表达式sqrt(4)*sqrt(9)的值为
  11. oracle从子表取出前几行数据:
  12. The engine “node“ is incompatible with this module
  13. Kepware欧姆龙驱动连接选型大全
  14. idea导出war包并部署在tomact
  15. CNKI知网论文下载工具
  16. typora 自动添加标题序号
  17. 高德地图添加导航依赖冲突 com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex
  18. 用Dockerfile构建MySQL镜像并实现容器启动过程中MySQL数据库系统的初始化
  19. SparkSteaming使用
  20. html5怎么导出表格,《网页 导出到 excel表格数据》 如何将网页表格导出到excel

热门文章

  1. java timezone id_java.util.TimeZone.setID()方法实例
  2. envs\TensorFlow2.0\lib\site-packages\tensorflow\python\framework\dtypes.py:516: FutureWarning 解决方案
  3. c语言 sqlite_SQLite与C语言
  4. 系统固件升级_固件和操作系统之间的差异
  5. Java——集合(HashMap与Hashtable的区别)
  6. uva 1617——Laptop
  7. 解释性语言和汇编性语言对比
  8. C++ this指针初步使用,与链式编程
  9. python 多人连接mysql 进行事务操作 对mysql加锁与释放锁
  10. QT使用SQLite数据库实现登录功能