springboot + swagger 實(shí)例代碼
swagger用于定義API文檔。
好處:
- 前后端分離開(kāi)發(fā)
- API文檔非常明確
- 測(cè)試的時(shí)候不需要再使用URL輸入瀏覽器的方式來(lái)訪問(wèn)Controller
- 傳統(tǒng)的輸入U(xiǎn)RL的測(cè)試方式對(duì)于post請(qǐng)求的傳參比較麻煩(當(dāng)然,可以使用postman這樣的瀏覽器插件)
- spring-boot與swagger的集成簡(jiǎn)單的一逼
1、項(xiàng)目結(jié)構(gòu)
和上一節(jié)一樣,沒(méi)有改變。
2、pom.xml
引入了兩個(gè)jar。
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.2.2</version> </dependency>
3、Application.java
package com.xxx.firstboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import springfox.documentation.swagger2.annotations.EnableSwagger2; @SpringBootApplication //same as @Configuration+@EnableAutoConfiguration+@ComponentScan @EnableSwagger2 //啟動(dòng)swagger注解 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
說(shuō)明:
引入了一個(gè)注解@EnableSwagger2來(lái)啟動(dòng)swagger注解。(啟動(dòng)該注解使得用在controller中的swagger注解生效,覆蓋的范圍由@ComponentScan的配置來(lái)指定,這里默認(rèn)指定為根路徑"com.xxx.firstboot"下的所有controller)
4、UserController.java
package com.xxx.firstboot.web; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.xxx.firstboot.domain.User; import com.xxx.firstboot.service.UserService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; @RestController @RequestMapping("/user") @Api("userController相關(guān)api") public class UserController { @Autowired private UserService userService; // @Autowired // private MyRedisTemplate myRedisTemplate; @ApiOperation("獲取用戶信息") @ApiImplicitParams({ @ApiImplicitParam(paramType="header",name="username",dataType="String",required=true,value="用戶的姓名",defaultValue="zhaojigang"), @ApiImplicitParam(paramType="query",name="password",dataType="String",required=true,value="用戶的密碼",defaultValue="wangna") }) @ApiResponses({ @ApiResponse(code=400,message="請(qǐng)求參數(shù)沒(méi)填好"), @ApiResponse(code=404,message="請(qǐng)求路徑?jīng)]有或頁(yè)面跳轉(zhuǎn)路徑不對(duì)") }) @RequestMapping(value="/getUser",method=RequestMethod.GET) public User getUser(@RequestHeader("username") String username, @RequestParam("password") String password) { return userService.getUser(username,password); } // @RequestMapping("/testJedisCluster") // public User testJedisCluster(@RequestParam("username") String username){ // String value = myRedisTemplate.get(MyConstants.USER_FORWARD_CACHE_PREFIX, username); // if(StringUtils.isBlank(value)){ // myRedisTemplate.set(MyConstants.USER_FORWARD_CACHE_PREFIX, username, JSON.toJSONString(getUser())); // return null; // } // return JSON.parseObject(value, User.class); // } }
說(shuō)明:
1、@Api:用在類上,說(shuō)明該類的作用
2、@ApiOperation:用在方法上,說(shuō)明方法的作用
3、@ApiImplicitParams:用在方法上包含一組參數(shù)說(shuō)明
4、@ApiImplicitParam:用在@ApiImplicitParams注解中,指定一個(gè)請(qǐng)求參數(shù)的各個(gè)方面
1、paramType:參數(shù)放在哪個(gè)地方 header-->請(qǐng)求參數(shù)的獲?。篅RequestHeader
①query-->請(qǐng)求參數(shù)的獲?。篅RequestParam
② path(用于restful接口)-->請(qǐng)求參數(shù)的獲取:@PathVariable
③body(不常用)
④ form(不常用)
2、name:參數(shù)名
3、dataType:參數(shù)類型
4、required:參數(shù)是否必須傳
5、value:參數(shù)的意思
6、defaultValue:參數(shù)的默認(rèn)值
5、@ApiResponses:用于表示一組響應(yīng)
6、@ApiResponse:用在@ApiResponses中,一般用于表達(dá)一個(gè)錯(cuò)誤的響應(yīng)信息
1、code:數(shù)字,例如400
2、message:信息,例如"請(qǐng)求參數(shù)沒(méi)填好"
3、response:拋出異常的類
7、@ApiModel:描述一個(gè)Model的信息(這種一般用在post創(chuàng)建的時(shí)候,使用@RequestBody這樣的場(chǎng)景,請(qǐng)求參數(shù)無(wú)法使
1、@ApiImplicitParam注解進(jìn)行描述的時(shí)候) @ApiModelProperty:描述一個(gè)model的屬性
以上這些就是最常用的幾個(gè)注解了。
需要注意的是:
ApiImplicitParam這個(gè)注解不只是注解,還會(huì)影響運(yùn)行期的程序,例子如下:
如果ApiImplicitParam中的phone的paramType是query的話,是無(wú)法注入到rest路徑中的,而且如果是path的話,是不需要配置ApiImplicitParam的,即使配置了,其中的value="手機(jī)號(hào)"也不會(huì)在swagger-ui展示出來(lái)。
具體其他的注解,查看:https://github.com/swagger-api/swagger-core/wiki/Annotations#apimodel
測(cè)試:
啟動(dòng)服務(wù),瀏覽器輸入"http://localhost:8080/swagger-ui.html"
最上邊一個(gè)紅框:@Api
GET紅框:method=RequestMethod.GET
右邊紅框:@ApiOperation
parameter紅框:@ApiImplicitParams系列注解
response messages紅框:@ApiResponses系列注解
輸入?yún)?shù)后,點(diǎn)擊"try it out!",查看響應(yīng)內(nèi)容:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot整合Swagger3生成接口文檔過(guò)程解析
- Springboot引入攔截器并放行swagger代碼實(shí)例
- SpringBoot集成Swagger2生成接口文檔的方法示例
- SpringBoot結(jié)合Swagger2自動(dòng)生成api文檔的方法
- 詳解SpringBoot結(jié)合swagger2快速生成簡(jiǎn)單的接口文檔
- SpringBoot集成swagger的實(shí)例代碼
- SpringBoot整合Swagger2實(shí)例方法
- IDEA SpringBoot 項(xiàng)目配置Swagger2的詳細(xì)教程
- Spring Boot中如何使用Swagger詳解
相關(guān)文章
使用Feign設(shè)置Token鑒權(quán)調(diào)用接口
這篇文章主要介紹了使用Feign設(shè)置Token鑒權(quán)調(diào)用接口,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03springboot+vue實(shí)現(xiàn)驗(yàn)證碼功能
這篇文章主要為大家詳細(xì)介紹了springboot+vue實(shí)現(xiàn)驗(yàn)證碼功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08MyBatis攔截器動(dòng)態(tài)替換表名的方法詳解
因?yàn)槲覀兂志脤涌蚣芨嗟厥褂肕yBatis,那我們就借助于MyBatis的攔截器來(lái)完成我們的功能,這篇文章主要給大家介紹了關(guān)于MyBatis攔截器動(dòng)態(tài)替換表名的相關(guān)資料,需要的朋友可以參考下2022-04-04java中l(wèi)ong數(shù)據(jù)類型轉(zhuǎn)換為int類型
這篇文章主要講解Java中基本數(shù)據(jù)類型,java long 類型與其java int類型的轉(zhuǎn)換的幾種方法,希望能給大家做一個(gè)參考2016-07-07關(guān)于struts2中Action名字的大小寫(xiě)問(wèn)題淺談
這篇文章主要給大家介紹了關(guān)于struts2中Action名字大小寫(xiě)問(wèn)題的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。2017-06-06Java處理InterruptedException異常的理論與實(shí)踐
在使用Java的過(guò)程中,有個(gè)情景或許很多人見(jiàn)過(guò),您在編寫(xiě)一個(gè)測(cè)試程序,程序需要暫停一段時(shí)間,于是調(diào)用 Thread.sleep()。但是編譯器或 IDE 報(bào)錯(cuò)說(shuō)沒(méi)有處理檢查到的 InterruptedException。InterruptedException 是什么呢,為什么必須處理它?下面跟著小編一起來(lái)看看。2016-08-08Intellij IDEA命令行執(zhí)行java無(wú)法加載主類解決方案
這篇文章主要介紹了Intellij IDEA命令行執(zhí)行java無(wú)法加載主類解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09