亲宝软件园·资讯

展开

SpringBoot集成Swagger SpringBoot集成SwaggerUi以及启动时遇到的错误

烽火连天下 人气:0

SwaggerUi是一个自动生成接口文档,并且还可以去测试这些接口的东西。

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 = "通过这个方法可以获取到cookies", httpMethod = "GET")  //Swagger的注解
  public String getCookies(HttpServletResponse response){
    //HttpServletRequest 装请求信息的类
    //HttpServletResponse 装相应信息的类
    Cookie cookie = new Cookie("login", "true");
    response.addCookie(cookie);
    return "恭喜你,获得cookies成功!";
  }

  /**
   * 要求客户端携带cookies访问
   * 这是一个需要携带cookies信息才能访问的get请求
   */
  @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 "这是一个需要携带cookies信息才能访问的get请求";
      }
    }

    return "你必须携带cookies才能访问";
  }

  /**
   * 开发一个需要携带参数才能访问的get请求。
   * 第一种实现方式, url:key=value&key=value
   * 模拟获取商品列表
   */
  @RequestMapping(value = "/get/with/param", method = RequestMethod.GET)
  @ApiOperation(value = "开发一个需要携带参数才能访问的get请求。第一种实现方式", 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;
  }

  /**
   * 开发一个需要携带参数才能访问的get请求。
   * 第二种实现方式, url: ip:port/get/with/param/10/20
   * 模拟获取商品列表
   */
  @RequestMapping(value = "/get/with/param/{start}/{end}")
  @ApiOperation(value = "开发一个需要携带参数才能访问的get请求。第二种实现方式", 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;
  }
}

编写启动类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);
  }
}

记一次启动时遇到的错误。(上面类中都是正确的)

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 = "通过这个方法可以获取到cookies", httpMethod = "Get")

这个注解中httpMethod = "Get"出错。
将其改为:httpMethod = “GET”

@ApiOperation(value = "通过这个方法可以获取到cookies", httpMethod = "GET")

即可。

打开浏览器,输入http://localhost/swagger-ui.html

加载全部内容

相关教程
猜你喜欢
用户评论