Class 'org.springframework.http.converter.json.MappingJackson2HttpMessageConverter' not found - spring

I am getting the following error in the servlet-context.xml. I am following the following tutorial to get this done.
Multiple annotations found at this line:
- Class 'org.springframework.http.converter.json.MappingJackson2HttpMessageConverter'
not found
- Class 'org.springframework.http.converter.json.MappingJackson2HttpMessageConverter'
not found [config set: myapp/web- context]
<!-- Configure bean to convert JSON to POJO and vice versa -->
<beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</beans:bean>

you have to add spring-web.jar in your classpath or if you are using maven then you should add the following dependency:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>X.Y.Z.RELEASE</version>
</dependency>

Related

Spring Session JdbcHttpSessionConfiguration causes JPA bean not to start

I am trying to use Spring Session with PostgreSQL for storing http sessions.Followed https://docs.spring.io/spring-session/docs/2.0.4.RELEASE/reference/html5/guides/xml-jdbc.html#httpsession-jdbc-xml-sample
Here is my XML
<context:annotation-config/>
<bean class="org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver"></property>
<property name="url" value="jdbc:postgresql://127.0.0.1:5432/myapp"></property>
<property name="username" value="postgres"></property>
<property name="password" value=""></property>
</bean>
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<constructor-arg ref="dataSource"/>
My application is already using Spring JPA for other DB operations. But Problem is after I added
<context:annotation-config/>
<bean class="org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration"/>
my JPA beans are not loaded on the startup and hence application is not getting started.
I tried following steps.
Created two data sources beans. One was already there for JPA and one as shown in the XML with id 'dataSource'. JPA one is also with same bean properties but with different id.
Removed one in XML and renamed JPA one also to 'dataSource' as I read JdbcHttpSessionConfiguration looks for bean 'dataSource' and by assuming both JPA and session JDBC will use the same one
But both the cases result in the same error.
I was not getting any error in catalina.out. So put try catch in my application where it tries to get the JPA bean. I am getting the following error.
Cannot find class [org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration] for bean
with name 'org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration#0'
defined in class path resource [applicationContext_Web.xml]; nested exception is java.lang.ClassNotFoundException:
org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration
Dependency added in pom
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-jdbc</artifactId>
<version>2.0.4.RELEASE</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
I could solve it . Somehow maven was still picking spring-session-core jar instead of spring-session-jdbc. I just removed pom and again did a maven update. Not sure how it took the correct one then.

java.lang.NoClassDefFoundError: org/apache/cxf/jaxrs/model/doc/DocumentationProvider

I am trying to integrate swagger. I am following steps as given here. application context and pom.xml is configured same.
when restarting tomcat getting this error.
2018-04-30 14:01:33 ERROR ContextLoader:351 - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productAPIServer': Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/apache/cxf/jaxrs/model/doc/DocumentationProvider
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583)
I have included
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-service-description-swagger</artifactId>
<version>3.1.7</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-service-description</artifactId>
<version>3.1.7</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.1.7</version>
</dependency>
Changes in context xml file:
<bean id="jacksonJsonProviderFasterxml" class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider" />
<bean id="swagger2Feature" class="org.apache.cxf.jaxrs.swagger.Swagger2Feature">
<property name="basePath" value="/staticdata-web-service"/>
<property name="license" value=""/>
<property name="licenseUrl" value=""/>
<property name="title" value="Static data web service"/>
</bean>
Added feature
<jaxrs:features>
<ref bean="swagger2Feature" />
</jaxrs:features>
You have a version mismatch. When you search on Maven for that class, you'll find that the package of the DocumentationProvider has changed from version 3.0.x to 3.1.x
org/apache/cxf/jaxrs/model/doc/DocumentationProvider
to
org/apache/cxf/jaxrs/model/wadl/DocumentationProvider
It's hard to tell where the mismatch is located without having insight into your entire build and package dependencies. But you may want to run
mvn dependency:tree -Dverbose
in your project folder to see which package versions come into question for raising your ClassNotFoundException.
use below dependency in your maven project to solve your issue,
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-rs-json-basic -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-json-basic</artifactId>
<version>3.4.3</version>
<scope>provided</scope>
</dependency>

java.lang.NoClassDefFoundError:org/apache/commons/fileupload/FileItemFactoryin Spring MVC

I am trying to build a simple Spring MVC Web App with the functionality of file uploading.I got following error:
java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory
After a quick search,all answers pointed to the missing of dependencies,but it seems not to be true in my case:
I have included the following code in pom.xml:
<dependencies>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
with commons.io-2.4.jar and commons.fileupload-1.3.1.jar added into lib folder.
One interesting thing I found was that whenever I deleted the code:
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize">
<value>10000000</value>
</property>
<property name="maxInMemorySize">
<value>10000000</value>
</property>
</bean>
the web app works fine(of course I removed the form for file uploading as well. )
If you visit the Maven Central Repository and enter the search term:
fc:org.apache.commons.fileupload.FileItemFactory
then every released artifact available containing that class will be listed.
You will find commons-upload 1.3.1 in that list.
Therefore you need to double check your deployment to ensure that jar is present.
Tip: Use fc: to locate jars in Maven Central that contain a specific class.
try to update your jar version, for Example:
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>2.0.0-SNAPSHOT</version>
</dependency>
We cann't find versions upper 1.3 on Apache and mvnrepository.com , but you can try this : https://repository.jboss.org/commons-fileupload/commons-fileupload/2.0.0-SNAPSHOT/
In fact, I encountered the same problem and now working fine with version 2.0 under SpringMVC framework.

getting error java.lang.NoClassDefFoundError: org/hibernate/validator/resourceloading/ResourceBundleLocator

I have a spring project in which i am using a validator like the following:
<beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
p:basenames="WEB-INF/i18n/messages,WEB-INF/i18n/application"
p:fallbackToSystemLocale="false" />
<beans:bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<beans:property name="validationMessageSource" ref="messageSource" />
</beans:bean>
<annotation-driven validator="validator" />
<resources location="/, classpath:/META-INF/web-resources/" mapping="/resources/**" />
When i run the project i get the following BeanCreationException:
org.springframework.beans.factory.BeanCreationException: Error creating bean with
name 'validator' defined in ServletContext resource [/WEB-INF/spring/appServlet
/servlet-context.xml]: Error setting property values; nested exception is
org.springframework.beans.PropertyBatchUpdateException; nested
PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException:
Property 'validationMessageSource' threw exception; nested exception is
java.lang.NoClassDefFoundError: org/hibernate/validator/resourceloading/ResourceBundleLocator
Here is my snippet of POM:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.3.2.RELEASE</version>
</dependency>
<!-- Hibernate entity manager with JPA 2 support. -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.0.Beta2</version>
</dependency>
<!-- Hibernate’s implementation of JSR-303. -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
</dependency>
<!-- The JSR-303 Bean Validation API library. -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
Why i am getting this error?
Thanks
Your dependencies are OK. Based on the provided information the error should not appear.
So I suggest you to check your IDE and whether the hibernate-validator dependency is really on classpath. If you are using Eclipse with M2E, try to update your project:
Project (right click on project) > Maven > Update Project....
This might be a broken backwards compatibility issue: https://jira.springsource.org/browse/SPR-10466
I had a similar problem using hibernate 5.2.10.Final and hibernate-validator 6.0.2.Final (that package version doesn't seem to be in sync with the others). Turns out, hibernate-validator's groupId was changed from org.hibernate to org.hibernate.validator. Once I made that change, the error went away, however I am using Dropwizard and noticed that there may be a version conflict somewhere, so that's something to keep an eye on.
I also had this exact same problem, tried all sorts of combinations of JAR versions, different bean definitions, even customised class implementations. Turns out, I just had to clean the project and restart Eclipse. Problem solved, and lesson learnt!
Check your dependency
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.2.8.Final</version>
</dependency>
Is must be at the same version of your other Hibernate dependencies.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
Just use following dependency, it will fix your issue as this dependency provides basic validation from get go similar to hibernate.

Missing Spring AOP libraries in STS

I'm getting my feet wet with Spring. I downloaded STS and I'm following basic examples from Spring in Action Second Edition. I'm stuck when trying to implement basic AOP and I guess I'm just missing some specific libraries in my project.
I say so because annotations like #Aspect are not recognized in my classes like also <aop:config> in my xml.
This are my Maven Dependencies:
junit-4.7.jar
spring-test-3.0.2.RELEASE.jar
spring-context-3.0.2.RELEASE.jar
spring-aop-3.0.2.RELEASE.jar
aopalliance-1.0.jar
spring-beans-3.0.2.RELEASE.jar
spring-core-3.0.2.RELEASE.jar
commons-logging-1.1.1.jar
spring-expression-3.0.2.RELEASE.jar
spring-asm-3.0.2.RELEASE.jar
log4j-1.2.14.jar
Please let me know what libraries I'm missing and where to find them.
Thank you!
EDIT:
The following:
<bean id="performancePointcut"
class="org.springframework.aop.aspectj.AspectJExpressionPointcut" >
<property name="expression" value="execution(* Performer+.perform(..))" />
</bean>
throws the following exception:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'performancePointcut' defined in file [C:\Users\Prova\Documents\STS\SpringIdol3\src\main\resources\META-INF\spring\spring-idol.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException
DONE!
This aspectj-annotation-tutorial did the job with steps 1, 2, and 3.
It's been a fun Friday night....
Put these two dependencies in your pom.xml:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency>
you may add maven dependencies:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>

Resources