SpringBoot集成SwaggerUi以及啟動(dòng)時(shí)遇到的錯(cuò)誤
SwaggerUi是一個(gè)自動(dòng)生成接口文檔,并且還可以去測(cè)試這些接口的東西。
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 = "通過(guò)這個(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訪問(wèn) * 這是一個(gè)需要攜帶cookies信息才能訪問(wèn)的get請(qǐng)求 */ @RequestMapping(value = "/get/with/cookies", method = RequestMethod.GET) @ApiOperation(value = "要求客戶端攜帶cookies訪問(wèn)", httpMethod = "GET") //Swagger的注解 public String getWithCookies(HttpServletRequest request){ Cookie[] cookies = request.getCookies(); if (Objects.isNull(cookies)){ return "你必須攜帶cookies才能訪問(wèn)"; } for (Cookie cookie : cookies) { if (cookie.getName().equals("login") && cookie.getValue().equals("true")){ return "這是一個(gè)需要攜帶cookies信息才能訪問(wèn)的get請(qǐng)求"; } } return "你必須攜帶cookies才能訪問(wèn)"; } /** * 開(kāi)發(fā)一個(gè)需要攜帶參數(shù)才能訪問(wèn)的get請(qǐng)求。 * 第一種實(shí)現(xiàn)方式, url:key=value&key=value * 模擬獲取商品列表 */ @RequestMapping(value = "/get/with/param", method = RequestMethod.GET) @ApiOperation(value = "開(kāi)發(fā)一個(gè)需要攜帶參數(shù)才能訪問(wèn)的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; } /** * 開(kāi)發(fā)一個(gè)需要攜帶參數(shù)才能訪問(wèn)的get請(qǐng)求。 * 第二種實(shí)現(xiàn)方式, url: ip:port/get/with/param/10/20 * 模擬獲取商品列表 */ @RequestMapping(value = "/get/with/param/{start}/{end}") @ApiOperation(value = "開(kāi)發(fā)一個(gè)需要攜帶參數(shù)才能訪問(wèn)的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("可樂(lè)", 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 = "通過(guò)這個(gè)方法可以獲取到cookies", httpMethod = "Get")
這個(gè)注解中httpMethod = "Get"出錯(cuò)。
將其改為:httpMethod = “GET”
@ApiOperation(value = "通過(guò)這個(gè)方法可以獲取到cookies", httpMethod = "GET")
即可。
打開(kāi)瀏覽器,輸入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?byte數(shù)組轉(zhuǎn)String的幾種常用方法
在Java中數(shù)組是一種非常常見(jiàn)的數(shù)據(jù)結(jié)構(gòu),它可以用來(lái)存儲(chǔ)多個(gè)相同類型的數(shù)據(jù),有時(shí)候,我們需要將數(shù)組轉(zhuǎn)換為字符串,以便于輸出或者傳遞給其他方法,這篇文章主要給大家介紹了關(guān)于java?byte數(shù)組轉(zhuǎn)String的幾種常用方法,需要的朋友可以參考下2024-09-09SpringBoot+Mybatis分頁(yè)插件PageHelper實(shí)現(xiàn)分頁(yè)效果
這篇文章主要介紹了SpringBoot+Mybatis實(shí)現(xiàn)分頁(yè)效果,本案例是采用Mybatis分頁(yè)插件PageHelper實(shí)現(xiàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-11-11Java多線程中的ThreadPoolExecutor使用解析
這篇文章主要介紹了Java多線程中的ThreadPoolExecutor使用解析,作為線程池的緩沖,當(dāng)新增線程超過(guò)maximumPoolSize時(shí),會(huì)將新增線程暫時(shí)存放到該隊(duì)列中,需要的朋友可以參考下2023-12-12SpringMVC多個(gè)模塊404報(bào)錯(cuò)問(wèn)題及解決
這篇文章主要介紹了SpringMVC多個(gè)模塊404報(bào)錯(cuò)問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09SpringSecurity?表單登錄的實(shí)現(xiàn)
本文主要介紹了SpringSecurity?表單登錄的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12java實(shí)現(xiàn)文件的斷點(diǎn)續(xù)傳
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)文件的斷點(diǎn)續(xù)傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06