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

[java] view plain copy

  1. <!-- 设置使用注解的类所在的jar包,只加载controller类 -->
  2. <context:component-scan base-package="com.jay.plat.config.controller" />
[java] view plain copy

  1. <!-- 使用 Swagger Restful API文档时,添加此注解 -->
  2. <mvc:default-servlet-handler />
  1. <mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>
  2. <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>

2.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>

3.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.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") 需要扫描的包路径

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

5.效果展示

访问路径:
[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


参考:

Spring MVC中使用 Swagger2 构建Restful API的更多相关文章

  1. Spring Boot中使用Swagger2构建RESTful API文档

    在开发rest api的时候,为了减少与其他团队平时开发期间的频繁沟通成本,传统做法我们会创建一份RESTful API文档来记录所有接口细节,然而这样的做法有以下几个问题: 1.由于接口众多,并且细 ...

  2. Spring Boot中使用Swagger2生成RESTful API文档(转)

    效果如下图所示: 添加Swagger2依赖 在pom.xml中加入Swagger2的依赖 <!-- https://mvnrepository.com/artifact/io.springfox ...

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

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

  4. Spring Boot中使用Swagger2构建RESTful APIs介绍

    1.添加相关依赖 <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <depen ...

  5. Spring MVC 中使用 Swagger2 构建动态 RESTful API

    当多终端(WEB/移动端)需要公用业务逻辑时,一般会构建 RESTful 风格的服务提供给多终端使用. 为了减少与对应终端开发团队频繁沟通成本,刚开始我们会创建一份 RESTful API 文档来记录 ...

  6. Spring Boot (21) 使用Swagger2构建restful API

    使用swagger可以与spring mvc程序配合组织出强大的restful api文档.它既可以减少我们创建文档的工作量,同时说明内容又整合入现实代码中,让维护文档和修改代码整合为一体,可以让我们 ...

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

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

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

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

  9. Spring Boot 入门系列(二十二)使用Swagger2构建 RESTful API文档

    前面介绍了如何Spring Boot 快速打造Restful API 接口,也介绍了如何优雅的实现 Api 版本控制,不清楚的可以看我之前的文章:https://www.cnblogs.com/zha ...

随机推荐

  1. SPSS常用基础操作(1)——变量分组

    有时我们需要对数据资料按照某个规则进行归组,如 在上述资料中,想按照年龄进行分组,30岁以下为组1,30-40岁为组2,40岁以上为组3 有两种方法可以实现: 1.使用计算变量功能 <1> ...

  2. MVC4中的Display Mode简介

    本文地址:http://www.cnblogs.com/egger/p/3400076.html  欢迎转载 ,请保留此链接๑•́ ₃•̀๑! 今天学习MVC4时,看到一个不错的特性"vie ...

  3. Objective-C( block的使用)

    block block用来保存一段代码 block的标志:^ block跟函数很像:可以保存代码.有返回值.有形参.调用方式跟调用方法一样 block内部可以访问外面的变量 默认情况下,block内部 ...

  4. WCF初探-18:WCF数据协定之KnownType

    KnownTypeAttribute 类概述 在数据到达接收终结点时,WCF 运行库尝试将数据反序列化为公共语言运行库 (CLR) 类型的实例.通过首先检查传入消息选择为反序列化而实例化的类型,以确定 ...

  5. OC语言description方法和sel

    OC语言description方法和sel 一.description方法 Description方法包括类方法和对象方法.(NSObject类所包含) (一)基本知识 -description(对象 ...

  6. stage simulator

    ---恢复内容开始--- 运行自带地图 rosrun stage_ros stageros /opt/ros/indigo/share/stage_ros/world/willow-erratic.w ...

  7. STC12C5A60S2 常用的中断源和相关寄存器

    1) 中断源 STC12C5A60S2共有十个中断源,每个中断源可设置4类优先级:当相同优先级下各中断优先级由高到低依次如下: 1.1)INT0(外部中断0) 中断向量地址 0003H, C语言编程: ...

  8. HDU 4746 莫比乌斯反演+离线查询+树状数组

    题目大意: 一个数字组成一堆素因子的乘积,如果一个数字的素因子个数(同样的素因子也要多次计数)小于等于P,那么就称这个数是P的幸运数 多次询问1<=x<=n,1<=y<=m,P ...

  9. Linux学习 : 裸板调试 之 使用MMU

    MMU(Memory Management Unit,内存管理单元),操作系统通过使用处理器的MMU功能实现以下:1)虚拟内存.有了虚拟内存,可以在处理器上运行比实际物理内存大的应用程序.为了使用虚拟 ...

  10. UVA 208 (DFS)

    题意:找出1到T的所有路径: 坑点:一开始以为是到终点,读错了题意,没测试第二个样例,结果WA了4遍,坑大了: #include <iostream> #include <cmath ...