How to comment on JAX-RS on an interface when using a jersey

This question has been asked several times before , however the answers do not seem to work, and / or Jersey moved on with new changes.

I am looking through some REST APIs using JAX-RS and Jersey (version 2.24). And I want to annotate the interface with JAX-RS and the specific implementation (without any annotations). However, since this patch , Jersey has ceased to support this opportunity. As far as I understand spec , it does not strictly prohibit doing this.

If a subclass or implementation method has any JAX-RS annotations, then all annotations in the superclass or interface method are ignored.

implying that this is quite normal. In many cases, it is useful to use an interface, and each server and client has their respective implementations.

There are many solutions

  • Use ResourceConfig and execute registerClasses(MyImplementation.class) . However, this does not work.
  • Disable the package scan configuration in web.xml , create a custom javax.ws.rs.Application and execute the register your implementation. Does not work.
  • use ResourceConfig and define a custom AbstractBinder and do a bind so that Jersey dependency injection can find specific implementations. Does not work.
  • Use RESTEasy. RESTEasy does not seem to introduce interface restrictions, as in Jersey. Never tried it myself.

I would appreciate it if someone could share their experience with this. Any help on getting a Jersey job would be great too. As for option (4), do I really need to switch? Sample code below.

MyResource

  package com.foo; import javax.ws.rs.GET; import javax.ws.rs.Path; @Path("/hello") public interface MyResource { @GET public String sayHello(); } 

MyResourceImpl

 package com.bar; public class MyResourceImpl implements MyResource { @Override public String sayHello() { return "Hello Jersey"; } } 

There is also web.xml in which packet scanning is allowed to scan com.foo

+7
source share
2 answers

In Jersey, we need to put the level @Path class on an implementation instead of an interface.

com.foo package;

 import javax.ws.rs.GET; import javax.ws.rs.Path; @Path("/hello") public interface MyResource { @GET public String sayHello(); } MyResourceImpl package com.bar; @Path("/hello") public class MyResourceImpl implements MyResource { @Override public String sayHello() { return "Hello Jersey"; } } 
0
source

If you want to separate the resource interface from the implementation (allowing you to use the interface with some REST client, such as the resteasy client), you can use @RequestScoped for the implementation. Thus, this component can use embedded resources such as EJB, EntityManager, ... Using your sample:

 import javax.ws.rs.GET; import javax.ws.rs.Path; @Path("/hello") public interface MyResource { @GET public String sayHello(); } 

MyresourceImpl

 package com.bar; @RequestScoped public class MyResourceImpl implements MyResource { @Override public String sayHello() { return "Hello Jersey"; } } 

However, you should take into account that as soon as you use certain JAX-RS classes in your implementation code (e.g. UriInfo, Response object, ...), you will create a connection between your implementation and the JAX-RS API.

0
source

Source: https://habr.com/ru/post/1260549/


All Articles