包含 Jersey REST 时,Spring Boot 不提供静态内容 [英] Spring boot not serving static content when Jersey REST is included

查看:37
本文介绍了包含 Jersey REST 时,Spring Boot 不提供静态内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试从 Spring Boot 应用程序提供 index.html(位于 main/resources/static 下)时收到 HTTP 404 错误.但是,如果我从项目中删除基于 Jersey 的 JAX-RS 类,则 http://localhost:8080/index.html 工作正常.

I am getting a HTTP 404 error when trying to serve index.html ( located under main/resources/static) from a spring boot app. However if I remove the Jersey based JAX-RS class from the project, then http://localhost:8080/index.html works fine.

以下是主类

@SpringBootApplication
public class BootWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(BootWebApplication.class, args);
    }
}

我不确定我是否在这里遗漏了什么.

I am not sure if I am missing something here.

谢谢

推荐答案

问题是Jersey servlet路径的默认设置,默认为/*.这会占用所有请求,包括对静态内容的默认 servlet 的请求.所以请求去Jersey寻找静态内容,当在Jersey应用内找不到资源时,会发出404.

The problem is the default setting of the Jersey servlet path, which defaults to /*. This hogs up all the requests, including request to the default servlet for static content. So the request is going to Jersey looking for the static content, and when it can't find the resource within the Jersey application, it will send out a 404.

您有几个选择:

  1. 将 Jerse 运行时配置为过滤器(而不是默认为 servlet).请参阅这篇文章,了解如何做到这一点.同样使用此选项,您需要配置 ServletProperties 之一以将 404 转发到 servlet 容器.您可以使用配置 Jersey 的属性来转发 all 导致找不到 Jersey 资源的请求,或者使用允许您为要转发的请求配置正则表达式模式的属性.

  1. Configure Jerse runtime as a filter (instead of as a servlet by default). See this post for how you can do that. Also with this option, you need to configure one of the ServletProperties to forward the 404s to the servlet container. You can use the property that configures Jersey to forward all request which results in a Jersey resource not being found, or the property that allows you to configure a regex pattern for requests to foward.

您可以简单地将 Jersey servlet 模式更改为默认值以外的其他模式.最简单的方法是使用 @ApplicationPath("/root-path") 注释您的 ResourceConfig 子类.或者你可以在你的 application.properties - spring.jersey.applicationPath 中配置它.

You can simply change the Jersey servlet pattern to something else other than the default. The easiest way to do that is to annotate your ResourceConfig subclass with @ApplicationPath("/root-path"). Or you can configure it in your application.properties - spring.jersey.applicationPath.

这篇关于包含 Jersey REST 时,Spring Boot 不提供静态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆