springboot集成swagger
1.pom引入
<!--swagger-->
<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.配置类
import com.google.common.base.Predicates;
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.builders.RequestHandlerSelectors;
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;
/**
* @author :author
* @date :Created in 2023/11/03 10:38:03
* @description: SwaggerConfig
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.pathMapping("/")
.select() // 选择那些路径和api会生成document
.apis(RequestHandlerSelectors.any())// 对所有api进行监控
//不显示错误的接口地址
.paths(Predicates.not(PathSelectors.regex("/error.*")))//错误路径不监控
.paths(PathSelectors.regex("/.*"))// 对根下所有路径进行监控
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("接口口文档档")
.contact(new Contact("romeo", "", "xxx@qq.com"))
.description("这是SWAGGER_2生成的接口文档")
.termsOfServiceUrl("NO terms of service")
.license("The Apache License, Version 2.0")
.licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
.version("v1.0")
.build();
}
}
3.controller配置:
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
/**
* @author :author
* @date :Created in 2023/10/25 08:39:33
* @description: AdminController
*/
@Api(value = "api/admin/evidence/", tags = "管理端-存证信息")//swagger注解
@Slf4j
@RestController
@RequestMapping("api/admin/evidence/")
public class AdminEvidenceController extends AdminBaseController {
@Resource
EvidenceService evidenceService;
@ApiOperation(value = "存证-分页", notes = "存证-分页")//swagger注解
@AdminAuthorization
@PostMapping("listPager")
public JsonResult listPager(@RequestBody EviAdminQueryVO queryVO){
return evidenceService.getAdminPager(queryVO);
}
}
4.访问http://127.0.0.1:9110/swagger-ui.html#/,9110是我自己项目的端口号,改成自己本项目的端口号即可
5.(可选)拦截器放行
如果配置了某种拦截器校验参数,需要对swagger的url放行,如:
package net.cnki.evidence.platform_evidence_author.config;
import net.cnki.evidence.platform_evidence_author.interceptor.CheckSignInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @author :author
* @date :Created in 2023/10/07 15:46:54
* @description: MyWebMvcConfig
*/
@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {
@Autowired
private YourInterceptor yourInterceptor;
/**
* 拦截器配置
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(yourInterceptor).addPathPatterns("/api/**").excludePathPatterns(
"/**/*swagger*/**",//放行swagger
"/api/xx/**"
);
}
}