• Post Reply Bookmark Topic Watch Topic
  • New Topic

Expose REST web service and choose specific implementation.  RSS feed

 
Claude Moore
Ranch Hand
Posts: 862
8
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Java interface and two different implementations of it that are actually two EJBs. After annotating the interface with @Path and its methods with @Get and @Post annotations, I was able to call via browser exposed rest services without problems. Anyway, exposed implementation seems to be the first Ejb implementing my interfaces that the containers finds during JAX-RS scan phase. I did not find a way to tell the container to use a specific implementation.Is there a way to achieve this?
Thanks in advance!
 
Claude Moore
Ranch Hand
Posts: 862
8
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I found a viable solution, even if a bit tricky.

First, I have to take advantage of default methods in interfaces in Java 8. Then, I define my interface like the following one:



where return LookupHelper.getInstance().lookup(ICommon.class,"1.0"); uses an helper class which a) search for any EJB which implements ICommon interface b) returns a specific version of the implementation (versioning is implemented via a custom annotation, nothing really complicated)
I'm not really fond of this solution, but at least it works and I don't have to write wrapper classes.

UPDATE: Actually things are more tricky than I believed... I had to modify the interface by adding a local Wrapper, like this:



and register ICommon.RESTWrapper as resource in overridden Application.getClasses() method.


 
Claude Moore
Ranch Hand
Posts: 862
8
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here again.... only to share which I think is a good solution.

After reading in depth RestEasy documentation, eventually I found a way to achieve my goal - i.e, select which implementation to use when multiple implementations are available.

The key is to use REST sub resources locators, as explained here in official Java EE 6 documentation..
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!