Using relative url with RESTEasy path annotation

I ran into the following problem in the office this week, I’m working on a WAR project that exposes multiple services of different types (SOAP, REST, AMF). The REST part was written using Jboss’s RESTEasy, after I renamed the REST servlet RESTEasy’s services were no longer working. I was surprised to find out that the servlet name was hard coded into the code as part of the @Path annotation content, in order to update RESTEasy with the new servlet name, I was supposed to change the annotation content in a number of classes. Luckily there’s a work around for this, and it’s one that in my opinion you should always use, RESTEasy lets you specify the path prefix as a context variable, all you have to do is declare it in your web.xml in the following way…

in my code I have the following Path deceleration:
<pre style="font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; border: 1px dashed rgb(153, 153, 153); line-height: 14px; padding: 5px; overflow: auto; width: 100%;">@Path("/rest-api/user")
</pre>rest-api is the servlet name, I’d like to make sure it’s no longer hard coded in my application..

I will add the “mapping prefix” into my web.xml:
<pre style="font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; border: 1px dashed rgb(153, 153, 153); line-height: 14px; padding: 5px; overflow: auto; width: 100%;">

rest-servlet
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher


rest-servlet
/rest-api/*


resteasy.servlet.mapping.prefix
/rest-api

.
.
.

</pre>Now when I’ve added the mapping prefix to the web.xml I can drop the servlet name from all path annotations:
<pre style="font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; color: rgb(0, 0, 0); background-color: rgb(238, 238, 238); font-size: 12px; border: 1px dashed rgb(153, 153, 153); line-height: 14px; padding: 5px; overflow: auto; width: 100%;">@Path("/user")
</pre>In case I’de like to rename my servlet or for some reason change a part of the service URL, all I need to do is update the mapping prefix variable in the web.xml

Thank you for your interest!

We will contact you as soon as possible.

Send us a message

Oops, something went wrong
Please try again or contact us by email at info@tikalk.com