Swagger2工具自动生成接口文档(Springboot2.4+Swagger2整合)

Swagger2工具自动生成接口文档(Springboot2.4+Swagger2整合)

1.导入Swagger2依赖

依赖如下:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

2.添加一个配置类(SwaggerConfig.java)

代码如下:

package com.kd.nm.config;

import io.swagger.annotations.Contact;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig{
     
    @Bean
    public Docket createRestApi() {
     
        return new Docket(DocumentationType.SWAGGER_2)
            .pathMapping("/")
            .select()
            
            // com.kd.nm.controller是自己控制器路径(注意修改)
            .apis(RequestHandlerSelectors.basePackage("com.kd.nm.controller"))
            .paths(PathSelectors.any())
            .build().apiInfo(new ApiInfoBuilder()
                .title("SpringBoot整合Swagger")
                .description("SpringBoot整合Swagger,详细信息......")
                .version("9.0")
                .license("The Apache License")
                .licenseUrl("http://www.baidu.com")
                .build());
    }
}

3.控制器中添加相应的注解(@Api、@ApiOperation)

代码如下:

package com.kd.nm.controller;

import com.kd.nm.service.StandardService;
import com.kd.nm.util.NmOptData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


@Controller
@CrossOrigin
@RequestMapping("/api/standard")

// 新增注解
@Api(tags = "StandardController控制器相关信息")
public class StandardController {
     

  // 依赖注入
  @Autowired
  private StandardService standardService;

  // 日志打印
  private static final Logger LOGGER = LoggerFactory.getLogger(StandardController.class);


  @RequestMapping(value = "/getUserById",method = RequestMethod.GET)
  @ResponseBody
  
  // 新增注解
  @ApiOperation("getUserById接口相关信息")
  public NmOptData getUserById(@RequestParam("id") Integer id){
     

    LOGGER.debug("获取数据:"+standardService.getUserById(id).getData().getName());

    return standardService.getUserById(id);
  }
}

4.访问http://IP:端口号/contextPath(上下文路径)/swagger-ui.html#/查看接口文档

如图:
Swagger2工具自动生成接口文档(Springboot2.4+Swagger2整合)_第1张图片
Swagger2工具自动生成接口文档(Springboot2.4+Swagger2整合)_第2张图片

5.处理跨域问题(CorsConfig.java)会影响Swagger接口文档的生成(报404错误)

处理跨域相关代码:

package com.kd.nm.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class CorsConfig extends WebMvcConfigurationSupport {
     

  /**
   * 解决跨域问题(新版本2.4.2)
   *
   * @author 小辰哥哥
   * @param registry
   */
  @Override
  public void addCorsMappings(CorsRegistry registry) {
     
    registry.addMapping("/**")
        // 设置允许跨域请求的域名
        .allowedOriginPatterns("*")
        // 是否允许证书(cookies)
        .allowCredentials(true)
        // 设置允许的方法
        .allowedMethods("*")
        // 跨域允许时间
        .maxAge(3600);
  }

}

报错信息:
Swagger2工具自动生成接口文档(Springboot2.4+Swagger2整合)_第3张图片
防止踩坑!!!


总结

每天一个提升小技巧!!!

你可能感兴趣的:(Springboot,spring,boot,swagger2)