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.pswp.cn/news/387160.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/387160.shtml
英文地址,請注明出處:http://en.pswp.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…