How to serve static content with JAX-RS?
Asked Answered
C

3

17

I have a self hosted JAX-RS REST service implemented with the JAX-RS Restlet extension.

Now I have to serve static content and I was wondering how to do it with JAX-RS. Note, that I do not know the physical directory structure at compile-time. So, given a URL like

http://bla-bla:8182/static/yaba/daba/doo.png

the file $(ROOT)/yaba/daba/doo.png has to be returned, where $(ROOT) is the static content root directory.

Is it possible to do it with pure JAX-RS?

Thanks.

EDIT

Known at compile-time:

  • File system path of the static content root folder
  • HTTP URL used to reference the static content root folder

Unknown at compile-time:

  • The actual content of the root folder - how many files, file types, directory structure.
Crescen answered 27/12, 2011 at 9:18 Comment(0)
C
22

Just found it.

According to the javax.ws.rs.Path annotation javadocs one can specify a regex to indicate what is considered to be the template parameter match.

Hence, the following code works:

@Path("static")
public class StaticContentHandler {
  ...
  @GET
  @Path("{path:.*}")
  public FileRepresentation Get(@PathParam("path") String path) {
    ...;
  }
}

GET http://localhost:8182/static/yaba/daba/doo.png reaches the Get method with path equal to "yaba/daba/doo.png" - just what I was looking for.

Hope it helps anyone.

BTW, FileRepresentation belongs to Restlet, so a really pure JAX-RS implementation would return something else here.

Crescen answered 27/12, 2011 at 9:39 Comment(0)
S
6

Assuming that static folder is located here: ./src/main/resources/WEB-INF/static in your project:

@Path("")
public class StaticResourcesResource {

  @Inject ServletContext context;

  @GET
  @Path("{path: ^static\\/.*}")
  public Response staticResources(@PathParam("path") final String path) {

    InputStream resource = context.getResourceAsStream(String.format("/WEB-INF/%s", path));

    return Objects.isNull(resource)
        ? Response.status(NOT_FOUND).build()
        : Response.ok().entity(resource).build();
  }
}

Here is full description with how-to example and repository: https://daggerok.github.io/thymeleaf-ee/#configure-jax-rs-serve-static-files-and-webjars

Starbuck answered 28/6, 2018 at 3:41 Comment(2)
I already done good work with answer in targeted link. Have you even check it? What the sense of your -1? My answer best what people can find in this thread!Starbuck
Understand, my apologiesStarbuck
P
1

You can do it with pure JAX-RS by implementing the corresponding resources: basically you just need to send a byte array and JAX-RS already includes the Byte Array provider for any media type.

The problem that your implementation will probably be less efficient then standard implementations of web servers. Usually the best is to put the static content on a Web Server like Apache HTTPD or IIS or even Tomcat.

Pasadis answered 27/12, 2011 at 9:23 Comment(3)
I have a limitation, that there might be neither apache nor IIS available.Crescen
My problem is not sending the data, but matching the static resources using JAX-RS specification.Crescen
Can you elaborate what exactly is known in compilation time, what can be configured later and what is dynamic. Static content usually means something static and not dynamic...Pasadis

© 2022 - 2024 — McMap. All rights reserved.