Avoid Jackson xmlMapper being used as default objectMapper with Spring - java

I had to add this dependency to my pom.xml in order to deserialize xml files in my software.
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
However it seem to now be used as default objectMapper by Spring (4.1.6) and all of the rest response which are not explicitly asked as json are returned as xml.
Seemingly, the AbstractJackson2HttpMessageConverter takes the wrong road.
I tried to force the use of the default object mapper by adding this to the app-config.xml but it did not change anything:
<bean name="jacksonMapper" class="com.fasterxml.jackson.databind.ObjectMapper"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="jacksonMapper" />
</bean>
</list>
</property>
</bean>
I might have to change jackson for xstream because it does not extend objectMapper and thus doesn't work the same way with Spring, but it would be much more work than setting up Spring.

I finally wound up with some sort of solution whereby I had to introduce two relatively gnarly hacks. But given how hardcoded the problem is in Spring this is the only way I could see to deal with this.
On the Spring MVC side of things I had to extend the WebMvcConfigurerAdapter overriding the following method. This simply takes out the message converter:
public void extendMessageConverters (List<HttpMessageConverter<?>> aConverters)
{
aConverters.removeIf (aConverter -> (aConverter instanceof MappingJackson2XmlHttpMessageConverter));
}
Then to fix the RestTemplate I had to add this to one of my configuration classes. It also simply removes the message converter.
#Autowired
private List<RestTemplate> restTemplates;
#PostConstruct
public void removeXmlMessageConverter ()
{
restTemplates.forEach (aRestTemplate -> aRestTemplate.getMessageConverters ().removeIf (aConverter -> (aConverter instanceof MappingJackson2XmlHttpMessageConverter)));
}
I opted for the removal of the unwanted converter rather than specifying a list of hardcoded message converters as that does not lock me in as much for future upgrades.

Related

in Spring mvc,how to exclude java entity from object mapper xml configuration

in my spring mvc application, we are using the below object mapper configuration.
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="propertyNamingStrategy">
<bean class="com.fasterxml.jackson.databind.PropertyNamingStrategy$PascalCaseStrategy" /></property>
</bean></property>
This configuration is applying for all java entity classes. i want to exclude one java entity(ie,when we pass one dto, it should not be changed by this object mapper configuration).Please let me know how to do this.

Can you set Jackon's Include.NON_NULL globally in JAX-RS?

I have a JAX-RS API using Apache CXF. Recently I switched from Jettison to Jackson (2.7.1) for JSON handling. I am using JacksonJaxbJsonProvider.
One thing I need to do to help with transition is to ensure that null fields in JSON are not being rendered. For that I am using following annotation on class level:
#JsonInclude(JsonInclude.Include.NON_NULL)
public class Book {
...
}
Is there any way to set it globally, so that I do not have to apply this annotation to every class?
With Jettison I was able to specify properties in beans.xml file, is there a similar approach possible with Jackson to achieve NON_NULL behavior?
<bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.json.JSONProvider">
<property name="dropRootElement" value="true"/>
<property name="supportUnwrapped" value="true"/>
</bean>
I have seen some suggestion to set up this property on ObjectMapper level when instantiated, however when using Apache CXF I never create ObjectMapper by hand, as this instantiation is handled by the framework (probably happens somewhere in JacksonJaxbJsonProvider).
Is there any way to set NON_NULL property globally?
Just in case it helps somebody else, configuring ObjectMapper in beans.xml worked with NON_NULL however my JAXB annotations stopped working. To get both of them working at the same time I resorted to creating my own ObjectMapper provider:
#Provider
public class CustomJacksonObjectMapperProvider implements ContextResolver<ObjectMapper> {
final ObjectMapper defaultObjectMapper;
public CustomJacksonObjectMapperProvider() {
defaultObjectMapper = createDefaultMapper();
}
#Override
public ObjectMapper getContext(Class<?> type) {
return (defaultObjectMapper);
}
private static ObjectMapper createDefaultMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(Include.NON_NULL);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.setAnnotationIntrospector(
AnnotationIntrospector.pair(
new JacksonAnnotationIntrospector(),
new JaxbAnnotationIntrospector(mapper.getTypeFactory())
));
return (mapper);
}
}
and registering it in beans.xml (under jaxrs:providers) as follows:
<bean id="customJacksonObjectMapperProvider" class="CustomJacksonObjectMapperProvider"/>
You can include the following configuration as below:
<bean id="jacksonJaxbJsonProvider"
class="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider">
<constructor-arg ref="objectMapper"></constructor-arg>
</bean>
<bean id="objectMapper" class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="serializationInclusion" value="NON_NULL"></property>
</bean>

How to contribute to a list property of a bean using Spring?

I have 3 projects:
framework
product-a
product-b
Each of the products depends on the framework, but they don't know each other.
I have 3 spring configuration files: one for each project. The configuration file of each product includes (with <import resource="classpath:/...) the configuration file of the framework.
In the framework there is a bean called "manager", which has a property List<AnInterface> theList. The "manager" has a addXxx(anImplementation), which adds elements to the list).
The framework, and each of the product provide implementations of AnInterface, which have to be added to theList.
So in the end, when product-a is running, the manager contains implementations from the framework, and from product-a, idem for product-b
What is the best practice to perform this initialization with Spring ?
The only solution I could think about is to create a dedicated class which contructor will take the manager and a list of contributions, and add them to the manager, but it's ugly because 1/ It manipulate external objects in the constructor, 2/ I have to create a dummy class just to initialize other classes... I don't like that.
I think that code should not know about Spring if it is not really needed. Therefore I would do all initialization in Spring config.
We can use bean definition inheritance and property overriding to do it.
Framework class
public class Manager {
private List<AnInterface> theList;
public void init() {
// here we use list initialized by product
}
}
Framework context
<bean id="manager"
init-method="init"
abstract="true"
class="Manager">
<property name="theList">
<list/> <!-- this will be overriden or extnded -->
</property>
</bean>
Product A context
<bean id="managerA"
parent="manager"
scope="singleton"
lazy-init="false">
<property name="theList">
<list>
<ref bean="impl1"/>
<ref bean="impl2"/>
</list>
</property>
</bean>
Watch out for parent and child properties in such configuration. Not all are inherited from parent. Spring documentation specifies:
The remaining settings are always taken from the child definition: depends on, autowire mode, dependency check, singleton, scope, lazy init.
Moreover, there is also collection merging in Spring so by specifing in child bean
<list merge="true">
you can merge parent and child lists.
I have observed this pattern in a number of projects and some extendable Web frameworks based on Spring.
I have accepted the answer of Grzegorz because it's a clean solution to my initial problem, but here as an alternate answer, the a technical solution to contribute to a list property of an existing bean.
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="manager"/>
<property name="targetMethod"><value>addXxx</value></property>
<property name="arguments"><list value-type="com.xxx.AnInterface">
<value ref="impl1" />
<value ref="impl2" />
...
</list></property>
</bean>

Why isn't spring using my constructor to init this bean (Map parameter)

I have a spring beans configuration file where I define the following jackson classes as spring beans.
For some reason on run-time the filterProvider bean is instantiated without the map argument.
You can see from the docs that the SimpleFilterProvider does have such a constructor and that SimpleBeanPropertyFilter implements BeanPropertyFilter.
<bean id="productAttributesAndAdvertiserNameFilter" class="org.codehaus.jackson.map.ser.impl.SimpleBeanPropertyFilter" factory-method="filterOutAllExcept">
<constructor-arg value="name"/>
</bean>
<bean id="offerIdFilter" class="org.codehaus.jackson.map.ser.impl.SimpleBeanPropertyFilter" factory-method="filterOutAllExcept">
<constructor-arg value="id"/>
</bean>
<bean id="filterProvider" class="org.codehaus.jackson.map.ser.impl.SimpleFilterProvider">
<constructor-arg>
<util:map value-type="org.codehaus.jackson.map.ser.BeanPropertyFilter">
<entry key="onlyNameFilter" value-ref="productAttributesAndAdvertiserNameFilter" />
<entry key="onlyIdFilter" value-ref="offerIdFilter" />
</util:map>
</constructor-arg>
</bean>
Update:
As of Jackson 1.9.5 this issue is fixed (thanks Tatu)
Any help would be appreciated.
Looks like you've found a bug in SimpleFilterProvider.
I just downloaded the latest sources (1.9.4) and the constructors are defined as such:
public SimpleFilterProvider() {
_filtersById = new HashMap<String,BeanPropertyFilter>();
}
/**
* #param mapping Mapping from id to filter; used as is, no copy is made.
*/
public SimpleFilterProvider(Map<String,BeanPropertyFilter> mapping) {
_filtersById = new HashMap<String,BeanPropertyFilter>();
}
The constructor which takes the mapping ignores it... (i.e. javadoc is incorrect)
I think <util:map> is misplaced here. I'd make it a separate bean, outside of the filter provider declaration, and refer to it. OR I'd change that to a <map> without the util namespace.
I don't see why it is not working.
At worst, you can create your own class by extending the SimpleFilterProvider and declare this bean in your Spring context...

Spring MappingJacksonJsonView, how to tell to use it instead of JSP view?

I'm trying to use MappingJacksonJsonView with Spring 3.0, without success. I don't know what I'm doing wrong, I think the problem is that I don't know how to tell to use the MappingJacksonJsonView to render a request. I tried to use the same name for view name and bean name of MappingJacksonView, but didn't work. I built a sample test application here: https://github.com/stivlo/restjson
In web.xml I've defined ContextLoaderListener and the mapping for dispatcherServlet.
In servlet-context.xml I've added
<mvc:annotation-driven/>
and
<bean name="jsonView"
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
In org.obliquid.restjson.web.ToDoList.java I set the logical view name as jsonView.
However, instead of using MappingJacksonJsonView, it looks for a JSP file, according to my JSP mapping.
message /restjson/WEB-INF/jsp/jsonView.jsp
description The requested resource (/restjson/WEB-INF/jsp/jsonView.jsp)
is not available.
What should I change to use MappingJacksonJsonView as a renderer?
UPDATE 1: In following tests I've found that if I add the following to my servlet-context.xml, JSON rendering works, but my other view, rendered as JSP (home) is not working anymore.
<!-- Resolve views based on string names -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
UPDATE 2: I removed the BeanNameViewResolver and changed my ToDoList.java to return directly the Collection to be converted in JSON, instead of ModelAndView, with a #ResponseBody annotation, as follows:
#RequestMapping("/toDoList")
public #ResponseBody List<ToDoItem> test() {
List<ToDoItem> toDoList = new ArrayList<ToDoItem>();
toDoList.add(new ToDoItem(1, "First thing, first"));
toDoList.add(new ToDoItem(1, "After that, do the second task"));
return toDoList;
}
In this way it works. Even though the mapping is even more "magical". It makes me wonder, if a similar renderer exists for XML for instance, how does Spring know which renderer to pick?
Spring will use Accept header sent by the client to return most appropriate view. Here you will find my complete Spring MVC application that returns both JSON and XML.
As you can see, I only needed:
<mvc:annotation-driven />
I also used the same annotations: #RequestMapping to map request to a method and #ResponseBody to tell Spring that what I am returning from the controller is the actual response. It might however need some tweaking/formatting, and here Spring takes care of marshalling your object into most appropriate type like JSON.
You should do it this way:
In your xml file set the following: set
<mvc:annotation-driven />
After it you need to set Jackson serializer:
<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jacksonMessageConverter"/>
</list>
</property>
</bean>
after it you can use it in your Controller:
#RequestMapping(value="/getObjects",method = RequestMethod.POST)
#ResponseBody
public List<MyObject> getCategories(){
List<MyObject> objects = daoService.gettAllObjects();
return objects;
}
Adding the following worked in my case
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
<property name="order" value="0" />
</bean>
So basically we should try to resolve any view as a bean first
you will need to see ContentNegotiatingViewResolver,and set defaultviews property to MappingJacksonJsonView, and #ResponseBody uses HttpMessageConverter to instead of ViewSolver,see the differences between them
http://ufasoli.blogspot.com/2013/08/viewresolver-vs-messageconverter-spring.html

Categories