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

查看:369
本文介绍了当包含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.

您可以选择以下几种方法:

You have a couple options around this:

  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天全站免登陆