Skip to content
Advertisement

javax.ws.rs.Path how to intercept just the project root without compromising all the other paths

I have this web.xml, don’t want a suffix for the url-pattern so I’m using a /* pattern:

JavaScript

This is my RestVersion.java class that I want to manage the root requests:

JavaScript

Where IRestVersion.java is the following:

JavaScript

The problem is that any other path is intercepted by this RestVersion class, like this:

  • http://localhost:8080/ —> it answers correctly with the RestVersion.version() json
  • http://localhost:8080/asd —> it is intercepted always by RestVersion but I would like to manage this on another class which will have a @Path("/asd") on top (but in this moment I cannot with this configuration)

How can I intercept just the project root without compromising all the other paths?

Advertisement

Answer

Add @Path("/") at class level. And in the other classes you want to manage, add their specific @Path("/asd").

At the end is all a hierarchy, starting by @ApplocationPath, followed by @Path at class level and ending in @Path at method level.

With combinations of those you should be able to manage any case.

The @GET, if found in a method without @Path, will manage GET requests of the @Path of the class level annotation.

Update: Adding an example

So, avoiding interfaces for simplification (although I only use them if necessary), you should accomplish what you want with this 2 classes:

JavaScript

and

JavaScript

And to activate JAX-RS you can do it via web.xml or simply by adding this other class:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement