How to use CDI Events in Java Jersey? - java

I am trying to follow this post's upvoted answer: Best practice for REST token-based authentication with JAX-RS and Jersey
I'm using the CDI approach and in my filter i have the definition:
#Inject
#AuthenticatedUser
Event<String> userAuthenticatedEvent;
But, when I run tomcat 7, I get the following error:
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=Event<String>,parent=AuthenticationFilter,qualifiers={#com.wink.rest.security.annotation.AuthenticatedUser()},position=-1,optional=false,self=false,unqualified=null,567185196)
at org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:75)
at org.jvnet.hk2.internal.Utilities.justInject(Utilities.java:945)
at org.jvnet.hk2.internal.ServiceLocatorImpl.inject(ServiceLocatorImpl.java:979)
at org.glassfish.jersey.ext.cdi1x.internal.AbstractCdiBeanHk2Factory$2.getInstance(AbstractCdiBeanHk2Factory.java:142)
at org.glassfish.jersey.ext.cdi1x.internal.AbstractCdiBeanHk2Factory._provide(AbstractCdiBeanHk2Factory.java:91)
at org.glassfish.jersey.ext.cdi1x.internal.GenericCdiBeanHk2Factory.provide(GenericCdiBeanHk2Factory.java:63)
I have tried everything I found on google and stackoverflow to try to resolve this, but none of the solutions worked. So now I'm not sure what else to do. Anyone have any advice?

After struggling with the same exact issue I found a solution to that. I switched from TomCat to Glassfish and updated maven dependencies to match the ones from official Jersey 2 documentation. After that it worked like magic.

Related

How to get Camel PgEvent component to use DataSource properly in Java Spring

I am using Apache Camel on Spring Boot and want a route to listen to PostgreSQL notify calls.
Camel version: 3.4.4
Spring Boot version: 2.3.3
I have tried using the PgEvent component from Camel, but have found the documentation quite lacking when you want to use a DataSource object to fill in the database specifics (i.e. url, username, password). I have neither found any examples of working solutions in stackoverflow, nor around the web.
Below are some sources I found helpful:
How to receive PostgreSQL LISTEN/NOTIFY with Apache Camel?
https://impossibl.github.io/pgjdbc-ng/docs/current/user-guide/
In the end, the "normal" jdbc:postgresql:/... url I was using did not work. So I followed the linked SO-answer, suggesting using the pgjdbc-ng driver (my colleague suggested the same). Following the documentation of the second link I found that my Camel route started working. All I needed to do was change the previous DataSource url to jdbc:pgsql:/....
Hope this helps other people with similar problems. Wasted all too many hours on this little problem.
I'm copying my own solution from the question into the answer section, as it may help others find the solution when the question is marked solved. Below is the copy of the final solution:
In the end, the "normal" jdbc:postgresql:/... url I was using did not
work. So I followed the linked SO-answer, suggesting using the
pgjdbc-ng driver (my colleague suggested the same). Following the
documentation of the second link I found that my Camel route started
working. All I needed to do was change the previous DataSource url to
jdbc:pgsql:/....

Fail to replace kubernetes statefulsets using fabric8 maven plugin

We use fabric8-maven-plugin to manage the full lifecycle of all of our kubernetes services. We are having a problem upgrading statefulsets (similar to this post but we dont use the kubernetes-client). The version of the maven plugin is 4.3.1. We have no issues with daemonsets but I just cant find a way to update statefulsets. We get the same error as the afore mentioned post.
Forbidden: updates to statefulset spec for fields other than 'replicas', 'template', and 'updateStrategy' are forbidden..
I have tried to set the updateStrategy to rolling but that doesn't seem to do anything.
Thanks Paul

Java service using Jersey won't deploy to Jboss

I'm trying to build a Java service that other services could call.
This service is not a WS, but is calling a RestfulWS.
I'm really just building a wrapper around this call. This would find the correct data it needs, set up the JSON for the call. Get a response and send it back up.
Was told to use Jersey for this. Trying to set up all the pom.xml to use Jersey.
Building code works fine, it is when the deploy to the server happens that things fail.
I get the error -- "JBAS011232: Only one JAX-RS Application Class allowed. "
I don't have a web.xml, which I guess is used to skip some ResetEasy files.
I do have exclusions in pom.xml and jboss-deployment-structure.xml.
I still get the error when deploy happens. Not really sure what else to check.
It looks like you have a problem with JAX-RS dependencies. JBoss already has its own implementation of JAX-RS and probably that’s causing the issue. Some solutions are already suggested here Jboss error: Only one JAX-RS Application Class allowed

Jersey HK2 Dependency Injection doesn't work after update to v2.27

I have a project using Jersey v2.25.1. I was using Jersey's inbuilt HK2 injection to perform dependency injection, and everything worked fine. Fast forward to now, I decided to update to Jersey v2.27.
When I ran my project, I got the following exception:
java.lang.IllegalStateException: InjectionManagerFactory not found
After some googling, I found that I needed to add the jersey-hk2 dependency. Doing so made me get the following exception:
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=<MyClass>,parent=<MyClass>,qualifiers={},position=0,optional=false,self=false,unqualified=null,1044705957)
Upon reverting all my dependencies to Jersey v2.25.1, everything works fine. What do I need to do to fix these errors, so I can use Jersey v2.27?
Edit:
I don't use Maven so I can't really post a pom.xml, but tommorow I will put together an MVCE with the exact dependencies I have, and a basic example of Dependency Injection.
Answer by Paul Samsotha in a comment:
Try to change your AbstractBinder import. There are two, a Jersey one and an HK2 one. Try to use the Jersey one.
Basically, I needed to change the AbstractBinder class I implemented from
org.glassfish.hk2.utilities.binding.AbstractBinder
to
org.glassfish.jersey.internal.inject.AbstractBinder
The difference is that Jersey decoupled HK2 from it's internal DI mechanism in version 2.26, and thus, I needed to use the new AbstractBinder import, which comes directly from Jersey, and not HK2.
There are a few API differences: for instance, instead of a Factory<T>, bindFactory() takes a java.util.function.Supplier<T>.

ResteasyProviderFactory exception in Glassfish

I'm having some trouble invoking a RESTful webservice using the Client Framework provided by RESTEasy. When I try to register the ResteasyProviderFactory I got a ClassCastException and nothing more works.
Here's the code:
RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
LifeGoalClient leClient = ProxyFactory.create(LifeGoalClient.class, "http://localhost:8080/TutorFinanceiro");
List<LifeGoal> lifeGoals = leClient.getLifeGoals();
JOptionPane.showMessageDialog(null, lifeGoals);
return lifeGoals;
And the exception:
java.lang.ClassCastException: com.sun.jersey.server.impl.provider.RuntimeDelegateImpl cannot be cast to org.jboss.resteasy.spi.ResteasyProviderFactory
I'm using Glassfish 3.0.1 and Resteasy 2.2.1.
I searched through the web but found no solution or no relevant info in respect to this.
If anyone have a solution or an alternative way of doing this, please help me!
Thanks in advance
I did force resteasy over jersey in glassfish by adding a file named javax.ws.rs.ext.RuntimeDelegate in the class path at "META-INF/services", wich contain only the following line :
org.jboss.resteasy.spi.ResteasyProviderFactory
However, for me, all seemed to work fine until I try to use resteasy-cdi. Adding that last artifact to my dependencies and configuring the required context-param did dig up the same classcast problem.
This is caused by conflicting jax-rs implementation - jersey and resteasy.
In order to prevent the error above, you can add
<context-param>
<param-name>resteasy.use.deployment.sensitive.factory</param-name>
<param-value>false</param-value>
</context-param>
in your web.xml.
By doing so, resteasy will choose instance of ResteasyProviderFactory.

Categories