如何在springboot項目中使用swagger

這篇文章將為大家詳細(xì)講解有關(guān)如何在springboot項目中使用 swagger,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

在洛南等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站制作、網(wǎng)站設(shè)計 網(wǎng)站設(shè)計制作按需求定制制作,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),營銷型網(wǎng)站,外貿(mào)網(wǎng)站建設(shè),洛南網(wǎng)站建設(shè)費用合理。

1、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>

2、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的配置來指定,這里默認(rèn)指定為根路徑"com.xxx.firstboot"下的所有controller)

3、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="請求參數(shù)沒填好"),
    @ApiResponse(code=404,message="請求路徑?jīng)]有或頁面跳轉(zhuǎn)路徑不對")
  })
  @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ù)的默認(rèn)值

5、@ApiResponses:用于表示一組響應(yīng)

6、@ApiResponse:用在@ApiResponses中,一般用于表達(dá)一個錯誤的響應(yīng)信息

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

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

   3、response:拋出異常的類

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

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

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

需要注意的是:

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

如何在springboot項目中使用 swagger

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

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

 測試:

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

 如何在springboot項目中使用 swagger

最上邊一個紅框:@Api

GET紅框:method=RequestMethod.GET

右邊紅框:@ApiOperation

parameter紅框:@ApiImplicitParams系列注解

response messages紅框:@ApiResponses系列注解

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

 如何在springboot項目中使用 swagger

關(guān)于如何在springboot項目中使用 swagger就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

名稱欄目:如何在springboot項目中使用swagger
轉(zhuǎn)載注明:http://bm7419.com/article28/ipojjp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、網(wǎng)站設(shè)計網(wǎng)站導(dǎo)航、Google、做網(wǎng)站、營銷型網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)