How do I properly configure an EntityManager in a jersey / hk2 application?

One option is to instead of creating a new EntityManagerFactory in the EMFactory (which is in a request scope), you could create a singleton factory for the EntityManagerFactory, then just inject the EntityManagerFactory into the EMFactory. public class EMFFactory implements Factory<EntityManagerFactory> { private final EntityManagerFactory emf; public EMFFactory (){ emf = Persistence.createEntityManagerFactory(persistenceUnit); } public EntityManagerFactory … Read more

Listing all deployed rest endpoints (spring-boot, jersey)

Probably the best way to do this, is to use an ApplicationEventListener. From there you can listen for the “application finished initializing” event, and get the ResourceModel from the ApplicationEvent. The ResourceModel will have all the initialized Resources. Then you can traverse the Resource as others have mentioned. Below is an implementation. Some of the … Read more

Jersey 2.x Custom Injection Annotation With Attributes

Yeah Jersey made the creation of custom injections a bit more complicated in 2.x. There are a few main components to custom injection you need to know about with Jersey 2.x org.glassfish.hk2.api.Factory – Creates injectable objects/services org.glassfish.hk2.api.InjectionResolver – Used to create injection points for your own annotations. org.glassfish.jersey.server.spi.internal.ValueFactoryProvider – To provide parameter value injections. You … Read more

Migrate Jersey project to use Java 10 results in java.lang.IllegalArgumentException at jersey.repackaged.org.objectweb.asm.ClassReader.

tl;dr To use Java 10, switch to Jersey 2.27 (which is the latest as of this date (10/5/18)). java.lang.IllegalArgumentException at jersey.repackaged.org.objectweb.asm.ClassReader.<init> Jersey repackages asm and puts these class files into the jersey-server jar. Just digging through the jar in my IDE, I looked at the ClassReader constructor (that’s what <init> means) to see where IllegalArgumentException … Read more

Jersey returns 404 with any error status code?

The default behavior with Jersey, when there is an error status (4xx, 5xx), is to use the servlet’s Response.sendError, which results in a redirect to an error page. Since there is no error page set up, it results in a 404. We can change this behavior by setting the Jersey property ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR You can do … Read more

Jersey fails when creating uber jar with maven-assembly-plugin

If you look inside the MOXy jar, you will see a folder META-INF/services. In that folder, you will see a file named org.glassfish.jersey.internal.spi.AutoDiscoverable. The content of that file should be a single line org.glassfish.jersey.moxy.json.internal.MoxyJsonAutoDiscoverable What this file is for is to allow Jersey to discover the MoxyJsonAutoDiscoverable, which registers MOXy for Jersey. This service loader … Read more

How to inject an object into jersey request context?

You could just use ContainterRequestContext.setProperty(String, Object). Then just inject the ContainerRequestContext @Override public void filter(ContainerRequestContext crc) throws IOException { MyObject obj = new MyObject(); crc.setProperty(“myObject”, myObject); } @POST public Response getResponse(@Context ContainerRequestContext crc) { return Response.ok(crc.getProperty(“myObject”)).build(); } Another option to inject the MyObject directly is to use the HK2 functionality Jersey 2 offers. Create a … Read more