I am using the following bean definition to make my spring app talking in JSON
<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
Is it possible with this message converter bean to use the @JsonView annotation?
@JsonViewis already supported in the Jackson JSON Processor from v1.4 onwards.New Edit: Updated for Jackson 1.9.12
According to the v1.8.4 documentation the function I was using
writeValueUsingViewis now Deprecated Use ObjectMapper.viewWriter(java.lang.Class) instead… however that has also been Deprecated Since 1.9, use writerWithView(Class) instead! (see v1.9.9 documentation)So here is an updated example, tested with Spring 3.2.0 and Jackson 1.9.12 which simply returns
{id: 1}and not the extended{name: "name"}since it is using the.writerWithView(Views.Public.class). Switching toViews.ExtendPublic.classwill result in{"id":1,"name":"name"}Previous Edit: You need to instantiate the
ObjectMapperand write out the object using a custom view as shown here, or in this example:Define views:
Use views:
If you are using Jackson >= 1.7 you might find that the
@JSONFilterbetter suits your needs.