【问题标题】:Swagger routes return 404 on Spring Boot + JerseySwagger 路线在 Spring Boot + Jersey 上返回 404
【发布时间】:2016-06-03 04:37:54
【问题描述】:

我正在尝试将 Swagger 添加到现有的 Spring Boot 应用程序中,如 this 教程中所述。

我的主要应用类如下:

@SpringBootApplication
@EnableAutoConfiguration
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Swagger 配置为:

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

我可以看到 Spring 在启动日志中明显识别出 Swagger 及其路由:

s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/v2/api-docs],methods=[GET],produces=[application/json || application/hal+json]}" onto public org.springframework.http.ResponseEntity<springfox.documentation.spring.web.json.Json> springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest)
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/configuration/ui]}" onto org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.UiConfiguration> springfox.documentation.swagger.web.ApiResourceController.uiConfiguration()
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/configuration/security]}" onto org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.SecurityConfiguration> springfox.documentation.swagger.web.ApiResourceController.securityConfiguration()
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/swagger-resources]}" onto org.springframework.http.ResponseEntity<java.util.List<springfox.documentation.swagger.web.SwaggerResource>> springfox.documentation.swagger.web.ApiResourceController.swaggerResources()

但是当我尝试访问 http://localhost:8080/v2/api-docs 时,我得到一个普通的 404 作为响应。

我可以在网上找到的所有类似问题都涉及不使用 Spring Boot 并且必须手动映射资源的人(在同一教程链接中也有描述),但这不是我的情况。

任何想法如何解决它或 - 至少 - 在哪里进行调查?

【问题讨论】:

  • 使用 Spring Boot 时,没有应用上下文。由于您的应用程序是使用 1:1 对的容器自行部署的,因此所有路由都直接从根 / 开始。非 swagger 路线仍然完全可访问,直接从 / 开始,只有 swagger 路线获得 404...:/
  • 以上配置对我有用。你的配置中有@EnableWebMvc 吗?如果是这样,您需要添加 ResourceHandler 映射。你试过访问localhost:8080/swagger-ui.html吗?
  • 感谢您的回复。没有@EnableWebMvc,只有@SpringBootApplication@EnableAutoConfiguration。尝试访问 swagger-ui.html 也会导致 404,所以仍然没有运气:/

标签: java spring spring-boot jersey swagger


【解决方案1】:

我发现问题与球衣的应用程序路径配置有关。就我而言,通过以下注释:

@Component
@ApplicationPath("/") //WRONG
@Configuration
public class JerseyConfiguration extends ResourceConfig {
...
}

@ApplicationPath 配置会覆盖 Jersey 本身(如白标签 /error)或第三方库(在本例中为 /v2/api-docs/configuration/ui/configuration/security/swagger-resources 来自 swagger)发布的任何路由。简单地将/ 作为路径强制使用我的应用程序中的自定义控制器严格解析请求。

@ApplicationPath 限制为应用程序提供的实际路径(在我的情况下为 /api)将允许 jersey 将其他路由绑定到自动生成的控制器:

@Component
@ApplicationPath("/api") //RIGHT
@Configuration
public class JerseyConfiguration extends ResourceConfig {
...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    • 2020-02-18
    • 1970-01-01
    • 2018-12-13
    • 2021-05-18
    • 1970-01-01
    • 2018-02-06
    相关资源
    最近更新 更多