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

论坛 期权论坛 脚本     
niminba   2021-5-23 03:17   1389   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!va: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内容请搜索社区以前的文章或继续浏览下面的相关文章希望大家以后多多支持社区!

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1060120
帖子:212021
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP