欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot + swagger 實例代碼

 更新時間:2017年05月08日 11:28:45   作者:趙計剛  
本篇文章主要介紹了springboot + swagger 實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

swagger用于定義API文檔。

好處:

  1. 前后端分離開發(fā)
  2. API文檔非常明確
  3. 測試的時候不需要再使用URL輸入瀏覽器的方式來訪問Controller
  4. 傳統(tǒng)的輸入URL的測試方式對于post請求的傳參比較麻煩(當然,可以使用postman這樣的瀏覽器插件)
  5. spring-boot與swagger的集成簡單的一逼

1、項目結構

和上一節(jié)一樣,沒有改變。

2、pom.xml

引入了兩個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       //啟動swagger注解
public class Application {

  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }

}

說明:

引入了一個注解@EnableSwagger2來啟動swagger注解。(啟動該注解使得用在controller中的swagger注解生效,覆蓋的范圍由@ComponentScan的配置來指定,這里默認指定為根路徑"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相關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="請求參數(shù)沒填好"),
    @ApiResponse(code=404,message="請求路徑沒有或頁面跳轉路徑不對")
  })
  @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);
//  }
  
}

說明:
1、@Api:用在類上,說明該類的作用

2、@ApiOperation:用在方法上,說明方法的作用

3、@ApiImplicitParams:用在方法上包含一組參數(shù)說明

4、@ApiImplicitParam:用在@ApiImplicitParams注解中,指定一個請求參數(shù)的各個方面

   1、paramType:參數(shù)放在哪個地方 header-->請求參數(shù)的獲?。篅RequestHeader

      ①query-->請求參數(shù)的獲?。篅RequestParam

      ② path(用于restful接口)-->請求參數(shù)的獲?。篅PathVariable

      ③body(不常用)

      ④ form(不常用)

   2、name:參數(shù)名

   3、dataType:參數(shù)類型

   4、required:參數(shù)是否必須傳

   5、value:參數(shù)的意思

   6、defaultValue:參數(shù)的默認值

5、@ApiResponses:用于表示一組響應

6、@ApiResponse:用在@ApiResponses中,一般用于表達一個錯誤的響應信息

   1、code:數(shù)字,例如400

   2、message:信息,例如"請求參數(shù)沒填好"

   3、response:拋出異常的類

7、@ApiModel:描述一個Model的信息(這種一般用在post創(chuàng)建的時候,使用@RequestBody這樣的場景,請求參數(shù)無法使    

    1、@ApiImplicitParam注解進行描述的時候) @ApiModelProperty:描述一個model的屬性

以上這些就是最常用的幾個注解了。

需要注意的是:

ApiImplicitParam這個注解不只是注解,還會影響運行期的程序,例子如下:

  

如果ApiImplicitParam中的phone的paramType是query的話,是無法注入到rest路徑中的,而且如果是path的話,是不需要配置ApiImplicitParam的,即使配置了,其中的value="手機號"也不會在swagger-ui展示出來。

具體其他的注解,查看:https://github.com/swagger-api/swagger-core/wiki/Annotations#apimodel

 測試:

啟動服務,瀏覽器輸入"http://localhost:8080/swagger-ui.html"

 

最上邊一個紅框:@Api

GET紅框:method=RequestMethod.GET

右邊紅框:@ApiOperation

parameter紅框:@ApiImplicitParams系列注解

response messages紅框:@ApiResponses系列注解

輸入?yún)?shù)后,點擊"try it out!",查看響應內容:

 

 以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 使用Feign設置Token鑒權調用接口

    使用Feign設置Token鑒權調用接口

    這篇文章主要介紹了使用Feign設置Token鑒權調用接口,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • java高級排序之希爾排序

    java高級排序之希爾排序

    這篇文章主要介紹了java高級排序之希爾排序 ,需要的朋友可以參考下
    2015-04-04
  • springboot+vue實現(xiàn)驗證碼功能

    springboot+vue實現(xiàn)驗證碼功能

    這篇文章主要為大家詳細介紹了springboot+vue實現(xiàn)驗證碼功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • MyBatis攔截器動態(tài)替換表名的方法詳解

    MyBatis攔截器動態(tài)替換表名的方法詳解

    因為我們持久層框架更多地使用MyBatis,那我們就借助于MyBatis的攔截器來完成我們的功能,這篇文章主要給大家介紹了關于MyBatis攔截器動態(tài)替換表名的相關資料,需要的朋友可以參考下
    2022-04-04
  • java中l(wèi)ong數(shù)據(jù)類型轉換為int類型

    java中l(wèi)ong數(shù)據(jù)類型轉換為int類型

    這篇文章主要講解Java中基本數(shù)據(jù)類型,java long 類型與其java int類型的轉換的幾種方法,希望能給大家做一個參考
    2016-07-07
  • Java線程池execute()方法源碼全面解析

    Java線程池execute()方法源碼全面解析

    這篇文章主要介紹了Java線程池execute()方法源碼全面解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 關于struts2中Action名字的大小寫問題淺談

    關于struts2中Action名字的大小寫問題淺談

    這篇文章主要給大家介紹了關于struts2中Action名字大小寫問題的相關資料,文中介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面跟著小編一起來學習學習吧。
    2017-06-06
  • Java處理InterruptedException異常的理論與實踐

    Java處理InterruptedException異常的理論與實踐

    在使用Java的過程中,有個情景或許很多人見過,您在編寫一個測試程序,程序需要暫停一段時間,于是調用 Thread.sleep()。但是編譯器或 IDE 報錯說沒有處理檢查到的 InterruptedException。InterruptedException 是什么呢,為什么必須處理它?下面跟著小編一起來看看。
    2016-08-08
  • java實現(xiàn)本地緩存的示例代碼

    java實現(xiàn)本地緩存的示例代碼

    在高性能服務架構設計中,緩存是不可或缺的環(huán)節(jié),因此這篇文章主要為大家詳細介紹了java中如何實現(xiàn)本地緩存,感興趣的小伙伴可以了解一下
    2024-01-01
  • Intellij IDEA命令行執(zhí)行java無法加載主類解決方案

    Intellij IDEA命令行執(zhí)行java無法加載主類解決方案

    這篇文章主要介紹了Intellij IDEA命令行執(zhí)行java無法加載主類解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-09-09

最新評論