10

I have a webapp that redirects to a particular URI: let's say /service/library. In another bundle, I have a jaxrs server that listens for /service in the URI, and defines some beans to handle the request. There are quite a few beans there already, and one of the classes is already implemented to handle requests for /service/library. I am trying to create a new class that also handles requests for /service/library, but with a different absolute URI path, for example: /service/library/mynewlibrary. My question is, is it possible to define the same @Path identifier in two classes, or must they be unique, in other words, will I need to use a URI like /service/mylibrary for my new class implementation instead of implementing a second class that also uses the same @Path identifier? I am pretty new to JAX-RS, so I hope my question makes sense!

Thanks!

4
  • 1
    What happens when you try it? Apr 18, 2011 at 22:16
  • When I tried it, none of the methods in my new class are being called, and only the methods from the current class handling /service/library are being called
    – Stephen
    Apr 18, 2011 at 22:37
  • @user663041 are you sure the new class is registered? What JAX-RS framework do you use?
    – Tarlog
    Apr 19, 2011 at 17:01
  • @Tarlog I am using CXFServlet, where can I make sure the class is registered? In my beans.xml file, it's using a jaxrs:server address="/service" and jaxrs:serviceBeans as "classA" and "classB" where classA uses @Path("/library") and classB (my class) is trying to use @Path("/library") as well. For example, one of the methods of classA will handle a request for /service/library/books, and a method from classB will handle a request for /service/library/movies. I'm not allowed to just add another method in classA to handle /service/library/movies, so that's why I want another class. Thanks!
    – Stephen
    Apr 19, 2011 at 22:25

3 Answers 3

5

It's possible to have two @Path annotations that match the URI. In your case, if servlet-mapping is service, you may have @Path("/library") and @Path("library/mynewlibrary"). When request arrives, the matching paths are sorted in descending order, so the second class should be called, when a request with /service/library/mynewlibrary arrives.

4
  • Is the @Path("library/mynewlibrary") that you mentioned, the @Path identifier for the class or the method within the class? This is what I'm trying to do: classA will handle /service/library/books and classB will handle /service/library/movies, so they will both have a @Path("/library") identifier for their class declaration, while the methods within the two classes will handle two different URI's. For example, classA.getBooks() will have @Path("/books") and classB.getMovies() will have @Path("/movies"), assuming they are all relative paths. Is it possible for two @Path("/library")?
    – Stephen
    Apr 19, 2011 at 22:36
  • I mean the class annotation and not the method annotation. Your approach will not work, since the framework may select the wrong class: it first tries to select the most correct class and then the most correct method.
    – Tarlog
    Apr 20, 2011 at 5:57
  • If you have one class that handles "books" and one class that handles "movies", why do you need method level annotations? Put the annotations at class level and the problem will be solved.
    – Tarlog
    Apr 20, 2011 at 6:04
  • The "books" and "movies" are just simple examples to help understand my situation, I am doing nothing related to that, but your answer makes sense to me now. I've had to change my logic a little bit, as both classes cannot have the same @Path annotation. Everything works out now though, Thanks!
    – Stephen
    Apr 20, 2011 at 22:57
1

It's most certainly possible to have two methods with the same @Path annotation, e.g., if they're distinguished by other means (such as HTTP method or @Consumes annotation). The @Path on a class acts as a default/root for the @Paths on the class's methods. Moreover, it's not a problem at all if you've got one path that is “within” another; JAX-RS specifies that the most specific match possible is used. (I prefer to not do it that way, instead having the “outer” class return a reference to the “inner” class on a suitable partial match, so that every path has a traceable route to responsibility that definitely leads to a single class. That requires a fairly different way of arranging the @Path annotations though.)

But if you've ended up with two methods that can serve the same incoming request, you've got a clash and the JAX-RS implementation will be free to pick which one to use (in an implementation-dependent manner). That's probably not what you want, as computers tend to make bad decisions when given a free choice.

0

You can achieve your goal to have /service/library/mynewlibrary using below configuration.

In your existing class you have /service/library configured at class level so you can configure /service at class level in new class you are adding and then at method level configure /library/mynewlibrary.

This way it will not have same path for both classes and your goal is also achieved. I tried this and it works.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.