4

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.

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 looks like so:

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);
            }
        });
    }
}

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?)

Community
  • 1
  • 1
Shaggy Frog
  • 27,575
  • 16
  • 91
  • 128
  • see if this is of any help to you http://stackoverflow.com/questions/10567699/getting-http-500-when-running-servlet-in-jetty/10609507#10609507 – ant May 30 '12 at 00:51

1 Answers1

4

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);
          }
        });
  }

}

and

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

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

}

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

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.

Community
  • 1
  • 1
condit
  • 10,852
  • 2
  • 41
  • 60
  • I don't have a `JerseyServletModule`. It is unnecessary to create or serve the `.war`. – Shaggy Frog May 30 '12 at 01:34
  • What are you configuring in com.example.MyGuiceConfig? How are you informing Guice which Jersey service classes to inject? – condit May 30 '12 at 01:36
  • Oh, sorry, you're right. It's in MyGuiceConfig. I posted the code. – Shaggy Frog May 30 '12 at 03:03
  • If you're asking the Jetty GuiceContainer to serve "/*" I believe it will intercept every request without knowledge of the static resources. Can you change that path to be "/services/*" or something similar? Then the GuiceContainer shouldn't obscure your static resources. – condit May 30 '12 at 03:22
  • Thanks for your help. After I changed the container to serve "/api/*" and cleared out some old cruft, it's serving static resources. – Shaggy Frog May 30 '12 at 05:48