How to serialize JodaTime using UNIX timestamp format with Jackson - spring

I am using
jackson-annotations-2.4.3.jar
jackson-core-2.4.3.jar
jackson-databind-2.4.3.jar
jackson-datatype-joda-2.4.3.jar
and Spring 3.2.11. I am using the joda time's DateTime format, and i want to serialize beans that have some date-times as properties. What i would like is to serialize only date-time's timestamp. Instead, jackson serializes the whole object, which leads to problems in js afterwards.
What i am trying to achieved worked when using jackson 1.8.3.
I have tried to register a JodaModule to the object mapper for MappingJackson2HttpMessageConverter, by defining this in applicationContext.xml. Even though the joda module is loaded, it doesnt seem to work.
I tried the following config:
<beans:beans>
<beans:bean id="objectMapper"
class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"
p:simpleDateFormat="yyyy-MM-dd'T'HH:mm:ss.SSSZ">
</beans:bean>
<beans:bean
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"
p:targetObject-ref="objectMapper" p:targetMethod="registerModule">
<beans:property name="arguments">
<beans:list>
<beans:bean class="com.fasterxml.jackson.datatype.joda.JodaModule" />
</beans:list>
</beans:property>
</beans:bean>
</beans:beans>
After that i tried:
What else should i try?

Nobody seems to care about this question. However, i will close it, since i have found the answer: i had to save the configuration in a xml file and import it in multiple xml's so that the config would be visible to all the involved contexts.

Related

Can we replace Springframework annotations (#CacheConfig, #Cacheable, #CachePut) in the XMl file?

I am implementing a module with Spring Cache mechanism. The module is generic and can cache different type of entities. So I don't want to change the Java code and want the user to configure the applicationcontext.xml file accordingly. He can put the name of the different types of entities within the applicationcontext.xml and the code should work. For e.g. -
<context:annotation-config/>
<cache:annotation-driven cache-manager="cacheManager"/>
<context:component-scan base-package="com.nokia.oss.sure.adapter"/>
<bean id="NetworkEntityService" class="com.nokia.oss.sure.adapter.cache.NetworkEntityServiceImpl"/>
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" name="NetworkEntity"/>
</set>
</property>
</bean>
He may change NetworkEntity to ServiceEntity and so on.
So in the Java code I need to mention -
#CacheConfig(cacheNames={"NetworkEntity"})
Or I can put the same for every method -
#CachePut(cacheNames="NetworkEntity", key="#entity.sureName")
public Entity addEntity(Entity entity) {
return entity;
}
But as I stated earlier, I don't want to put the cache name "NetworkEntity" in the Java code, but want to put the same in the applicationcontext.xml file. Is it possible?
Furthermore is it possible to omit all the annotations in the Java file? If I just use AbstractApplicationContext context = new GenericXmlApplicationContext("applicationContext.xml"); is it possible to mention in the applicationContext.xml file what are the methods where I want to apply the #Cacheable annotation for e.g.
I searched a lot, couldn't find it anywhere.
Thanks
Nirmalya
I found out the answer. We can put the following in the applicationContext.xml -
<!-- define caching behavior -->
<cache:advice id="cacheAdviceInterface" cache-manager="cacheManager">
<cache:caching cache="NetworkEntity">
<cache:cacheable method="getEntity"/>
<cache:cache-put method="putEntity"/>
</cache:caching>
</cache:advice>
In that case we don't need to put the #CacheConfig, #CachePut etc annotations within the Java file.

Spring Data RedisTemplate: Serializing the Value and HashValue

I tried following this tutorial:
http://javakart.blogspot.in/2012/12/spring-data-redis-hello-world-example.html
My question is related to this:
Weird redis key with spring data Jedis
I was able to solve the keys and hashkeys using the StringRedisSerializer .
<bean
id="stringRedisSerializer"
class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<bean
id="redisTemplate"
class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnectionFactory"
p:keySerializer-ref="stringRedisSerializer"
p:hashKeySerializer-ref="stringRedisSerializer"
/>
However I found it a problem using a serializer for the value and hashvalue.
I tried adding this:
p:valueSerializer-ref="stringRedisSerializer"
p:hashValueSerializer-ref="stringRedisSerializer"
But an error prompted:
"User cannot be cast to java.lang.String"
Can anyone share how to use a serializer for the value/hashvalue?
Redis stores keys and values as string. It's up to your persistence layer to handle the parsing. In the example, User is a POJO and not a String. I suggest that you use JacksonJsonRedisSerializer instead of StringRedisSerializer. This way you're storing json as your value.
<bean id="userJsonRedisSerializer"
class="org.springframework.data.redis.serializer.JacksonJsonRedisSerializer">
<constructor-arg type="java.lang.Class" value="com.mycompany.redis.domain.User"/>
</bean>

Programmatically change property value

<beans:bean id="loginUrlAuthenticationEntryPoint"
class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
<beans:property name="loginFormUrl" value="/session-timeout-1.do" />
</beans:bean/>
I have 2 different session-timeout pages for different types of users. After a user logs in, the value of the property may have to change from "/session-timeout-1.do" to "/session-timeout-2.do" after checking the type of the user.
I am wondering that is there an API that can change the property value at runtime?
Or is it possible to have a variable in the config file, e.g.
<beans:property name="loginFormUrl" value="${time-out-url}">
where variable "time-out-url" can be set programmatically?
You could subclass the LoginUrlAuthenticationEntryPoint class and provide your own logic.
See the class source here:
http://git.springsource.org/spring-security/rwinchs-spring-security/blobs/2d271666a406a4409def9afcd73ea340c40a7a88/web/src/main/java/org/springframework/security/web/authentication/LoginUrlAuthenticationEntryPoint.java
Specifically the method:
determineUrlToUseForThisRequest
which "Allows subclasses to modify the login form URL that should be applicable for a given request".

Spring data jpa add filter/interceptor

I have used hibernate before, and have successfully added a filter that would intercept saves and entities who implemented a certain interface would have something logged.
Is it possible to do something similar in the new Spring Data, I have just started out using it.
Yes you can always add filters/interceptors with spring data
Following is an example:
<bean id="customizableTraceInterceptor" class="
org.springframework.aop.interceptor.CustomizableTraceInterceptor">
<property name="enterMessage" value="Entering $[methodName]($[arguments])"/>
<property name="exitMessage" value="Leaving $[methodName](): $[returnValue]"/>
</bean>
<aop:config>
<aop:advisor advice-ref="customizableTraceInterceptor"
pointcut="execution(public * org.springframework.data.jpa.repository.JpaRepository+.*(..))"/>
</aop:config>
reference: http://static.springsource.org/spring-data/data-jpa/docs/current/reference/html/

How to do spring request parameter conversion

In a Spring 3 based web (portlet) application I have a controller with a method like this:
#RenderMapping
public ModelAndView handleRenderRequest(...,#RequestParam MyClass myObject)
{
...
}
Now I wonder: How do I tell spring how to convert the request parameter to MyClass. I found information about property editors and about the Converter interface and there seem to be some implications that Converter is the successor of the property editor, but nobody seems to like being explicit about it.
I implemented the converter interface for String to MyClass conversion. But how do I tell Spring about it? I am using annotation based configuration wherever possible, so I checked whether spring will detect the Converter from my classpath automatically, but it does not.
So thought that the part Configuring a ConversionService from the manual wants to tell me that I've got to add the following to my applicationContext.xml which I did:
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="some.package.MyConverter"/>
</list>
</property>
</bean>
Bit still:
org.springframework.beans.ConversionNotSupportedException: Failed to
convert value [...]
So what am I missing? And is there a way, to just configure a package and let spring scan this package for converters and register them automatically? And say that in one certain method I want to use a different converter than in all other methods. For example I want an integer that has a Luhn-Checksum to be checked and the checksum removed, how can I do that? Something like #RequestParam(converter=some.package.MyConverter.class) would be great.
EDIT
Ok, I just caught in the documentation:
Use the Formatter SPI when you're working in a client environment,
such as a web application, and need to parse and print localized field
values
So I guess that means I should use the Formatter SPI, yet a third possibility next to property editors and converters (I think I could really to with a comparison table or the like). I did implement the Parser interface as well and tried to register my converter using:
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="some.package.SortOrderEnumConverterSpring"/>
</set>
</property>
</bean>
As you can see I used "set" instead of "list" for specifying the converters. I set a debugging breakpoint in the FormattingConversionServiceFactoryBean.setConverters method which did not fire upon using list, but it did fire on using set.
Additionally I added
<mvc:annotation-driven conversion-service="conversionService"/>
And the namespace for the mvc-prefix to my applicationContext. But still I get the conversion not supported exception.
I also tried going back to the converter approach and changed in my applicationContext.xml file the parameter list for converters from list to set, but that did not change anything either.
EDIT2
As digitaljoel pointed out it is possible to set different converters per controller using an initBinder method. I applied this to my controller:
#Autowired
private ConversionService conversionService;
#InitBinder
public void initBinder(WebDataBinder binder)
{
binder.setConversionService(conversionService);
}
And in my applicationContext.xml:
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="some.package.with.MyConverter"/>
</set>
</property>
</bean>
And all suddenly the conversion works just fine :-). But I am not quite happy about having to apply this to each and every of my controllers. There must be a way to just set it in my applicationContext for everyone, is there not? Good to know that I can override default if I need to (after all I asked for that), but I still want to set defaults.
And what about the Formatter stuff. Shouldn't I be using that instead of Converter?
Spring Portlet MVC 3.0 does not support
<mvc:annotation-driven conversion-service="conversionService"/>
Visit https://jira.springsource.org/browse/SPR-6817 for more info about this.
However you can add this to your common applicationContext
<bean
class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean
class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="conversionService">
<list>
<ref bean="conversionService" />
</list>
</property>
</bean>
</property>
</bean>
This way you do not need add #InitBinder to every single controller
and of course
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<!-- converter implementations here -->
</list>
</property>
</bean>
You are correct that Converter (and ConverterFactory) are the successors to property editors. Your problem may be that you are not accepting the appropriate type as a parameter to your converter, but that's hard to say without seeing the converter code. If you are expecting Long or Integer you may actually be getting a String from Spring and need to perform that key conversion yourself first.
As for configuration, I believe you need to list all of your converters in the bean configuration in your xml. If you annotate your converter implementation with #Component you might be able to reference it by the bean name instead of the fully qualified path, but I have only tried that for a ConverterFactory, not a Converter.
Finally, on specific converters, it looks like you may be able to configure the conversion service at the controller level (see Javi's answer on Setting up a mixed configuration for annotation-based Spring MVC controllers ) and then you could just place that method (and others that require that controller) into a controller that uses a secondary conversion service which you ought to be able to inject by name with the #Resource annotation.
Implement a WebArgumentResolver:
public class MyArgumentResolver implements WebArgumentResolver
{
#Override
public Object resolveArgument(MethodParameter methodParameter,
NativeWebRequest webRequest) throws Exception
{
Class<?> paramType = methodParameter.getParameterType();
if (paramType == MyClass.class)
{
String parameterName = methodParameter.getParameterName();
String stringParameter = webRequest.getParameter(parameterName);
return convert(stringParameter);
}
return UNRESOLVED;
}
}
And register it in your applicationContext.xml:
<bean class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="customArgumentResolver">
<bean class="com.dshs.eakte.util.MyArgumentResolver" />
</property>
</bean>
This works and even has the advantage of allowing parameter conversion that is based on multiple method parameters.
To achieve something similar to what you're doing, I found this blog entry useful.
i think you need to use something like
public ModelAndView handleRenderRequest(...,#ModelAttribute("myObject") MyClass myObject)

Resources