【问题标题】:Trying to reach swagger-ui or api/api-docs but got a 404 error尝试访问 swagger-ui 或 api/api-docs 但出现 404 错误
【发布时间】:2019-07-06 13:51:51
【问题描述】:

这是我的第一篇文章,如果我做错了什么,请随时留下一些反馈:)

我正在使用 spring-boot 和 resteasy :

<!-- Spring Boot -->
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-dependencies</artifactId>
 <version>2.1.0.RELEASE</version>
 <type>pom</type>
 <scope>import</scope>
</dependency>
<dependency>
  <groupId>com.paypal.springboot</groupId>
  <artifactId>resteasy-spring-boot-starter</artifactId>
  <version>2.3.4-RELEASE</version>
</dependency>

我正在尝试使用 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>

此依赖项已加载到我的仓库中,一切看起来都不错。


我添加了这个类来制作最简单的配置类:

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}

当我在调试模式中启动应用程序时,我输入了以前的 Bean。一切看起来都很好。


当我启动 Spring 应用程序时:

@SpringBootApplication
@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class,RabbitAutoConfiguration.class})
public class SituationServicesApplication implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplication.run(SituationServicesApplication.class, args);
    }
    @Override
    public void run(String... args) throws Exception {
    }
}

一切看起来都很好:

2019-07-06 15:43:27.222  INFO 6336 --- [           main] pertySourcedRequestMappingHandlerMapping : Mapped URL path [v2/api-docs] onto method [public org.springframework.http.ResponseEntity<springfox.documentation.spring.web.json.Json> springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest)]

但是当我尝试访问 swagger-ui.html 时:

http://localhost:8092/test/v2/api-docs

http://localhost:8092/test/swagger-ui.html

我遇到了 404 错误:

“RESTEASY003210:找不到完整路径的资源: http://localhost:8092/test/v2/api-docs"。

我尝试通过添加属性来修改默认 URL:springfox.documentation.swagger.v2.path=v2/api-docs-test

仍然是 404。

我从头开始尝试了一个新的空项目,它工作得很好。我猜我的项目有问题。

我确定使用的 URL。

你知道我该如何调试这个问题吗?我可以从 Swagger 看到一些创建的源代码吗? 如果我可以放一些日志以了解问题出在哪里?

【问题讨论】:

  • 你好@Pierrot 你在使用 Spring Security,如果是,你必须添加一些配置
  • @Pierrot /test 路径定义在哪里?
  • 嗨 Madhu:在 JAXRS 配置中:@Component @ApplicationPath("/test/") public class SituationServicesJaxrsApplication extends ServiceApplication { }
  • 刚刚发生了一些非常奇怪的事情:我将其删除并再次粘贴,我已经能够访问 HTML :o 但它没有信息......:“规范中没有定义操作!”总比没有好,但仍然不明白刚刚发生了什么以及为什么我无法获得 API 的信息:s Docket Bean 仍然非常开放:.apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any())
  • 如果未指定“/test/”,则它不起作用... @ApplicationPath("/")

标签: java spring-boot swagger-ui swagger-2.0 springfox


【解决方案1】:

如果您使用的是 Spring Security,那么您必须添加更多配置,如下所示。

Complete Working Example

SecurityConfig.java

package com.swagger.demo.security;


import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter implements WebMvcConfigurer{

    //Swagger Resources
        @Override 
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
            registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        }

    //If you are using Spring Security Add Below Configuration

    @Override
    public void configure(WebSecurity web) throws Exception {
        web
          .ignoring()
            .antMatchers("/v2/api-docs", "/configuration/**", "/swagger*/**", "/webjars/**")
            .antMatchers(HttpMethod.OPTIONS,"/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception{

         http
         .csrf().disable()
         .authorizeRequests()
         .antMatchers("/**", "/v2/api-docs", "/configuration/**", "/swagger*/**", "/webjars/**")
         .permitAll()
         .anyRequest().authenticated(); 
    }

}

SwaggerConfig.java

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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 apiDocket() {

       Docket docket =  new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.swagger.demo"))
                .paths(PathSelectors.any())
                .build();

       return docket;

    } 
}

pom.xml

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

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-core</artifactId>
            <version>2.9.2</version>
        </dependency>

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

【讨论】:

  • 如果这是与 Spring 安全相关的问题,可能会出现 401 或 403 而不是 404。
  • 您好@MadhuBhat 是的,出于安全原因,它可能是 403/401。但是当我尝试实现 swagger 时,由于 spring 安全性阻止了资源加载,出现了 404 和 403
  • 您好帕特尔,非常感谢您的回答。没有使用 Spring Security,所以这不是问题/据我所知,我们对 API 调用的唯一限制是我们需要在调用的标头中提及 From:$header-From={name}。如果我们错过它,它会返回 401 响应而不是 404。但是感谢您的建议,很高兴知道并且可以帮助其他人:)
  • @Pierrot 你好,如果可能的话,你能分享你的项目来识别问题吗
  • 不幸的是,这是一个专业且庞大的项目,我无法分享它:/问题是日志丢失/我无法查看该问题,因为 Spring 似乎没有检测到该问题,只有 URL 不存在的事实:"RESTEASY003210: Could not find resource for full path: http://localhost:8092/test/v2/api-docs". 但它应该存在,因为我启用了它并生成了它:-D
【解决方案2】:

@Pierrot,您能否检查一下应用程序中配置的上下文路径和端口以及用于测试 swagger UI 的 URL 中的上下文路径和端口是否正确? 正如您所提到的,当您从头开始创建一个新项目时它运行良好,问题可能出在上下文路径/端口上。

【讨论】:

  • 您好,感谢您的回答!我很确定 URL:/ 我可能会强制端口到 Swagger 端?我正在使用特定的 8092 端口。不过应该没问题吧?
猜你喜欢
  • 1970-01-01
  • 2016-02-01
  • 1970-01-01
  • 2020-06-10
  • 2021-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-22
相关资源
最近更新 更多