Reputation: 1855
I'm using Maven Jax-RS version 1.19
I am searching for a way to get to a path that is relative.
So for example:
My url: localhost/css/all
This url goes to a controller for the css
When i post the following url: localhost/parameter/anotherparam/css/all
Then i also want to go to the same controller.
I couldn't really find an answer fit to my question in the suggestions or on google. (Funny that Stackoverflow can still search there own site better than google)
So here is my question:
How do you make a relative path in Jax-RS so that the path that matches always follows no matter what the url is up front (or back)
my css controller:
@Path("/css")
@Singleton
public class CssController{
@GET
@Path("{id}")
public Response getCss(@Context HttpServletRequest request, @PathParam("id") String id
String path = request.getServletContext().getRealPath("/css/"+id+".css");
File f = new File(path);
if (f.exists()) {
return Response.ok(new Viewable(path)).build();
}
return null;
}
Upvotes: 0
Views: 1776
Reputation: 1855
I included the following regex to my path to make it take a parameter or not so now the url
css/all
and
/test/alsotest/css/all
both go to the css controller:
@Singleton
@Path("{param1 : ^(?![\\s\\S])|(\\w+/)*?}css/{id}")
public class CssController {
@GET
@Produces(MediaType.TEXT_HTML)
public Response getCss(@Context HttpServletRequest request, @PathParam("id") String id)
{
String path = request.getServletContext().getRealPath("/css/"+id+".css");
File f = new File(path);
if (f.exists()) {
return Response.ok(new Viewable(path)).build();
}
return null;
}
Please steal nicely!
with regex:
^(?![\s\S])|(\w+/)*?
in parameter:
{param1 : ^(?![\s\S])|(\w+/)*?}
so the full path for me is:
{param1 : ^(?![\s\S])|(\w+/)*?}css/{id}
I think this is also usefull for images and such, due to the relative path they can have, this way, location of calling doesn't matter, only the location of the resource!
I would like the thoughts of you stackoverflow people on this topic.
Looking forward!
Upvotes: 1