What I would like to do is serialize the People
object having a list of only summary persons and a single person detail record. Person
includes nested objects like Name
(included in summary) and Address
(which is detail only). And I need the internal Spring ObjectMapper
configured to do this serialization. I've tried to do some things with @JsonView
and @JsonFilter
, but not finding the right path forward. I've also tried to provide my own custom ObjectMapper
, and I can through its construction when my container is started, but at runtime I get errors that no filter with id "summary" has been configured.
I'm asking why the setFilter(...)
did not seem to take effect during runtime, it's as if my customer ObjectMapper
was constructed but not being used?
The rest of my implementation can be ignored, as I am not at all sure I am on the right path. But, in case it helps...
- I put
@JsonFilter("summary"
) onPerson
- I put
@JsonView(Views.Summary.class)
on select properties withinPerson
- I did not make use of Json Views, only used the annotation as a marker that my filter, which I can't get to execute, attempted to look for and then serialize the attribute if found.
- I discovered filters are not applied to List's, so...
How can I do this?
public class People
{
@JsonProperty("persons")
// A list of persons, summary info only
private List<Person> persons;
@JsonProperty("person")
// A single, full detail person
private Person person;
}
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper" ref="myObjectMapper" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="myObjectMapper" class="this.that.MyObjectMapper" />
public class MyObjectMapper extends ObjectMapper {
public MyObjectMapper() {
super();
Set<Class<?>> views = new HashSet<Class<?>>();
views.add(Views.Summary.class);
FilterProvider filters = new SimpleFilterProvider()
.addFilter("summary", new FilterExceptViewFilter(views));
setFilters(filters);
}
}