I have a problem that can not be resolved on the configuration of ObjectMapper. I need to configure it to ignore those parameters that do not have my POJO...so easy but I configured a thousand different ways and I can not make it work.
my servlet.xml
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" >
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="featuresToDisable">
<array>
<util:constant
static-field="com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES"/>
</array>
</property>
</bean>
</property>
</bean>
I have also tried to extend the class ObjectMapper but I get the same results. I see the mapper is configured correctly but i hope MappingJackson2HttpMessageConverter receives a different instance of ObjectMapper. I Dont know what else to do to make me ignore the parameters globally.
When I make a request with a parameter that should be ignored (not present on the POJO) gives a syntax error in the request.
Im using:
Spring 3.2.0
jackson 2.1.2
Best regards and thank´s
Try
<!--
Implement a custom ObjectMapper and initialize the features you needed
eg. FAIL_ON_UNKNOWN_PROPERTIES
-->
<bean id="jacksonObjectMapper" class="com.sample.CustomObjectMapper"/>
<!-- Replace Spring's default message converter -->
<mvc:annotation-driven>
<mvc:message-converters>
<ref bean="jacksonObjectMapper"/>
</mvc:message-converters>
</mvc:annotation-driven>
Solution tested on Spring 3.2.18.RELEASE and Jackson 2.7.5.
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<bean id="jacksonObjectMapper"
class="com.fasterxml.jackson.databind.ObjectMapper" />
<bean
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="jacksonObjectMapper" />
<property name="targetMethod" value="configure" />
<property name="arguments">
<list>
<value
type="com.fasterxml.jackson.databind.DeserializationFeature">FAIL_ON_UNKNOWN_PROPERTIES</value>
<value>false</value>
</list>
</property>
</bean>
<mvc:annotation-driven>
<mvc:message-converters>
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="jacksonObjectMapper" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
Related
I have created a project and it worked fine with MySQL.
I need to use SQL Server instead of MySQL, the exception bellow is displayed:
com.sun.faces.mgbean.ManagedBeanCreationException: An error occurred
performing resource injection on managed bean ManageUserBean
Knowing that I made some changes on the file spring.xml like the following, could you please tell me what's missing?
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="employeeDAO" class="com.journaldev.spring.jdbc.dao.EmployeeDAOImpl">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="employeeDAOJDBCTemplate" class="com.journaldev.spring.jdbc.dao.EmployeeDAOJDBCTemplateImpl">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url" value="jdbc:sqlserver://localhost:1433;database=springDBA"/>
<property name="username" value="hello" />
<property name="password" value="world" />
</bean>
</beans>
Any Reply is appreciated.Thanks in advance.
After giving more details about my problem by this thread Incorrect syntax near the keyword 'table' and could not extract ResultSet.
I found the solution: I have to avoid calling an entity like 'User' on SQL-Server while it's considered like a reserved keyword on this database.
There is no error caused by the configuration below. However it is not taken efect in Json converter. I am using MethodInvokingBean instead of MethodInvokingFactoryBean because I read that the second one give me a new instance and I am interested in changing the current instance used by RestTemplate. Anyway, I tried the factory one and it doesn't take effect as well.
Honestly, after some weeks searching for, I am wondering if it is really possible to change the default configuration of MapperObject when it is used by RestTemplate. The default configuration is to ignore the nanoseconds and I am really asking myself how someone else has been using RestTemplate when there is the requirement of nanoseconds. I can fix this by changing from sql.TimeStamp to String but it doesn't seem the best approach. If someone else has faced similiar issue and has been able to use sql.TimeStamp with nanoseconds either by changing Jackson Mapper configuration or other way I will appreciate a lot some tips.
//everything start here
ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
_myCllientOfRestService = context.getBean(MyCllientOfRestService.class, request, response);
_myCllientOfRestService.myMethod();
// myCllientOfRestService
Public void myMethod(){
//it is not returning the nano seconds and I am sure the nano seconds is available in rest service return side. MyReturnType object has the sql.TimeStamp variable filled in with nanoseconds in the rest service side but it is lost in client side
_myReturnType = restTemplate.postForObject(urlRestService, myParameters,MyReturnType.class);
}
//applicationContext.xml
<bean id="myCllientOfRestService" class="com.someCompany.mhe.log.handler.MyCllientOfRestService" scope="prototype" lazy-init="true">
<property name="restTemplate" ref="restTemplate2" />
</bean>
<bean id="myMIB"
class="org.springframework.beans.factory.config.MethodInvokingBean">
<property name="targetObject" ref="jacksonObjectMapper" />
<property name="targetMethod" value="configure" />
<property name="arguments">
<list>
<value type="com.fasterxml.jackson.databind.SerializationFeature">WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS</value>
<value>true</value>
</list>
</property>
</bean>
<bean id="myMIB2"
class="org.springframework.beans.factory.config.MethodInvokingBean">
<property name="targetObject" ref="jacksonObjectMapper" />
<property name="targetMethod" value="configure" />
<property name="arguments">
<list>
<value type="com.fasterxml.jackson.databind.DeserializationFeature">READ_DATE_TIMESTAMPS_AS_NANOSECONDS</value>
<value>true</value>
</list>
</property>
</bean>
<bean id="restTemplate2" class="org.springframework.web.client.RestTemplate" >
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="jacksonObjectMapper" />
</bean>
</list>
</property>
</bean>
I am currently working on a system that has been around for a number of years and has gone through a fair amount of transformation. (GWT\Spring\Hibernate). I recently had to merge two entities as part of a change. When I looked at the actual sql tables I noticed that there were a number of orphaned columns that were actually not mapped to the entities and could effectively be dropped. I was wondering if there are any tools out there that would help us to identify and strip out outdated columns which are not actually being linked to our entities.
If you´re Using Spring and hibernate you can use autodetection to create/update database though the entities attributes that you have in your entity.
your hibernate.hbm2ddl.auto setting should be defining that the database is created (options are validate, create, update or create-drop)
Here the Spring configuration:
<?xml version="1.0" encoding="UTF-8"?>
<persistence-unit name="NewPersistenceUnit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/Icarus"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hbm2ddl.auto" value="create"/>
</properties>
<class>net.interaxia.icarus.data.models.ServerNode</class>
</persistence-unit>
I have a custom MessageConverter registered in spring with the following configuration:
<bean id="jsonHttpMessageConverter" class="com.eventwiz.web.util.ServiceResponseHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<util:list id="beanList">
<ref bean="jsonHttpMessageConverter"/>
</util:list>
</property>
</bean>
However, it's not being called as I confirmed that with a breakpoint in my code. ServiceResponseHttpMessageConverter subclasses MappingJacksonHttpMessageConverter and overrides writeInternal() method. I've even tried overriding MessageConverter.supports() just to see if that was being called and it wasn't. Any ideas what's going on?
The issue, as was found based on an answer from the author was the <mvc:annotation-driven/> tag which registers its own handlerAdapter, so if another handlerAdapter is added to the Spring MVC configuration file with converters added to this adapter, the custom adapters will not take effect. The fix is to either register the httpMessageConverters through <mvc:message-converters... tag under <mvc:annotation-driven or removing <mvc:annotation-driven and having the custom handleradapter with the httpmessageconverter registered under it.
I created a basic Spring MVC / JPA / Hibernate app. I am trying to save a UserProfile entity to test if I can actualy persist it, but nothing gets saved and no exception is thrown either.
In the controller method I create a simple UserProfile (which is an #Entity) and I am sending that to a service method. The UserProfileServiceImpl class is annotated with #Service and the addUserProfile(UserProfile profile) method is annotated with #Transactional.
In the service method, all I do is call a DAO method (class annotated with #Repository). In the DAO method all I do is call entityManager.persist(object), with object being the user profile object.
Nothing gets written to the server log and the log level is at INFO.
Nothing appears in the Mysql query log (and I know the query log works)
The entityManager gets properly injected.
The datasource is properly initiated, because when I enter faulty credentials I get SQLExceptions.
I hope you can tell me what's wrong. I'll post some of my code and config files below.
The service method:
// The service method gets called from the controller.
// Its class is annotated with #Service
#Transactional(readOnly = false)
public void addUserProfile(UserProfile userProfile) {
userProfileDao.save(userProfile);
}
The Dao method:
// The save(T object) method is in the GenericDaoJpa class, which is the superclass
// of the UserProfileDaoJPA class that is referenced from the service.
// I have established that the entityManager is there and the object is a
// UserProfile. The #Repository annotation is on the child class UserProfileDaoJpa.
public void save(T object) {
entityManager.persist(object);
}
Main application-context.xml
<context:property-placeholder location="classpath*:**/*.properties"/>
<import resource="spring-jpa.xml"/>
The application-context-web.xml file
<mvc:annotation-driven />
<context:component-scan base-package="nl.codebasesoftware.produx" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
spring-jpa.xml
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="${db.driverClassName}"
p:url="${db.url}" p:username="${db.username}" p:password="${db.password}"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory"/>
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
<bean id="defaultLobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler"/>
persistence.xml
<persistence-unit name="mysqlPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
<!-- Needed to properly process #PersistenceContext -->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
Somehow no SQL is sent to Mysql with this setup, but no exception is thrown either, so I have no idea what's going on. Hope you can help :)
i think you are missing transaction propagation at service. marking readonly=false just set session to auto flush. but setting up proper transaction propogation will make sure start of trnasaction and commit/rollback.
after removing mode="aspectj" it has started working because i think because of as per spring doc
The default mode "proxy" will process annotated beans to be proxied
using Spring's AOP framework (following proxy semantics, as discussed
above, applying to method calls coming in through the proxy only). The
alternative mode "aspectj" will instead weave the affected classes
with Spring's AspectJ transaction aspect (modifying the target class
byte code in order to apply to any kind of method call). AspectJ
weaving requires spring-aspects.jar on the classpath as well as
load-time weaving (or compile-time weaving) enabled. (See the section
entitled Section 6.8.4.5, “Spring configuration” for details on how to
set up load-time weaving.)
and probably you have not configured load time weaving
Ok, I got it. First I thought I found the answer here
Declarative transactions (#Transactional) doesn't work with #Repository in Spring
But after some more testing I found it was not the location of
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
but its contents.
After removing the mode="aspectj" attribute it started working! If anyone would like to comment why that is, please do.
Let me share my full setup with you. This is for Spring 3.1 with Hibernate 4. NOTE: for 'brevity' I only posted the contents of the configuration files, and omitted the outer tags <beans> and <persistence> and the namespace declarations. I removed spring-jpa.xml altogether and moved its contents into application-context.xml.
Contents of web.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<display-name>My Spring MVC web application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:**/application-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>produxDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:**/application-context-web.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>produxDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Contents of application-context.xml:
<!-- main setup -->
<context:property-placeholder location="classpath*:**/*.properties"/>
<context:annotation-config/>
<context:component-scan base-package="nl.codebasesoftware.produx.domain" />
<context:component-scan base-package="nl.codebasesoftware.produx.service" />
<context:component-scan base-package="nl.codebasesoftware.produx.dao" />
<!-- Data and JPA setup -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="${db.driverClassName}"
p:url="${db.url}" p:username="${db.username}" p:password="${db.password}"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" p:dataSource-ref="dataSource"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" p:entityManagerFactory-ref="entityManagerFactory"/>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="defaultLobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler"/>
Contents of application-context-web.xml:
<mvc:annotation-driven />
<context:component-scan base-package="nl.codebasesoftware.produx.controller" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
Contents of webapps/META-INF/persistence.xml
<persistence-unit name="mysqlPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<!-- <property name="hibernate.hbm2ddl.auto" value="create-drop"/> -->
</properties>
</persistence-unit>
<!-- Needed to properly process #PersistenceContext which injects the entity manager -->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
Contents of environment.properties:
db.url=jdbc:mysql://localhost:3306/yourDatabaseName
db.driverClassName=com.mysql.jdbc.Driver
db.username=yourUsername
db.password=yourPassword