springboot集成swagger2,解决访问404问题
·
现在主流的前后端分离开发,swagger是一个方便后端编写接口文档的开源项目,用于生成、描述、调用和可视化RESTful风格的Web服务。
1、pom引入jar,最新的3.0.0版本会报404,有点Bug,所以选择2.9.2
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.5.RELEASE</version>
</dependency>
<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配置类,访问swagger-ui.html如果报404,需要配置类实现WebMvcConfigurer接口,WebMvcConfigurer接口实际上就是springBoot做拦击的一个抽象的接口,在springBoot早期的版本,使用的是WebMvcConfigurerAdapter抽象父类进行管理权限。然后在SwaggerConfig类重写addResourceHandlers方法,将不拦截的路径放入。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @author KingLee
* @date 2020-11-10
* @since 1.0
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurationSupport {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.mywind.integrated"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("明阳HR集成AD接口")
.description("使用RestFul风格, 创建人:白领说事")
.version("1.0")
.build();
}
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
// 解决静态资源无法访问
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
// 解决swagger无法访问
registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
// 解决swagger的js文件无法访问
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
3、springboo启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyIntegratedApplication {
public static void main(String[] args) {
SpringApplication.run(MyIntegratedApplication.class, args);
}
}
4、controller类对外发布接口
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
@Api(tags = {"测试接口"})
public class HelloController {
@RequestMapping(value = "/get", method = RequestMethod.GET)
@ApiOperation(value = "测试swagger get")
public String get(){
return "swagger get!";
}
}
5、增加资源文件application.properties
# 服务端口
server.port=8001
# 服务名
spring.application.name=service-swagger
6、访问swagger
http://localhost:8001/swagger-ui.html
更多推荐
已为社区贡献1条内容
所有评论(0)