permaculture playing cards*
The moose likes Web Services Certification (SCDJWS/OCEJWSD) and the fly likes Can we put @Path, @Get in interface instead of class ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Services Certification (SCDJWS/OCEJWSD)
Bookmark "Can we put @Path, @Get in interface instead of class ?" Watch "Can we put @Path, @Get in interface instead of class ?" New topic
Author

Can we put @Path, @Get in interface instead of class ?

Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 734
I have this code to deploy to Tomcat



In my web.xml, I have this{


When I deploy it and run it, I got this severe message:
"SEVERE: The ResourceConfig instance does not contain any root resource classes."

If I put all the @Path, @GET in the shoppingApp.impl.ShoppingServiceImpl, everything works.

Question: can I put those JAX-RS annotations in the interface and let the classes to inherit them?
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1660
    
  25

Hi Himai,

First of all: with Servlet 3.0 you don't have to use the web.xml anymore. JAX-RS also supports that fact by introducing the @ApplicationPath annotation (see Jersey v1.17 tutorial 2.2. Deploying a RESTful Web Service)

When I deploy it and run it, I got this severe message: "SEVERE: The ResourceConfig instance does not contain any root resource classes."

This is because you have
instead of

The ShoppingService interface is in the package shoppingApp and your setup will look at the shoppingApp.impl package and all it's subpackages so it won't discover the ShoppingService interface.
Question: can I put those JAX-RS annotations in the interface and let the classes to inherit them?

Yes, you can (check JAX-RS v1.1 spec, 3.6 Annotation Inheritance)

Regards,
Frits
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 734
Hi, Frit. I followed your instruction to put the package of the interface in the web.xml.
I have a client like this:

It returns me HTTP response code 505, IOException in the BufferedReader line.

If I put all the @Path, @Get annotations in the class instead of the interface and change the web.xml back, it works.
I wonder why putting the annotations in interface does not work, but putting them in the class work.
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1660
    
  25

If I put all the @Path, @Get annotations in the class instead of the interface and change the web.xml back, it works.

That is odd, that should not make a difference...

By the way I miss something important in your client, the connection.connect() method (see URLConnection), like this:

And do you close() your BufferedReader at the end of the main method?

Regards,
Frits
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 734
Yes. I did close the reader stream at the end.
The strange thing is that it works when I put the annotation in the implementation class. But it doesn't when I put them in the interface implemented by the class.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 734
Hi, Frit.
I just notice a line in the tomcat server window:
SEVERE: this class shoppingApp is an interface and cannot be instantiated.
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1660
    
  25

SEVERE: this class shoppingApp is an interface and cannot be instantiated.

Oh, yeah that was it! Although not literally described you can get that from the examples in the JAX-RS v1.1. specs:


3.6 Annotation Inheritance
JAX-RS annotations MAY be used on the methods and method parameters of a super-class or an implemented interface. Such annotations are inherited by a corresponding sub-class or implementation class method provided that method and its parameters do not have any JAX-RS annotations of its own. Annotations on a super-class take precedence over those on an implemented interface. If a subclass or implementation method has any JAX-RS annotations then all of the annotations on the super class or interface method are ignored. E.g.:


In the above, ActivityLog.getFeed inherits the @GET and @Produces annotations from the interface

Conversely:


In the above, the @GET annotation on ReadOnlyAtomFeed.getFeed is not inherited by ActivityLog.getFeed and it would require its own request method designator since it redefines the @Produces annotation.

Regards,
Frits
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 734
Hi, Frit. I put the @Path in the implementation class. Now, it works. Also, just want to share with you my interesting finding. In the web.xml file, I put the param-value either the package name of the service interface or the package name of the service implementation class. Both works. But if I delete web.xml, it does not work. So, in this case web.xml is not optional.
For your information, here is what I did (only relevant part shown) for the web.xml

or
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1660
    
  25

So, in this case web.xml is not optional.

Ok, I now see that you are using Tomcat, as I was using Glassfish (with the Jersey framework JAX-RS Reference Implementation) where the web.xml is optional.

Thanks for sharing this info

Regards,
Frits
 
Don't get me started about those stupid light bulbs.
 
subject: Can we put @Path, @Get in interface instead of class ?