Support Jackson @JsonFilter
This commit adds a filters property to MappingJacksonValue and also manages a special FilterProvider class name model key in order to be able to specify a customized FilterProvider for each handler method execution, and thus provides a more dynamic alternative to our existing JsonView support. A filters property is also now available in Jackson2ObjectMapperBuilder and Jackson2ObjectMapperFactoryBean in order to set easily a global FilterProvider. More details about @JsonFilter at http://wiki.fasterxml.com/JacksonFeatureJsonFilter. Issue: SPR-12586
This commit is contained in:
@@ -28,6 +28,7 @@ import com.fasterxml.jackson.core.JsonEncoding;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.databind.ser.FilterProvider;
|
||||
|
||||
import org.springframework.http.converter.json.MappingJacksonValue;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -176,9 +177,11 @@ public abstract class AbstractJackson2View extends AbstractView {
|
||||
protected Object filterAndWrapModel(Map<String, Object> model, HttpServletRequest request) {
|
||||
Object value = filterModel(model);
|
||||
Class<?> serializationView = (Class<?>) model.get(JsonView.class.getName());
|
||||
if (serializationView != null) {
|
||||
FilterProvider filters = (FilterProvider) model.get(FilterProvider.class.getName());
|
||||
if (serializationView != null || filters != null) {
|
||||
MappingJacksonValue container = new MappingJacksonValue(value);
|
||||
container.setSerializationView(serializationView);
|
||||
container.setFilters(filters);
|
||||
value = container;
|
||||
}
|
||||
return value;
|
||||
@@ -205,16 +208,21 @@ public abstract class AbstractJackson2View extends AbstractView {
|
||||
|
||||
writePrefix(generator, object);
|
||||
Class<?> serializationView = null;
|
||||
FilterProvider filters = null;
|
||||
Object value = object;
|
||||
|
||||
if (value instanceof MappingJacksonValue) {
|
||||
MappingJacksonValue container = (MappingJacksonValue) value;
|
||||
value = container.getValue();
|
||||
serializationView = container.getSerializationView();
|
||||
filters = container.getFilters();
|
||||
}
|
||||
if (serializationView != null) {
|
||||
this.objectMapper.writerWithView(serializationView).writeValue(generator, value);
|
||||
}
|
||||
else if (filters != null) {
|
||||
this.objectMapper.writer(filters).writeValue(generator, value);
|
||||
}
|
||||
else {
|
||||
this.objectMapper.writeValue(generator, value);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -29,6 +29,7 @@ import javax.servlet.http.HttpServletResponse;
|
||||
import com.fasterxml.jackson.annotation.JsonView;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.ser.FilterProvider;
|
||||
|
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||
import org.springframework.http.converter.json.MappingJacksonValue;
|
||||
@@ -182,7 +183,8 @@ public class MappingJackson2JsonView extends AbstractJackson2View {
|
||||
Set<String> modelKeys = (!CollectionUtils.isEmpty(this.modelKeys) ? this.modelKeys : model.keySet());
|
||||
for (Map.Entry<String, Object> entry : model.entrySet()) {
|
||||
if (!(entry.getValue() instanceof BindingResult) && modelKeys.contains(entry.getKey()) &&
|
||||
!entry.getKey().equals(JsonView.class.getName())) {
|
||||
!entry.getKey().equals(JsonView.class.getName()) &&
|
||||
!entry.getKey().equals(FilterProvider.class.getName())) {
|
||||
result.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user