MappingJacksonHttpMessageConverter not found with Spring4 - java

I migrated my Spring framework from 3.x to 4.2.RELEASE but, when I start the jUnit I'm getting this error:
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from URL [file:src/test/resources/applicationContext.xml]; nested exception is java.lang.NoClassDefFoundError: org/springframework/http/converter/json/MappingJacksonHttpMessageConverter
I read on internet from Version 4.1. it's not supported anymore; I checked and the new version is available on application class path (I imported spring-web dependency). ( Spring 4 and Rest WS integration )
It seems spring is still looking for the old version of Converter. It probably depends on something I've on my application context, but, the question is, how can I "tell to Spring" to use new version?
-- UPDATE
I commented
<mvc:annotation-driven />
and it seems to works fine but... why?

There's a newer version of that class in Spring 4: use org.springframework.http.converter.json.MappingJackson2HttpMessageConverter (note the '2').
In my REST servlet's Spring config file I have it configured as follows (I added my custom objectMapper to it, but you can just omit that):
<mvc:annotation-driven>
<mvc:message-converters>
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="s3.util.json.CustomJsonMapper" />
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>

Related

How to use class which is not a dependency in my spring boot project

In my spring project I can defined bean in XML like this:
<bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">
<property name="messageFactory">
<bean class="weblogic.xml.saaj.MessageFactoryImpl" />
</property>
</bean>
even my project does not contain weblogic.xml.saaj.MessageFactoryImpl class. When I deploy my application to wls server everything works fine just STS is complaining that it doesn't know this class. How I can convert this code to spring-boot application and define this factory in some java class ?
In a configuration class (has #Configuration above class definition) create a method as a factory for your MessageFactoryImpl and use #Bean above it. Defining a bean in spring boot is like the old java configuration.
See This question , It will help
weblogic.xml.saaj.MessageFactoryImpl is an implementation class which exists already in WebLogic server library jars.
To do that using #Bean, use #ConditionOnClass
#Bean
#Primary
#ConditionalOnClass(weblogic.xml.saaj.MessageFactoryImpl.class)
public SoapMessageFactory messageFactory() {
return new weblogic.xml.saaj.MessageFactoryImpl();
}

Full Java Config of app-context.xml including for example jdbc and tx directives

I am using spring 4.2.1 and wanted to move away from xml configs in all their flavours. In this case I want to get rid of my app-config.xml but am finding it hard to find information about this when it comes to all but the most trivial entries (#Configuration and #Bean). My old xml config class has entries like:
<jdbc:embedded-database id="dataSource" type="H2" />
<tx:annotation-driven transaction-manager="transactionManager" />
<jpa:repositories base-package="de.dahi.resourcecal.repos"/>
How can I do those in java config? I found an old spring-javaconfig from 2008 and also a comment from the old project lead that it had been merged into core and didn't apply any more. But the question is how do I do things like that in java now?
I found the following blog entry (http://automateddeveloper.blogspot.co.uk/2014/02/spring-4-xml-to-annotation-configuration.html) for the same with spring-security and also .config packages in the respective spring jars (org.springframework.transaction.config) but no info on how to use anything in there.
Take a look at the 2.1.2 Annotation based configuration section in the docs. It has an example of annotation-based configuration for the tags you include in your question. You can set the basePackage for the jpa repository using the value property of the #EnableJpaRepositories annotation - see the javadoc.
The EmbeddedDataBaseBuilder can handle H2 as well, for example:
#Bean
public DataSource dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
return builder.setType(EmbeddedDatabaseType.H2).build();
}

Spring BeanCreationException due to NoSuchMethodError in Property class

I am currently trying to start up a server side of an application, built with Maven; the clean install is succesful, but then tomcat7:run fails with the following Spring error message
04:21:19,059 [localhost-startStop-1] ERROR org.springframework.web.context.ContextLoader -
Context initialization failed org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'properties' defined in class path resource [context.xml]:
Initialization of bean failed; nested exception is java.lang.NoSuchMethodError:
org.springframework.core.convert.Property.<init>(Ljava/lang/Class;Ljava/lang/reflect/Method;
Ljava/lang/reflect/Method;Ljava/lang/String;)V
The concerned bean definition in context.xml is as follows
<bean id="properties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:server.properties"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
Research shows that these kind of errors are usually related to incorrect JAR versions on the classpath. In this particular application, Spring version 3.2.4.RELEASE dependencies are defined in Maven pom.xml, and checking out the API of the concerned org.springframework.core.convert.Property class it seems that constructor Property(Class objectType, Method readMethod, Method writeMethod, String name) is actually available in this version. Any ideas are appreciated
Try to run your java with -verbose option.
It shows wich jar has been loaded class from.
Looks like there is a spring jar in the bootstrap classloader of your Tomcat - older version of Spring.

Springframework beans

I have not until now had worked with spring framework. I tried reading and reading the spring documentation but could not locate the answer to the following simple question.
The application is ant built to a build directory. So far when I tried to start the JBOSS (or apache) server, the logs says it is unable to build the bean because it could not find
var/config/madagascar.prop.
.
WRT the following snippet in WEB-INF/applicationContext.xml
<bean id="application.home" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="java.lang.System"/>
<property name="targetMethod" value="getProperty"/>
<property name="arguments">
<list>
<value>application.home</value>
</list>
</property>
</bean>
<bean id="propertyFile" class="java.io.File">
<constructor-arg ref="application.home"/>
<constructor-arg value="var/config/madagascar.prop"/>
</bean>
Could you tell me where {application.home} and var/config are expected to be located?
Is it relative to the Eclipse project home or relative to WEB-INF?
What is the bean id="application.home" attempting to do - is it trying to read the value for "application.home" from the system env?
"application.home" is the runtime value of System.getProperty("application.home") and the "propertyFile" bean is a java.io.File object returned from calling new java.io.File(String, String) with the whatever application.home is set to and that second String.
If the bean you wanted was 'propertyFile', the runtime equivalent would be:
File file = new File(System.getProperty("application.home"),"var/config/madagascar.prop");
That config is a very Spring 1.x (read old) way of doing things. XML is a very kludgy way to perform many of these types of initialization, and that's one of the reasons that Spring's #JavaConfig approach became so popular.

spring ide and ref hyperlink click doesn't work if bean is defined in different file

when I have a bean
<bean name="myBean" class="mypackage.myBean">
<property name="otherBean" ref="otherBeanRef" />
</bean>
and I click on otherBeanRef I'm redirected to definition of otherBeanRef, however this only works if its in the same file.
how to configure spring ide to also support other spring files?
You need to add both files to the Spring Spring Config File Set.
Spring Explorer/Properties/Config Sets

Categories