Cannot find class [org.springframework.http.converter.json.Jackson2ObjMapperFactoryBean] - spring

I am working on a simple demo project and after fixing multiple erroes now i am stuck here,
bean declaration couldn't find class org.springframework.http.converter.json.Jackson2ObjMapperFactoryBean .I couldn't make head or tail of the error as i see all declared properly. May be someone experienced can point out the mistake here.
A detailed explanation would be nice.
i am using spring 5.3.13
java 11
tomcat 9
Error :
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/dispatcherServlet-servlet.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: No persistence units parsed from {classpath*:META-INF/persistence.xml}
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.http.converter.json.Jackson2ObjMapperFactoryBean] for bean with name 'jsonMapper' defined in ServletContext resource [/WEB-INF/dispatcherServlet-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.http.converter.json.Jackson2ObjMapperFactoryBean
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.http.converter.json.Jackson2ObjMapperFactoryBean] for bean with name 'xmlMapper' defined in ServletContext resource [/WEB-INF/dispatcherServlet-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.http.converter.json.Jackson2ObjMapperFactoryBean
dispatcherServlet-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/sprin-jpa-1.8.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- xmlns:repository="http://www.springframework.org/schema/data/repository" -->
<!-- http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository.xsd -->
<context:component-scan base-package="org.charlie" />
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="jsonMapper"></property>
</bean>
<bean class="org.springframework.http.converter.xml.MappingJackson2xmlHttpMesssageConverter">
<property name="objectMapper" ref="xmlMapper" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="jsonMapper" class="org.springframework.http.converter.json.Jackson2ObjMapperFactoryBean">
<property name="simpleDateFormat" value="yyyy-MM-dd HH:mm:ss" />
</bean>
<bean id="xmlMapper" parent="jsonMapper">
<property name="createXmlMapper" value="true"/>
</bean>
<mvc:resources mapping="/webjars/**" location="classpath:META-INF/resources/webjars/" />
<jpa:repositories base-package="org.charlie.repository" />
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:META-INF/sql/schema.sql" />
<jdbc:script location="classpath:META-INF/sql/data.sql" />
</jdbc:embedded-database>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="h2WebServer" class="org.h2.tools.Server" factory-method="createWebServer" init-method="start" destroy-method="stop">
<constructor-arg value="-web,-webAllowOthers,-webDaemon,-webPort,8082" />
</bean>
</beans>
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-isntance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="toDo">
<description>My Persistence Unit</description>
</persistence-unit>
</persistence>

It seems there is a typo. It should be Jackson2ObjectMapperFactoryBean instead of Jackson2ObjMapperFactoryBean.

Related

Getting error for EntityManagerFactory bean creation: com.sun.proxy.$ProxyXXX cannot be cast to javax.persistence.EntityManagerFactory

Trying to build simple spring integration app but it's failing for "emf" bean creation from following xml configuration file.
I've tried java configuration mentioned in Spring JPA - org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean cannot be cast to javax.persistence.EntityManagerFactory but still getting the same error.
Using following spring versions:
spring 5.3.18,
spring-integration 5.4.13,
hibernate 5.4.12.Final
XML Configuration:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="emf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="someDataSource" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<property name="persistenceUnitName" value="XXX" />
<property name="persistenceUnitManager" ref="persistenceUnitManager" />
</bean>
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf" />
</bean>
<bean id="persistenceUnitManager"
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="defaultDataSource" ref="someDataSource" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="someDataSource"/>
</bean>
<jee:jndi-lookup id="someDataSource" jndi-name="Da:/someDS" />
</beans>

Spring data jpa + Spring Transaction for multiple database

I am implementing multiple transaction(database) for my service using spring data jpa with #Transaction, having two persistent Unit names. And deploying in Jboss 6.4 EAP
spring xml includes
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.1.xsd">
<context:annotation-config />
<tx:annotation-driven />
<context:component-scan base-package="com.test.demo.domain.service" />
<context:component-scan base-package="com.test.demo.domain" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="demo-domain" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean>
</property>
<property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence" />
</bean>
<bean id="entityManagerFactoryMeta"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="demo-meta" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean>
</property>
<property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence" />
</bean>
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<qualifier value="demoadmin"/>
</bean>
<bean id="transactionManagerMeta" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactoryMeta" />
<qualifier value="demoappmeta"/>
</bean>
<jpa:repositories base-package="com.test.demo.domain.repository" />
</beans>
Below are my service details
#Named(ServerConstants.SERVICE_DRAG_DROP)
**#Transactional("demoappmeta")**
public class DragDropService {
private static final Logger LOG = LoggerFactory.getLoggerFactory().getDomainLogger(ServerConstants.LOGGER_DOMAIN, DragDropService.class.toString());
#Inject
private TbAsmiDragDropRepository dragDropRepo;
public void createDragDrop(Message pMessage){
JSONObject requestJson = pMessage.getRequestObject().getRequestJson();
LOG.debug("inside createDragDrop() - Request : "+requestJson);
In Jboss I am unable to deploy it, I get the following exception:
Caused by: org.springframework.beans.factory.BeanCreationException:
Could not autowire field: private
com.test.demo.domain.repository.TbAsmiDragDropRepository
com.test.demo.domain.service.DragDropService.dragDropRepo; nested
exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'tbAsmiDragDropRepository': Invocation
of init method failed; nested exception is
java.lang.IllegalArgumentException: Not an managed type: class
com.test.demo.domain.entity.TbAsmiDragDrop
If anything is missing or something is wrong, please advise
I found the solution just we have to set the attribute entity-manager-factory-ref="entitymanagerfactoryrefname" in
<jpa:repositories base-package="com.test.demo.domain.repository" entity-manager-factory-ref="entitymanagerfactoryrefname" />

Conflicting persistence unit definitions exception

I am getting the following exception on server startup. I have just one persistence.xml in my project. Any ideas?
weblogic.application.ModuleException:
at weblogic.servlet.internal.WebAppModule.startContexts(WebAppModule.java:1520)
at weblogic.servlet.internal.WebAppModule.start(WebAppModule.java:484)
at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:425)
at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:52)
at weblogic.application.internal.flow.ModuleStateDriver.start(ModuleStateDriver.java:119)
Truncated. see log file for complete stacktrace
Caused By: java.lang.IllegalStateException: Conflicting persistence unit definitions for name 'TRAVEL_SYSTEM_UNIT': file:/D:/TravelSystem/workspace-sts/Travel_System-EAR/EarContent/APP-INF/classes/, file:/D:/TravelSystem/workspace-sts/Travel_System-dataaccess/target/classes/
at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.preparePersistenceUnitInfos(DefaultPersistenceUnitManager.java:362)
at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.afterPropertiesSet(DefaultPersistenceUnitManager.java:326)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:235)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
Truncated. see log file for complete stacktrace
My context file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation=" http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd">
<context:annotation-config />
<context:component-scan base-package="com.dataaccess" />
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/mysql" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="persistenceUnitName" value="SYSTEM_UNIT"></property>
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
<property name="packagesToScan" value="com.dataaccess.model"></property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean>
</property>
<!-- <property name="jpaVendorAdapter">
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="true"></property>
<property name="showSql" value="true" />
</bean>
</property> -->
</bean>
<bean class="org.springframework.transaction.jta.JtaTransactionManager" id="transactionManager"/>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
</beans>
My persistence.xml file is in META-INF where it should be:
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="TRAVEL_SYSTEM_UNIT" transaction-type="JTA">
<jta-data-source>jdbc/mysql</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.max_fetch_depth" value="3" />
<property name="default_batch_fetch_size" value="25" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.generate_statistics" value="true" />
<!-- <property name="hibernate.hbm2ddl.auto" value="update"/> -->
<property name="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.SunONETransactionManagerLookup" />
</properties>
</persistence-unit>
</persistence>
using 3.1.2.RELEASE Maven 3.0.4
I just add below configuration to My context file:
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="pum"
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<list>
<value>classpath:META-INF/persistence-test.xml</value>
</list>
</property>
<property name="defaultDataSource" ref="dataSource"></property>
</bean>
On window I put META-INF/persistence.xml under src/java/resources.
Then I import same project to Ubuntu, this problem appeared. And it is fixed after I moved META-INF/persistence.xml to src/main/webapp.

Spring 3 with Thymeleaf config issue

Hello I have the following jars in my build path -
spring-beans-3.1.2.RELEASE.jar
spring-context-3.1.2.RELEASE.jar
spring-core-3.1.2.RELEASE.jar
spring-expression-3.1.2.RELEASE.jar
spring-web-3.1.2.RELEASE.jar
spring-webmvc-3.1.2.RELEASE.jar
thymeleaf-spring3-2.0.13.jar
and my servlet
<?xml version="1.0" encoding="UTF-8"?><br>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<context:component-scan base-package="web.controller" />
<!-- Enabling Spring MVC configuration through annotations -->
<mvc:annotation-driven />
<!-- Mapping Static Resources -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="order" value="1" />
<property name="viewNames" value="*.html" />
</bean>
</beans>
The error I get on launching is -
Cannot find class [org.thymeleaf.templateresolver.ServletContextTemplateResolver] for bean with name 'templateResolver' defined in ServletContext resource [/WEB-INF/springMVC-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.thymeleaf.templateresolver.ServletContextTemplateResolver
Am I missing any other library here? Any help is much appreciated.
You are missing the actual Thymeleaf jar. You included the Spring jar that provides the integration but you missed the actual implementation of it.
Download the jar from here
thymeleaf download site

mybatis configuration with spring3

I am trying to configure mybatis with spring 3. I am getting the following error while building my project.
Error creating bean with name 'datasource' defined in ServletContext resource [/WEB- INF/spring-servlet.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'driver' of bean class [org.apache.commons.dbcp.BasicDataSource]: Bean property 'driver' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
My spring-servel.xml is
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<!-- Enable annotation driven controllers, validation etc... -->
<mvc:annotation-driven />
<context:component-scan
base-package="com.mycom.mycontroller.controller" />
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/mydatabase"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mycom.mydatabase.db.mybatis.sqlmap" />
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
Jar files. I have added are
commons-dbcp-1.4.jar
commons-pool-1.6.jar
mybatis-3.1.0.jar
mybatis-spring-1.1.1.jar
mysql-connector-java-5.1.18-bin.jar
org.springframework.transaction-3.1.1.RELEASE.jar
and other spring jars
Please let me know if you need anything for more clarification
Problem has nothing to do with myBatis.
org.apache.commons.dbcp.BasicDataSource has not any "driver" property. I think you should user driverClassName instead.
See doc.

Resources