Spring MVC中使用 Swagger2 构建Restful API

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.           
  3.         <dependency>  
  4.             <groupId>io.springfox</groupId>  
  5.             <artifactId>springfox-swagger2</artifactId>  
  6.             <version>2.4.0</version>  
  7.         </dependency>  
  8.         <dependency>  
  9.             <groupId>io.springfox</groupId>  
  10.             <artifactId>springfox-swagger-ui</artifactId>  
  11.             <version>2.4.0</version>  
  12.         </dependency>  


2.Swagger配置文件

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


配置说明:

            

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


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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/387160.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Go语言规范汇总

目录 统一规范篇合理规划目录GOPATH设置import 规范代码风格大小约定命名篇基本命令规范项目目录名包名文件名常量变量变量申明变量命名惯例全局变量名局部变量名循环变量结构体(struct)接口名函数和方法名参数名返回值开发篇包魔鬼数字常量 & 枚举结构体运算符函数参数返回…

3.14 01串排序

将01串首先按照长度排序&#xff0c;其次按1的个数的多少排序&#xff0c;最后按ASCII码排序。 输入样例&#xff1a; 10011111 00001101 10110101 1 0 1100 输出样例&#xff1a; 0 1 1100 1010101 00001101 10011111 #include<fstream> #include<iost…

platform(win32) 错误

运行cnpm install后&#xff0c;出现虽然提示不适合Windows&#xff0c;但是问题好像是sass loader出问题的。所以只要执行下面命令即可&#xff1b;方案一&#xff1a;cnpm rebuild node-sass #不放心可以重新安装下 cnpm install方案二&#xff1a;npm update npm install no…

Error: Program type already present: okhttp3.Authenticator$1

在app中的build.gradle中加入如下代码&#xff0c; configurations {all*.exclude group: com.google.code.gsonall*.exclude group: com.squareup.okhttp3all*.exclude group: com.squareup.okioall*.exclude group: com.android.support,module:support-v13 } 如图 转载于:ht…

3.15 排列对称串

筛选出对称字符串&#xff0c;然后将其排序。 输入样例&#xff1a; 123321 123454321 123 321 sdfsdfd 121212 \\dd\\ 输出样例 123321 \\dd\\ 123454321 #include<fstream> #include<iostream> #include<string> #include<set> using …

ES6规范 ESLint

在团队的项目开发过程中&#xff0c;代码维护所占的时间比重往往大于新功能的开发。因此编写符合团队编码规范的代码是至关重要的&#xff0c;这样做不仅可以很大程度地避免基本语法错误&#xff0c;也保证了代码的可读性&#xff0c;毕竟&#xff1a;程序是写给人读的&#xf…

前端 HTML 常用标签 head标签相关内容 script标签

script标签 定义JavaScript代码 <!--定义JavaScript代码--> <script type"text/javascript"></script> 引入JavaScript文件 src""引入的 js文件路径 <!-- 引入JavaScript文件 --> <script src"./index.js"></s…

3.16 按绩点排名

成绩60分及以上的课程才予以计算绩点 绩点计算公式&#xff1a;[(课程成绩-50) / 10 ] * 学分 学生总绩点为所有绩点之和除以10 输入格式&#xff1a; 班级数 课程数 各个课程的学分 班级人数 姓名 各科成绩 输出格式&#xff1a; class 班级号: 姓名&#xff08;占1…

iview日期控件,双向绑定日期格式

日期在双向绑定之后格式为&#xff1a;2017-07-03T16:00:00.000Z 想要的格式为2017-07-04调了好久&#xff0c;几乎一天&#xff1a;用一句话搞定了 on-change”addForm.Birthday$event”<Date-picker placeholder"选择日期" type"datetime" v-model&…

移除html,jsp中的元素

移除html&#xff0c;jsp中的元素 某些时候&#xff0c;需要移除某个元素&#xff0c;比如移除表中的某一行 $("#tbody").children().eq(i).remove();或者 $("#tr").remove();PS&#xff1a;获取表中的tr的数量&#xff1a; $("#tbody").childre…

ACM001 Quicksum

本题的重点在于数据的读入。 可采用cin.getlin()一行一行读入数据&#xff1b;也可采用cin.get()一个一个读入字符。 cin会忽略回车、空格、Tab跳格。 cin.get()一个一个字符读&#xff0c;不忽略任何字符。 cin.getline()一行一行读入。 #include<fstream> #include…

[Swift]LeetCode884. 两句话中的不常见单词 | Uncommon Words from Two Sentences

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号&#xff1a;山青咏芝&#xff08;shanqingyongzhi&#xff09;➤博客园地址&#xff1a;山青咏芝&#xff08;https://www.cnblogs.com/strengthen/&#xff09;➤GitHub地址&a…

微信公众号 语音录音jssdk

1.开发流程 如果开发的是普通的展示性页面&#xff0c;就和开发普通的页面没有区别&#xff0c;不过这里要用到设备&#xff08;手机&#xff09;的录音功能&#xff0c;就需要调用微信app的录音接口&#xff0c;需要使用微信jssdk。 使用微信jssdk&#xff1a;微信JS-SDK说明文…

iview table 方法若干

新增默认选中1. _checked字段增加2. 给data项设置特殊 key _checked: true2.0 多选框样式错乱&#xff0c;默认选中问题1. 修改为元素checkbox 样式大概调整2. 如果样式不好看 可以自行修改或者使用其他组件ui checkboxAPI props 属性说明类型items显示的结构化数据Arraycolumn…

05 MapReduce应用案例01

1、单词计数 在一定程度上反映了MapReduce设计的初衷--对日志文件进行分析。 public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{//该方法循环调用&#xff0c;从文件的split中读取每行调用一次&#xff0c;把该行所在的下标为key&a…

ios高级开发之多线程(一)

1.概念&#xff1a; 多线程&#xff08;multithreading&#xff09;到底是什么呢&#xff0c;它是指在软件或者硬件上实现多个线程并发执行的技术。具有多线程能力的计算机因有硬件的支持&#xff0c;而能够在同一时间执行多个线程&#xff0c;进而提升整体处理性能。在一个程序…

v-if的简单应用

<span v-if"item.status0"> 项目状态&#xff1a;未提交 </span> <span v-if"item.status1"> 项目状态&#xff1a;审批中 </span> <span v-if"item.status2"> 项目状态&#xff1a;审批退回 </span> <s…

05 MapReduce应用案例02

6、統計每個月份中&#xff0c;最高的三個溫度。 輸入格式&#xff1a;年月日 空格 時分秒 TAB 溫度 inputfile: 1949-10-01 14:21:02 34c 1949-10-02 14:01:02 36c 1950-01-01 11:21:02 32c 1950-10-01 12:21:02 37c 1951-12-01 12:21:02 23c 1950-10-…

05 MapReduce应用案例03

8、PageRank Page-rank源于Google&#xff0c;用于衡量特定网页相对于搜索引擎索引中的其他网页而言的重要程度。 Page-rank实现了将链接价值概念作为排名因素。 算法原理 – 入链 投票 • Page-rank 让链接来“ 投票 “ ,到一个页面的超链接相当于对该页投一票。 – 入…

利用微信的weui框架上传、预览和删除图片

jQuery WeUI 是专为微信公众账号开发而设计的一个框架&#xff0c;jQuery WeUI的官网&#xff1a;http://jqweui.com/ 需求&#xff1a;需要在微信公众号网页添加上传图片功能 技术选型&#xff1a;实现上传图片功能可选百度的WebUploader、饿了么的Element和微信的jQuery WeUI…