java - Swagger 路线在 Spring Boot + Jersey 上返回 404

标签 java spring spring-boot jersey swagger

我正在尝试将 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 且必须手动映射资源的人(也在同一教程链接中进行了描述),但这不是我的情况。

有什么想法可以解决这个问题或者 - 至少 - 在哪里进行调查?

最佳答案

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

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

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

@ApplicationPath 限制为应用程序提供的实际路径(在我的例子中 /api)将允许 jersey 将其他路由绑定(bind)到自动生成的 Controller :

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

关于java - Swagger 路线在 Spring Boot + Jersey 上返回 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37606036/

相关文章:

java - 如何在服务类和 util 类之间拆分应用程序功能?

java - Spring Boot 返回 403 虽然我有管理员权限

spring - HTTP : What is the difference between SC_NOT_FOUND and NOT_FOUND

java - Spring boot servlet 在 Tomcat 但不是 Jetty 中显示性能问题

spring-boot - Zuul 通过外部代理服务器路由请求

java - 非官方 iText 4.2.1 : Purely created magenta color contains 44% of yellow

java - 适用于 Spring 和其他应用程序的 OSGi bundle

java - 用于 Java 8 的带有 lambda 表达式的构建器

java - 在 OSX 上通过 Java 访问 iSight

java - 使用 HSQLDB (2.2.8) + DDLUtils 自动增量