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

spring boot整合Swagger2的示例代碼

 更新時間:2017年04月27日 16:07:39   作者:牛頭人  
Swagger 是一個規(guī)范和完整的框架,用于生成、描述、調(diào)用和可視化RESTful風(fēng)格的 Web 服務(wù)。這篇文章主要介紹了spring boot整合Swagger2,需要的朋友可以參考下

Swagger 是一個規(guī)范和完整的框架,用于生成、描述、調(diào)用和可視化RESTful風(fēng)格的 Web 服務(wù)??傮w目標(biāo)是使客戶端和文件系統(tǒng)作為服務(wù)器以同樣的速度來更新。文件的方法,參數(shù)和模型緊密集成到服務(wù)器端的代碼,允許API來始終保持同步。Swagger 讓部署管理和使用功能強(qiáng)大的API從未如此簡單。

1.代碼示例

1).在pom.xml文件中引入Swagger2

 <dependency> 
   <groupId>io.springfox</groupId> 
   <artifactId>springfox-swagger2</artifactId> 
   <version>2.6.1</version>
  </dependency> 
  <dependency> 
   <groupId>io.springfox</groupId> 
   <artifactId>springfox-swagger-ui</artifactId> 
   <version>2.6.1</version>
  </dependency>

2).在Application同級目錄下添加Swagger2的配置類

package com.example;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class Swagger2Config {
 @Bean
 public Docket createRestApi() {
  return new Docket(DocumentationType.SWAGGER_2)
    .apiInfo(apiInfo())
    .select()
    .apis(RequestHandlerSelectors.basePackage("com.example"))
    .paths(PathSelectors.any())
    .build();
 }
 private ApiInfo apiInfo() {
  return new ApiInfoBuilder()
    .title("Spring Boot中使用Swagger2構(gòu)建RESTful APIs")
    .description("spring boot整合swagger2")
    .termsOfServiceUrl("www.baidu.com")
    .contact("牛頭人")
    .version("1.0")
    .build();
 }
}

如上代碼所示,通過 @Configuration 注解,讓Spring來加載該類配置。再通過 @EnableSwagger2 注解來啟用Swagger2。

通過 createRestApi 函數(shù)創(chuàng)建 Docket 的Bean之后, apiInfo() 用來創(chuàng)建該Api的基本信息(這些基本信息會展現(xiàn)在文檔頁面中)。 select() 函數(shù)返回一個 ApiSelectorBuilder 實例用來控制哪些接口給Swagger來展現(xiàn),本例采用指定掃描的包路徑來定義,Swagger會掃描該包下所有Controller定義的API,并產(chǎn)生文檔內(nèi)容(除了被 @ApiIgnore 指定的請求)。

3).新建User實體類

package com.example.swagger2;
public class User {
 private String id;
 private String name;
 public String getId() {
  return id;
 }
 public void setId(String id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
}

4).新建SwaggerDemoController類

package com.example.swagger2;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
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(value="/api") 
@Api("SwaggerDemoController相關(guān)api")
public class SwaggerDemoController {
 static Map<String, User> users = Collections.synchronizedMap(new HashMap<String, User>());
 @ApiOperation(value="獲取用戶列表", notes="")
 @ApiResponses({
  @ApiResponse(code=400,message="請求參數(shù)沒填好"),
  @ApiResponse(code=404,message="請求路徑?jīng)]有或頁面跳轉(zhuǎn)路徑不對")
 })
 @RequestMapping(value={""}, method=RequestMethod.GET)
 public List<User> getUserList() {
  List<User> r = new ArrayList<User>(users.values());
  return r;
 }
 @ApiOperation(value="創(chuàng)建用戶", notes="根據(jù)User對象創(chuàng)建用戶")
 @ApiImplicitParam(name = "user", value = "用戶詳細(xì)實體user", required = true, dataType = "User")
 @ApiResponses({
  @ApiResponse(code=400,message="請求參數(shù)沒填好"),
  @ApiResponse(code=404,message="請求路徑?jīng)]有或頁面跳轉(zhuǎn)路徑不對")
 })
 @RequestMapping(value="", method=RequestMethod.POST)
 public String postUser(@RequestBody User user) {
  users.put(user.getId(), user);
  return "success";
 }
 @ApiOperation(value="獲取用戶詳細(xì)信息", notes="根據(jù)url的id來獲取用戶詳細(xì)信息")
 @ApiImplicitParam(name = "id", value = "用戶ID",paramType="path", required = true, dataType = "String")
 @ApiResponses({
  @ApiResponse(code=400,message="請求參數(shù)沒填好"),
  @ApiResponse(code=404,message="請求路徑?jīng)]有或頁面跳轉(zhuǎn)路徑不對")
 })
 @RequestMapping(value="/{id}", method=RequestMethod.GET)
 public User getUser(@PathVariable String id) {
  System.out.println("id="+id);
  return users.get(id);
 }
 @ApiOperation(value="更新用戶詳細(xì)信息", notes="根據(jù)url的id來指定更新對象,并根據(jù)傳過來的user信息來更新用戶詳細(xì)信息")
 @ApiImplicitParams({
   @ApiImplicitParam(name = "id", value = "用戶ID",paramType="path", required = true, dataType = "String"),
   @ApiImplicitParam(name = "user", value = "用戶詳細(xì)實體user", required = true, dataType = "User")
 })
 @ApiResponses({
  @ApiResponse(code=400,message="請求參數(shù)沒填好"),
  @ApiResponse(code=404,message="請求路徑?jīng)]有或頁面跳轉(zhuǎn)路徑不對")
 })
 @RequestMapping(value="/{id}", method=RequestMethod.PUT)
 public String putUser(@PathVariable String id, @RequestBody User user) {
  System.out.println("id="+id);
  User u = users.get(id);
  u.setName(user.getName());
  users.put(id, u);
  return "success";
 }
 @ApiOperation(value="刪除用戶", notes="根據(jù)url的id來指定刪除對象")
 @ApiImplicitParam(name = "id", value = "用戶ID",paramType="path", required = true, dataType = "String")
 @ApiResponses({
  @ApiResponse(code=400,message="請求參數(shù)沒填好"),
  @ApiResponse(code=404,message="請求路徑?jīng)]有或頁面跳轉(zhuǎn)路徑不對")
 })
 @RequestMapping(value="/{id}", method=RequestMethod.DELETE)
 public String deleteUser(@PathVariable String id) {
  System.out.println("id="+id);
  users.remove(id);
  return "success";
 }
}

說明:

    @Api:用在類上,說明該類的作用
    @ApiOperation:用在方法上,說明方法的作用
    @ApiImplicitParams:用在方法上包含一組參數(shù)說明
    @ApiImplicitParam:用在@ApiImplicitParams注解中,指定一個請求參數(shù)的各個方面
        paramType:參數(shù)放在哪個地方
            header-->請求參數(shù)的獲?。篅RequestHeader
            query-->請求參數(shù)的獲?。篅RequestParam
            path(用于restful接口)-->請求參數(shù)的獲取:@PathVariable
            body(不常用)
            form(不常用)
        name:參數(shù)名
        dataType:參數(shù)類型
        required:參數(shù)是否必須傳
        value:參數(shù)的意思
        defaultValue:參數(shù)的默認(rèn)值
    @ApiResponses:用于表示一組響應(yīng)
    @ApiResponse:用在@ApiResponses中,一般用于表達(dá)一個錯誤的響應(yīng)信息
        code:數(shù)字,例如400
        message:信息,例如"請求參數(shù)沒填好"
        response:拋出異常的類
    @ApiModel:描述一個Model的信息(這種一般用在post創(chuàng)建的時候,使用@RequestBody這樣的場景,請求參數(shù)無法使用@ApiImplicitParam注解進(jìn)行描述的時候)
        @ApiModelProperty:描述一個model的屬性

通過 @ApiOperation 注解來給API增加說明、通過 @ApiImplicitParams 、 @ApiImplicitParam 注解來給參數(shù)增加說明。

需要注意的是:

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

具體其他的注解,查看:

https://github.com/swagger-api/swagger-core/wiki/Annotations#apimodel

2.測試:

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

GET紅框:method=RequestMethod.GET

右邊紅框:@ApiOperation

parameter紅框:@ApiImplicitParams系列注解

response messages紅框:@ApiResponses系列注解

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

以上所述是小編給大家介紹的spring boot整合Swagger2的示例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Spring中@Autowired注解的原理詳解

    Spring中@Autowired注解的原理詳解

    這篇文章主要介紹了Spring中@Autowired注解的原理詳解,對于spring配置一個bean時,如果需要給該bean提供一些初始化參數(shù),則需要通過依賴注入方式,所謂的依賴注入就是通過spring將bean所需要的一些參數(shù)傳遞到bean實例對象的過程,需要的朋友可以參考下
    2023-11-11
  • Spring實戰(zhàn)之注入嵌套Bean操作示例

    Spring實戰(zhàn)之注入嵌套Bean操作示例

    這篇文章主要介紹了Spring實戰(zhàn)之注入嵌套Bean操作,結(jié)合實例形式分析了嵌套Bean相關(guān)配置與使用操作技巧,需要的朋友可以參考下
    2019-11-11
  • 圖解Java排序算法之堆排序

    圖解Java排序算法之堆排序

    這篇文章主要為大家詳細(xì)介紹了Java經(jīng)典排序算法之堆排序,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • 使用Java Minio搭建自己的文件系統(tǒng)詳解

    使用Java Minio搭建自己的文件系統(tǒng)詳解

    這篇文章主要介紹了使用Java Minio搭建自己的文件系統(tǒng)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2021-09-09
  • MyBatis后端對數(shù)據(jù)庫進(jìn)行增刪改查等操作實例

    MyBatis后端對數(shù)據(jù)庫進(jìn)行增刪改查等操作實例

    Mybatis是appach下開源的一款持久層框架,通過xml與java文件的緊密配合,避免了JDBC所帶來的一系列問題,下面這篇文章主要給大家介紹了關(guān)于MyBatis后端對數(shù)據(jù)庫進(jìn)行增刪改查等操作的相關(guān)資料,需要的朋友可以參考下
    2022-08-08
  • 利用spring boot如何快速啟動一個web項目詳解

    利用spring boot如何快速啟動一個web項目詳解

    這篇文章主要給大家介紹了關(guān)于利用spring boot如何快速啟動一個web項目的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧、
    2017-12-12
  • jmeter基本使用小結(jié)

    jmeter基本使用小結(jié)

    jmeter是apache公司基于java開發(fā)的一款開源壓力測試工具,體積小,功能全,使用方便,是一個比較輕量級的測試工具,使用起來非常簡單。本文就簡單的介紹一下如何使用,感興趣的
    2021-11-11
  • Java中的鎖與鎖的狀態(tài)升級詳細(xì)解讀

    Java中的鎖與鎖的狀態(tài)升級詳細(xì)解讀

    這篇文章主要介紹了Java中的鎖與鎖的狀態(tài)升級詳細(xì)解讀,Java 1.6以后官方針對鎖的優(yōu)化,主要是增加了兩種新的鎖:偏向鎖和輕量級鎖,再加上本身重量級鎖,那么鎖基本上可以大致分為這三種,它們之間的區(qū)別主要是體現(xiàn)在等待時間上面,需要的朋友可以參考下
    2024-01-01
  • java中GZIP壓縮解壓類使用實例

    java中GZIP壓縮解壓類使用實例

    這篇文章主要介紹了java中GZIP壓縮解壓類使用實例的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • SpringBoot壓縮json并寫入Redis的示例代碼

    SpringBoot壓縮json并寫入Redis的示例代碼

    由于業(yè)務(wù)需要,存入redis中的緩存數(shù)據(jù)過大,占用了10+G的內(nèi)存,內(nèi)存作為重要資源,需要優(yōu)化一下大對象緩存,所以我們需要對json進(jìn)行壓縮,本文給大家介紹了SpringBoot如何壓縮Json并寫入redis,需要的朋友可以參考下
    2024-08-08

最新評論