I\'m trying to make a property in a domain class hidden but it keeps appearing in the outputted JSON. I\'m using Jackson 2.0 and Spring 3.1.1
Output of /users/1:
Support for Jackson 2 has been added to Spring 3.1.2 (backported from Spring 3.2 - SPR-9507). Just upgrade your project from Spring 3.1.1 to Spring 3.1.2, and Jackson 2 works with the configuration you already have.
The problem is that Spring doesn't work with Jackson 2.0. This has been solved in the following way:
<bean id="jacksonMessageConverter"
class="own.implementation.of.MappingJacksonHttpMessageConverter"/>
<bean class="org.springframework.web.servlet.mvc
.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jacksonMessageConverter"/>
</list>
</property>
<property name="requireSession" value="false"/>
</bean>
And the own.implementation.of.MappingJacksonHttpMessageConverter is based on this:
http://www.jarvana.com/jarvana/view/org/springframework/spring-web/3.0.0.RELEASE/spring-web-3.0.0.RELEASE-sources.jar!/org/springframework/http/converter/json/MappingJacksonHttpMessageConverter.java?format=ok
But use ObjectMapper and other Jackson classes from Jackson 2.0 instead of Jackson 1.*
If you are using Spring 4 version, You will have to do following, Update dependency
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.3.3</version>
</dependency>
Replace older codehaus annotation import to following, if do not do this, your older annotation like JsonIgnoreProperties will not work with new version of Jackson
import com.fasterxml.jackson.annotation.*;
Remove mediaTypes from below, it has been deprecated.
ContentNegotiatingViewResolver
, Not needed, will be picked up from activation framework jar, Jackson view should be using
MappingJackson2JsonView
<beans:property name="viewResolvers">
<beans:list>
<beans:bean
class="org.springframework.web.servlet.view.BeanNameViewResolver" />
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<beans:bean
class="org.springframework.web.servlet.view.tiles3.TilesViewResolver">
<beans:property name="viewClass"
value="org.springframework.web.servlet.view.tiles3.TilesView"></beans:property>
<beans:property name="order" value="0"></beans:property>
</beans:bean>
</beans:list>
</beans:property>
<beans:property name="defaultViews">
<beans:list>
<beans:bean
class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
</beans:list>
</beans:property>
My configuration looks like this
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver" p:order="1">
<property name="mediaTypes">
<map>
<entry key="html" value="text/html"/>
<entry key="json" value="application/json"/>
</map>
</property>
<property name="viewResolvers">
<!-- this is not important for your jackson problem -->
<bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver" p:viewClass="org.springframework.web.servlet.view.tiles2.TilesView" p:order="2" depends-on="tilesConfigurer"/>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
</list>
</property>
</bean>
My controller looks like this
@Controller
public class SearchController {
@RequestMapping(value="/search", method = {RequestMethod.GET, RequestMethod.POST})
public SearchResponse search(@RequestParam(value = QUERY, required = true) String query) {
return generateSearchResponse(query);
}
}
My SearchResponse is more complex structure but it contains this type of objects.
@JsonIgnoreProperties({ "popis" })
public class Kurz implements Serializable {
private String nazev;
private String popis;
}
In addition to upgrading to Spring 3.1.2
, you also have to change the message converter to org.springframework.web.servlet.view.json.MappingJackson**2**HttpMessageConverter