Autowire annotation in JAX-RS and Spring - spring

I am trying to add REST support to my Spring 3 + Hibernate application.
I have created the REST support using the wizard from Netbeans, at it has put a #Autowire annotation (not #autowired) above my Resource class. Getting the #Autowire annotation from Spring causes the error
incompatible types
found : org.springframework.beans.factory.annotation.Autowire
required: java.lang.annotation.Annotation
Getting #Autowire from JAX-RS should be only for Spring 2.5 as far as I understand from here. I get the following error if I include it, which I think is related to Spring 2.5 being loaded:
SEVERE: Exception while loading the app : java.lang.IllegalStateException:
ContainerBase.addChild: start: org.apache.catalina.LifecycleException:
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception
parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml];
nested exception is java.lang.NoSuchMethodError:
org.springframework.beans.MutablePropertyValues.add(Ljava/lang/String;Ljava/lang/
Object;)Lorg/springframework/beans/MutablePropertyValues;
Could someone point me on how to add this annotation, and make JAX-RS work with Spring? Also, I was using a SessionFactory and the autogenerated code refers to a entityManagerFactory in the applicationcontext. Can those be used interchangingly?
PS: Allow me to say that I hate Java EE with a passion so far in my three week journey with the platform, major stumbling blocks at every level, sorry for the rant.

You are using the wrong Autowire. The 'Netbeans REST wizard' looks like it would be using com.sun.jersey.api.spring.Autowire (last image, very bottom of page), and from your message above, you are using org.springframework.beans.factory.annotation.Autowire which appears to be an Enum used in Spring 2.0.
From what I can tell, this may be a specific thing to Spring 2.0 anyways. Perhaps you should take a look at doing the REST JAX-RS stuff yourself (using Jersey), as it is not that difficult.

Related

springboot app looking for GenericResponseService bean

I am new to springboot.
Doing a migration of my service (kotlin)following a guide written at work.
Got this weird exception and cannot find any documentation.
Parameter 3 of method multipleOpenApiResource in org.springdoc.webflux.core.MultipleOpenApiSupportConfiguration required a bean of type 'org.springdoc.core.GenericResponseService' that could not be found.
Action:
Consider defining a bean of type 'org.springdoc.core.GenericResponseService' in your configuration.
Should I define this bean at my #Configuration?
Is this a symptom of dependency missing or bad dependency wiring?
One of my beans was called ResponseBuilder and it conflicted with spring boot.
Sorry for the trouble

How to use both reactive and non reactive MongoDB template on Webflux Application

Currently, I'm developing a springboot webflux app for my project, where I use reactive-mongo-template and other reactive implementations. Now I have a legacy spring boot application that is implemented by WebMvc ( here i dont use the real springboot app. just using the core services implemented with mongoDB configurations) and it's using (normal) mongo-template for its implemented services. In my webflux app I'm trying to use the old services defined in the legacy application.
For that i tried importing the legacy app using component scan (com.a.b.cservice.core.configuration) and got some error. The problem is it wont detect the application correctly and the non-reactive mongo template.
Does the spring support only 1 mongo connection? These are the error logs i got;
[main] WARN o.s.b.w.r.c.AnnotationConfigReactiveWebServerApplicationContext - [] - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mapperDAOSpringConfigurations': Unsatisfied dependency expressed through field 'mongoTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoTemplate' defined in class path resource
[com/a/b/cservice/core/configuration/MongoConfigurations.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.core.MongoTemplate]: Factory method 'mongoTemplate' threw exception; nested exception is java.lang.NoSuchMethodError: com.mongodb.connection.ClusterSettings.getDescription()Ljava/lang/String;
***************************
APPLICATION FAILED TO START
***************************
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
com.mongodb.connection.DefaultClusterFactory.createCluster(DefaultClusterFactory.java:182)
The following method did not exist:
com.mongodb.connection.ClusterSettings.getDescription()Ljava/lang/String;
The method's class, com.mongodb.connection.ClusterSettings, is available from the following locations:
jar:file:/C:/Users/abc/.m2/repository/org/mongodb/mongodb-driver-core/4.0.5/mongodb-driver-core-4.0.5.jar!/com/mongodb/connection/ClusterSettings.class
jar:file:/C:/Users/abc/.m2/repository/org/mongodb/mongo-java-driver/3.12.7/mongo-java-driver-3.12.7.jar!/com/mongodb/connection/ClusterSettings.class
The class hierarchy was loaded from the following locations:
com.mongodb.connection.ClusterSettings: file:/C:/Users/abc/.m2/repository/org/mongodb/mongodb-driver-core/4.0.5/mongodb-driver-core-4.0.5.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of com.mongodb.connection.ClusterSettings
Are there any workaround to work the legacy app with the webflux application with mongoDb? Or do i need to keep it as a seperate micro-service and call the relevent endpoints?
after a few works and searches, I found the issue is not related to Webflux. purely a mongo client collision between legacy application and webflux drivers. so I exclude the drivers from legacy and imported the spring-boot-starter-data-mongodb along with the reactive spring-boot-starter-data-mongodb-reactive version. (both are using the same version 2.3.5.RELEASE).
ps: found the same issue on here Spring boot 2.0.5.RELEASE and mongo 4.0 connection issues

Spring4.3.0 integration with Hiberbnate 5

In Hibernate 5 bootstrapping api is changed by introducing BootStrapServiceRegistry class, now how can I bootstrap the sessionFactoryBean from spring context.xml file . Please provide me on approaches for xml bootstrapping rather than programmatic way that's explained in docs.
Currently we have spring 4 and hibernate 4 integration in the context.xml and we are planning to migrate to Hibernate 5. We are facing challenges with LocalSessionFactoryBean, getting No qualifying bean exceptions for the serices, which is thrown by another exception like no qualifying bean JtaTransaction Manager

Integration Akka with Spring and OSGI (No configuration setting found for key 'akka')

I'v added one Akka actor to my app that works with Spring and OSGI.
I try to use Actor from Spring #Component bean like this:
private final ActorSystem system = ActorSystem.create("actor-system");
private ActorRef managerActorRef = system.actorOf(Props.create(ManagerActor.class), "ldapManagerActor");
When I start the app it throws an exception (No configuration setting found for key 'akka'):
Instantiation of bean failed; nested exception is org.springframework.
beans.BeanInstantiationException: Could not instantiate bean class [com.myconpany....ByBean]: Constructor threw exception; nested exception is com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'akka'
I've look at this: doc. And seems the root of my problem is related to class loader that I should pass to/for akka-system and application.conf file that describes this.
But I could not find appropriate stets to make it all working so far.
Could someone help?
My tries:
Flowing this article.
When I put:
<bean id="actorSystem" class="akka.actor.ActorSystem" factory-method="create" scope="singleton"></bean>
I have the similar error:
Could not autowire field: private akka.actor.ActorSystem
com.typesafe.config.ConfigException$Missing: No configuration setting fou
nd for key 'akka'
If you just want to use Akka actors you don't need to pass in application.conf settings. You can just use the default. Make sure you have the config library on your class path.
You didn't mention what version of Akka youre using, however if you grab the latest Akka 2.2 RC you can use an IndirectActorProducer to handle the spring wiring.
See this article on how to do that; http://blog.nemccarthy.me/?p=272
You should also tell Spring to call shutdown on the ActorSystem to cleanly shutdown. There is also an updated guide on how to integrate the ActorSystem into Spring or a web app here

Spring, using new ClassPathXmlApplicationContext and getting error being unable to find applicationContext.xml and others?

I am trying to follow this tutorial: http://www.vogella.de/articles/SpringDependencyInjection/article.html to use annotation dependency injection in my application. I set up the bean, etc like in the tutorial and then am trying to get an instance of the bean within my MainController class (a controller class that handles generating a specific page for my spring web mvc app).. I keep getting
SEVERE: Servlet.service() for servlet spring threw exception
java.io.FileNotFoundException: class path resource [WEB-INF/applicationContext.xml] cannot be opened because it does not exist
I am doing this in my MainController:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
BeanFactory factory = context;
BeanIRPlus beanirPlus = (BeanIRPlus) factory
.getBean("BeanIRPlus");
IRPlusInterface irPlus = beanirPlus.getIRPlus();
I have searched and searched on this and yet to find an answer that fixes my problem. My applicationContext in in webapp/WEB-INF/ and my spring app seems to be working otherwise as it was handling requests, etc before this. I have tried putting the applicationContext.xml in WEB-INF classes but still nothing. Is there any workaround to make this not search the path this way as I think its doing a relative path search. Thanks for any advice
Not a direct answer, but here goes.
The tutorial you have referred is for dependency injection in a standalone application and not a web application. In case of web application, spring automatically loads the context files and initializes the beans. So you would not need any of the lines specified in the MainController.
Instead, you could do something like this to use beanIRPlus bean in your controller.
#Autowired
private BeanIRPlus beanIRPlus;

Resources