1

I noticed that org.glassfish.jersey.servlet.ServletContainer is required to be used as Filter (setting spring.jersey.type=filter in spring boot's application.properties) instead than as a Servlet in order to provide access to static resources (e.g. /static/somefile.jsp, /static/myfile.css).

Since I am forced to use ServletContainer as a Servlet, is there any workaround to allow static resources with this limitation?

alessiop86
  • 1,285
  • 2
  • 19
  • 38
  • 1
    _"...is there any workaround..."_. Change your app root path to something other than the default `/*`. Just annotate your ResourceConfig subclass with something like `@ApplicationPath("/api")`. – Paul Samsotha Jan 25 '16 at 13:33
  • @peeskillet thank you for your hint, it does work indeed, but unfortunately I can't proceed on this road because I can't change the existing urls – alessiop86 Jan 25 '16 at 14:21
  • 1
    The only other thing I can think of is to use [Jersey's MVC feature](https://jersey.java.net/documentation/latest/mvc.html) to server up the JSPs. This doesn't require a filter. For the other static content, you would need to create resource methods for them. For this you would also need to produce your own caching headers, so the brower caches them. – Paul Samsotha Jan 25 '16 at 14:32
  • 1
    The problem is not with it being a servlet or a filter, but it's a problem with the `/*`. This hogs up all the request. This is not a limitation of Jersey, but of servlets in general. Where normally the default servlet would serve up the static content from the root, when you specify Jersey to use the root, the static content can't be accessed. The _solution_ is to use Jersey as a filter, that forwards the request to the servlet container, when it can't find the static content path within the Jersey application – Paul Samsotha Jan 25 '16 at 14:35
  • @peeskillet Do you mean mapping each static resource to a method of a controller? If yes, I thought about that but that seems unpractical since I have a lot of files. I'll have a look at Jersey's MVC for jsp and I can always put images, js and css on another server – alessiop86 Jan 25 '16 at 14:36
  • 1
    Resource paths can use regex, e.g. `@Path("{file: (*.js|*.css)}")`, if that makes it easier – Paul Samsotha Jan 25 '16 at 14:38
  • 1
    You might also want to [try this](http://stackoverflow.com/a/837020/2587435) in an `ExceptionMapper` for NotFoundException. I've never tried it, but I could see it working. When Jersey can't find a resource, it will throw a NotFoundException. You can try to forward the request yourself to the default servlet. – Paul Samsotha Jan 25 '16 at 14:57

1 Answers1

0

In the end I used the first solution provided by @peeskillet

I annotated my ResourceConfig subclass with @ApplicationPath("/v1"), and managed to keep the endpoint unchanged: luckily they were all defined as @Path("/v1/endpoint1)", @Path("/v1/endpoint2"), etc. so I removed /v1 from their @Path (e.g. @Path("/endpoint1"), and they are still accessible at their original address (e.g. /v1/endpoint1 ) thanks to the @ApplicationPath prefix /v1.

alessiop86
  • 1,285
  • 2
  • 19
  • 38