使用Jetty / Jersey / Guice提供静态内容 [英] Serving static content with Jetty/Jersey/Guice

查看:354
本文介绍了使用Jetty / Jersey / Guice提供静态内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与其他问题类似(参见过滤静态内容泽西)我想提供静态服务Jetty的内容。在广阔的互联网上散布着几个类似的问题,但大多数都没有涉及Guice,而且那些完全过时。

Similar to another question (cf. Filtering static content Jersey) I want to serve static content from Jetty. There are several similar questions scattered all around the vast Internet, but most of them do not involve Guice, and those that do are completely out of date.

我已经存在使用Jersey(1.12)和Guice(3)以及 web.xml 的服务:

I have an existing service that uses Jersey (1.12) and Guice (3) with the following web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
    <display-name>My Service</display-name>

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>

    <listener>
        <listener-class>com.example.MyGuiceConfig</listener-class>
    </listener>

    <filter>
        <filter-name>Guice Filter</filter-name>
        <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>Guice Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

MyGuiceConfig 如下所示:

public class MyGuiceConfig extends GuiceServletContextListener
{
    @Override
    protected Injector getInjector()
    {
        return Guice.createInjector(new JerseyServletModule()
        {
            @Override
            protected void configureServlets()
            {
                bind(SomeResource.class);
                bind(SomeDao.class).to(ConcreteSomeDao.class);
                serve("/*").with(GuiceContainer.class);
            }
        });
    }
}

当我使用<调用jetty-maven-plugin时code> mvn jetty:运行,我的服务按预期工作。但是,对静态内容的任何请求都会产生404.

When I invoke the jetty-maven-plugin using mvn jetty:run, my service works as expected. But, any request to static content produces a 404.

如何在不影响我的服务的情况下提供任意静态内容? (即,不需要我更改我的技术堆栈所需的最小更改?)

How can I serve arbitrary static content without affecting my service? (i.e. The minimal change necessary that doesn't require me to change my tech stack?)

推荐答案

您是如何配置网址的? Jersey将在JerseyServletModule中处理的片段?如果你指定一个与你的静态内容不冲突的前缀,它应该可以工作。

How have you configured the url fragment that Jersey will handle in your JerseyServletModule? If you specify a prefix that doesn't conflict with your static content it should work.

public class Config extends GuiceServletContextListener {

  protected Injector getInjector() {
    return Guice.createInjector(
        new JerseyServletModule() {
          protected void configureServlets() {
            bind(Service.class);
            serve("/services/*").with(GuiceContainer.class);
          }
        });
  }

}

@Singleton
@Path("/service")
@Produces({MediaType.TEXT_PLAIN})
public class Service {

  @GET
  public String run() {
    return "Service running";
  }

}

应该从主机提供Servlet.class :8080 / webapp中包含的服务/服务和静态资源......

should serve Servlet.class from host:8080/services/service and static resources that are included in the webapp...

编辑请参阅泽西/ * servlet映射导致静态资源出现404错误在不改变REST端点路径的情况下完成此任务。

EDIT See Jersey /* servlet mapping causes 404 error for static resources for another way to accomplish this without changing the path of your REST endpoint.

这篇关于使用Jetty / Jersey / Guice提供静态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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