解决Swagger3接口文档无法访问

项目整合 Swagger3 生成的接口文档无法正常访问

解决方法是在 WebMvcConfig 配置类中重写 WebMvcConfigurationSupport 中的 addResourceHandlers 方法,设置静态资源映射。

WebMvcConfig

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

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    /**
     * @Description: 设置静态资源映射
     * @Author: 翰戈.summer
     * @Date: 2023/11/17
     * @Param: ResourceHandlerRegistry
     * @Return: void
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {

        //处理静态资源无法访问
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");
        //处理Swagger无法访问
        registry.addResourceHandler("/swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        //处理Swagger的js文件无法访问
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

如果项目中整合了 SpringSecurity6 ,则还需要在 SecurityConfig 配置类中为静态资源放行。

SecurityConfig

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;

/**
 * @Description: SpringSecurity配置类
 * @Author: 翰戈.summer
 * @Date: 2023/11/17
 * @Param:
 * @Return:
 */
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig {

    /**
     * 静态资源放行
     */
    @Bean
    public WebSecurityCustomizer webSecurityCustomizer() {
        return (web) -> web.ignoring().requestMatchers(
                "/doc.html",
                "/doc.html/**",
                "/v3/api-docs",
                "/v3/api-docs/**",
                "/webjars/**",
                "/authenticate",
                "/swagger-ui.html/**",
                "/swagger-resources",
                "/swagger-resources/**"
        );
    }
}

如果不知道如何在 SpringBoot3 项目中整合 SpringSecurity 的话,可以看看我的这篇文章:

https://blog.csdn.net/qq_74312711/article/details/134558633?spm=1001.2014.3001.5501

  • 12
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Swagger是一种API文档生成工具,可以帮助开发人员自动生成API文档。Swagger3是Swagger的最新版本,它提供了更多的功能和更好的用户体验。下面是使用Swagger3生成接口文档的步骤: 1.在pom.xml文件中添加Swagger3的依赖: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> ``` 2.添加Swagger配置类: ```java @Configuration @EnableSwagger2WebMvc public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) .paths(PathSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("API文档") .description("这是一个API文档示例") .version("1.0.0") .build(); } } ``` 3.在Controller中添加Swagger接口注解: ```java @RestController @RequestMapping("/api") @Api(tags = "用户管理") public class UserController { @GetMapping("/users") @ApiOperation("获取用户列表") public List<User> getUsers() { // ... } @PostMapping("/users") @ApiOperation("创建用户") public void createUser(@RequestBody User user) { // ... } @GetMapping("/users/{id}") @ApiOperation("获取用户信息") public User getUserById(@PathVariable Long id) { // ... } @PutMapping("/users/{id}") @ApiOperation("更新用户信息") public void updateUser(@PathVariable Long id, @RequestBody User user) { // ... } @DeleteMapping("/users/{id}") @ApiOperation("删除用户") public void deleteUser(@PathVariable Long id) { // ... } } ``` 4.启动应用程序并访问http://localhost:8080/swagger-ui/index.html,即可查看和测试接口。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值