当前位置: 移动技术网 > IT编程>开发语言>Java > SpringBoot集成SwaggerUi以及启动时遇到的错误

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

2020年06月23日  | 移动技术网IT编程  | 我要评论

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

到此这篇关于springboot集成swaggerui以及启动时遇到的错误的文章就介绍到这了,更多相关springboot集成swagger内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网