最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot集成SwaggerUi以及啟動(dòng)時(shí)遇到的錯(cuò)誤

 更新時(shí)間:2020年06月10日 14:15:18   作者:烽火連天下  
這篇文章主要介紹了SpringBoot集成SwaggerUi以及啟動(dòng)時(shí)遇到的錯(cuò)誤,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

SwaggerUi是一個(gè)自動(dòng)生成接口文檔,并且還可以去測試這些接口的東西。

SpringBoot集成SwaggerUi

引入依賴

<properties>
    <swagger.version>2.6.1</swagger.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>${swagger.version}</version>
    </dependency>

    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>${swagger.version}</version>
    </dependency>
  </dependencies>

編寫Swagger配置類com.wjh.config.SwaggerConfig

package com.wjh.config;

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.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration  //表示是Swagger的配置類
@EnableSwagger2  //啟用Swagger2
public class SwaggerConfig {
  @Bean
  public Docket api(){
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo())
        .pathMapping("/")
        .select()
        .paths(PathSelectors.regex("/.*"))
        .build();
  }

  private ApiInfo apiInfo() {
    return new ApiInfoBuilder().title("我的接口文檔")
        .contact(new Contact("wjh", "", "wjh_dan@163.com"))
        .description("這是swaggerUI生成的接口文檔")
        .version("1.0.0.0")
        .build();
  }
}

編寫接口方法類com.wjh.server.MyMethod

package com.wjh.server;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RestController
@Api(value = "/", description = "全部的get方法") //Swagger的注解
public class MyMethod {

  @RequestMapping(value = "/getCookies", method = RequestMethod.GET)
  @ApiOperation(value = "通過這個(gè)方法可以獲取到cookies", httpMethod = "GET")  //Swagger的注解
  public String getCookies(HttpServletResponse response){
    //HttpServletRequest 裝請(qǐng)求信息的類
    //HttpServletResponse 裝相應(yīng)信息的類
    Cookie cookie = new Cookie("login", "true");
    response.addCookie(cookie);
    return "恭喜你,獲得cookies成功!";
  }

  /**
   * 要求客戶端攜帶cookies訪問
   * 這是一個(gè)需要攜帶cookies信息才能訪問的get請(qǐng)求
   */
  @RequestMapping(value = "/get/with/cookies", method = RequestMethod.GET)
  @ApiOperation(value = "要求客戶端攜帶cookies訪問", httpMethod = "GET")  //Swagger的注解
  public String getWithCookies(HttpServletRequest request){
    Cookie[] cookies = request.getCookies();
    if (Objects.isNull(cookies)){
      return "你必須攜帶cookies才能訪問";
    }

    for (Cookie cookie : cookies) {
      if (cookie.getName().equals("login") && cookie.getValue().equals("true")){
        return "這是一個(gè)需要攜帶cookies信息才能訪問的get請(qǐng)求";
      }
    }

    return "你必須攜帶cookies才能訪問";
  }

  /**
   * 開發(fā)一個(gè)需要攜帶參數(shù)才能訪問的get請(qǐng)求。
   * 第一種實(shí)現(xiàn)方式, url:key=value&key=value
   * 模擬獲取商品列表
   */
  @RequestMapping(value = "/get/with/param", method = RequestMethod.GET)
  @ApiOperation(value = "開發(fā)一個(gè)需要攜帶參數(shù)才能訪問的get請(qǐng)求。第一種實(shí)現(xiàn)方式", httpMethod = "GET")  //Swagger的注解
  public Map<String, Integer> getList(@RequestParam Integer start, @RequestParam Integer end){
    Map<String, Integer> myList = new HashMap<>();

    myList.put("鞋", 400);
    myList.put("襯衫", 300);
    myList.put("干脆面", 1);
    myList.put("雪碧", 3);

    return myList;
  }

  /**
   * 開發(fā)一個(gè)需要攜帶參數(shù)才能訪問的get請(qǐng)求。
   * 第二種實(shí)現(xiàn)方式, url: ip:port/get/with/param/10/20
   * 模擬獲取商品列表
   */
  @RequestMapping(value = "/get/with/param/{start}/{end}")
  @ApiOperation(value = "開發(fā)一個(gè)需要攜帶參數(shù)才能訪問的get請(qǐng)求。第二種實(shí)現(xiàn)方式", httpMethod = "GET")  //Swagger的注解
  public Map<String, Integer> myGetList(@PathVariable Integer start, @PathVariable Integer end) {
    Map<String, Integer> myList = new HashMap<>();

    myList.put("雪碧", 3);
    myList.put("鞋", 400);
    myList.put("襯衫", 300);
    myList.put("可樂", 3);

    return myList;
  }
}

編寫啟動(dòng)類Application

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan("com.wjh")
public class Application {
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

記一次啟動(dòng)時(shí)遇到的錯(cuò)誤。(上面類中都是正確的)

2020-06-08 21:39:10.147 ERROR 5720 --- [      main] s.d.s.r.o.OperationHttpMethodReader   : Invalid http method: GetValid ones are [[Lorg.springframework.web.bind.annotation.RequestMethod;@5477a1ca]

java.lang.IllegalArgumentException: No enum constant org.springframework.web.bind.annotation.RequestMethod.Get
	at java.lang.Enum.valueOf(Enum.java:238) ~[na:1.8.0_25]
	at org.springframework.web.bind.annotation.RequestMethod.valueOf(RequestMethod.java:35) ~[spring-web-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at springfox.documentation.swagger.readers.operation.OperationHttpMethodReader.apply(OperationHttpMethodReader.java:49) ~[springfox-swagger-common-2.6.1.jar:2.6.1]
	at springfox.documentation.spring.web.plugins.DocumentationPluginsManager.operation(DocumentationPluginsManager.java:123) [springfox-spring-web-2.6.1.jar:2.6.1]
	at springfox.documentation.spring.web.readers.operation.ApiOperationReader.read(ApiOperationReader.java:73) [springfox-spring-web-2.6.1.jar:2.6.1]
	at springfox.documentation.spring.web.scanners.CachingOperationReader$1.load(CachingOperationReader.java:50) [springfox-spring-web-2.6.1.jar:2.6.1]
	at springfox.documentation.spring.web.scanners.CachingOperationReader$1.load(CachingOperationReader.java:48) [springfox-spring-web-2.6.1.jar:2.6.1]
	at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3527) [guava-18.0.jar:na]
	at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2319) [guava-18.0.jar:na]
	at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2282) [guava-18.0.jar:na]
	at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2197) [guava-18.0.jar:na]
	at com.google.common.cache.LocalCache.get(LocalCache.java:3937) [guava-18.0.jar:na]
	at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3941) [guava-18.0.jar:na]
	at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4824) [guava-18.0.jar:na]
	at com.google.common.cache.LocalCache$LocalLoadingCache.getUnchecked(LocalCache.java:4830) [guava-18.0.jar:na]
	at springfox.documentation.spring.web.scanners.CachingOperationReader.read(CachingOperationReader.java:57) [springfox-spring-web-2.6.1.jar:2.6.1]
	at springfox.documentation.spring.web.scanners.ApiDescriptionReader.read(ApiDescriptionReader.java:66) [springfox-spring-web-2.6.1.jar:2.6.1]
	at springfox.documentation.spring.web.scanners.ApiListingScanner.scan(ApiListingScanner.java:89) [springfox-spring-web-2.6.1.jar:2.6.1]
	at springfox.documentation.spring.web.scanners.ApiDocumentationScanner.scan(ApiDocumentationScanner.java:70) [springfox-spring-web-2.6.1.jar:2.6.1]
	at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.scanDocumentation(DocumentationPluginsBootstrapper.java:85) [springfox-spring-web-2.6.1.jar:2.6.1]
	at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.start(DocumentationPluginsBootstrapper.java:127) [springfox-spring-web-2.6.1.jar:2.6.1]
	at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:182) [spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:53) [spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:360) [spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:158) [spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:122) [spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:894) [spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:162) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:553) [spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at Application.main(Application.java:10) [classes/:na]

原因是在MyGetMethod類中:

@ApiOperation(value = "通過這個(gè)方法可以獲取到cookies", httpMethod = "Get")

這個(gè)注解中httpMethod = "Get"出錯(cuò)。
將其改為:httpMethod = “GET”

@ApiOperation(value = "通過這個(gè)方法可以獲取到cookies", httpMethod = "GET")

即可。

打開瀏覽器,輸入http://localhost/swagger-ui.html

到此這篇關(guān)于SpringBoot集成SwaggerUi以及啟動(dòng)時(shí)遇到的錯(cuò)誤的文章就介紹到這了,更多相關(guān)SpringBoot集成Swagger內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 一篇文章帶你了解JAVA面對(duì)對(duì)象應(yīng)用

    一篇文章帶你了解JAVA面對(duì)對(duì)象應(yīng)用

    Java是一門面向?qū)ο蟮恼Z言。對(duì)象是Java程序中的基本實(shí)體。除了對(duì)象之外Java程序同樣處理基本數(shù)據(jù)。下面這篇文章主要給大家總結(jié)了關(guān)于Java中面向?qū)ο蟮闹R(shí)點(diǎn),需要的朋友可以參考借鑒,下面來一起看看吧
    2021-08-08
  • SpringBoot+Vue+Redis實(shí)現(xiàn)單點(diǎn)登錄(一處登錄另一處退出登錄)

    SpringBoot+Vue+Redis實(shí)現(xiàn)單點(diǎn)登錄(一處登錄另一處退出登錄)

    小編接到一個(gè)需求,需要實(shí)現(xiàn)用戶在瀏覽器登錄后,跳轉(zhuǎn)到其他頁面,當(dāng)用戶在其它地方又登錄時(shí),前面用戶登錄的頁面退出登錄,這篇文章主要介紹了SpringBoot+Vue+Redis實(shí)現(xiàn)單點(diǎn)登錄,需要的朋友可以參考下
    2019-12-12
  • 認(rèn)證授權(quán)中解決AuthenticationManager無法注入問題

    認(rèn)證授權(quán)中解決AuthenticationManager無法注入問題

    這篇文章主要介紹了認(rèn)證授權(quán)中解決AuthenticationManager無法注入問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-10-10
  • SpringBoot文件上傳同時(shí)接收復(fù)雜參數(shù)的過程詳解

    SpringBoot文件上傳同時(shí)接收復(fù)雜參數(shù)的過程詳解

    這篇文章主要介紹了SpringBoot文件上傳同時(shí),接收復(fù)雜參數(shù),本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-12-12
  • SpringBoot2.x中management.security.enabled=false無效的解決

    SpringBoot2.x中management.security.enabled=false無效的解決

    這篇文章主要介紹了SpringBoot2.x中management.security.enabled=false無效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java實(shí)現(xiàn)將TXT文本文件轉(zhuǎn)換為PDF文件

    Java實(shí)現(xiàn)將TXT文本文件轉(zhuǎn)換為PDF文件

    與TXT文本文件,PDF文件更加專業(yè)也更適合傳輸,所以這篇文章小編主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)將TXT文本文件轉(zhuǎn)換為PDF文件 ,需要的可以參考下
    2024-02-02
  • shenyu怎么處理sign鑒權(quán)前置到網(wǎng)關(guān)

    shenyu怎么處理sign鑒權(quán)前置到網(wǎng)關(guān)

    這篇文章主要為大家介紹了shenyu怎么處理sign鑒權(quán)前置到網(wǎng)關(guān)方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • java從字符串中提取數(shù)字的簡單實(shí)例

    java從字符串中提取數(shù)字的簡單實(shí)例

    下面小編就為大家?guī)硪黄猨ava從字符串中提取數(shù)字的簡單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-10-10
  • java打印菱形及直角和等腰三角形的方法

    java打印菱形及直角和等腰三角形的方法

    用Java輸出菱形本身是一個(gè)比較簡單的問題,這是Java初學(xué)者都要編寫的一個(gè)算法,下面這篇文章主要給大家介紹了關(guān)于java打印菱形及直角和等腰三角形的方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • java8日期工具類封裝的實(shí)戰(zhàn)記錄

    java8日期工具類封裝的實(shí)戰(zhàn)記錄

    java time包中的是類是不可變且線程安全的,新的時(shí)間及日期API位于java.time中,下面這篇文章主要給大家介紹了關(guān)于java8日期工具類封裝的相關(guān)資料,需要的朋友可以參考下
    2021-09-09

最新評(píng)論

古蔺县| 石河子市| 阿瓦提县| 普安县| 克什克腾旗| 泰来县| 福建省| 吴桥县| 林西县| 青龙| 镇宁| 左贡县| 吉水县| 阿图什市| 西和县| 怀安县| 连云港市| 郁南县| 宁安市| 周至县| 临澧县| 康保县| 拉萨市| 黎城县| 南开区| 盐亭县| 若羌县| 改则县| 邵武市| 镇雄县| 新泰市| 鹰潭市| 温泉县| 随州市| 成都市| 哈尔滨市| 怀宁县| 冀州市| 九龙县| 工布江达县| 长丰县|