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:
@@ -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.
|
||||
@@ -28,6 +28,7 @@ import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
|
||||
import com.fasterxml.jackson.databind.JavaType;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.databind.ser.FilterProvider;
|
||||
|
||||
import org.springframework.http.HttpInputMessage;
|
||||
import org.springframework.http.HttpOutputMessage;
|
||||
@@ -218,15 +219,20 @@ public abstract class AbstractJackson2HttpMessageConverter extends AbstractHttpM
|
||||
try {
|
||||
writePrefix(generator, object);
|
||||
Class<?> serializationView = null;
|
||||
FilterProvider filters = null;
|
||||
Object value = object;
|
||||
if (value instanceof MappingJacksonValue) {
|
||||
MappingJacksonValue container = (MappingJacksonValue) object;
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFilter;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
@@ -42,6 +43,7 @@ import com.fasterxml.jackson.databind.PropertyNamingStrategy;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.databind.cfg.HandlerInstantiator;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
import com.fasterxml.jackson.databind.ser.FilterProvider;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
@@ -109,6 +111,8 @@ public class Jackson2ObjectMapperBuilder {
|
||||
|
||||
private HandlerInstantiator handlerInstantiator;
|
||||
|
||||
private FilterProvider filters;
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
|
||||
@@ -486,6 +490,16 @@ public class Jackson2ObjectMapperBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the global filters to use in order to support {@link JsonFilter @JsonFilter} annotated POJO.
|
||||
* @since 4.2
|
||||
* @see MappingJacksonValue#setFilters(FilterProvider)
|
||||
*/
|
||||
public Jackson2ObjectMapperBuilder filters(FilterProvider filters) {
|
||||
this.filters = filters;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Spring {@link ApplicationContext} in order to autowire Jackson handlers ({@link JsonSerializer},
|
||||
* {@link JsonDeserializer}, {@link KeyDeserializer}, {@code TypeResolverBuilder} and {@code TypeIdResolver}).
|
||||
@@ -593,6 +607,9 @@ public class Jackson2ObjectMapperBuilder {
|
||||
if (this.handlerInstantiator != null) {
|
||||
objectMapper.setHandlerInstantiator(this.handlerInstantiator);
|
||||
}
|
||||
if (this.filters != null) {
|
||||
objectMapper.setFilters(this.filters);
|
||||
}
|
||||
else if (this.applicationContext != null) {
|
||||
objectMapper.setHandlerInstantiator(
|
||||
new SpringHandlerInstantiator(this.applicationContext.getAutowireCapableBeanFactory()));
|
||||
|
||||
@@ -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.
|
||||
@@ -23,6 +23,7 @@ import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFilter;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.databind.AnnotationIntrospector;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
@@ -35,6 +36,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.databind.cfg.HandlerInstantiator;
|
||||
import com.fasterxml.jackson.databind.ser.FilterProvider;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
|
||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||
@@ -373,6 +375,15 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
|
||||
this.builder.handlerInstantiator(handlerInstantiator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the global filters to use in order to support {@link JsonFilter @JsonFilter} annotated POJO.
|
||||
* @since 4.2
|
||||
* @see Jackson2ObjectMapperBuilder#filters(FilterProvider)
|
||||
*/
|
||||
public void setFilters(FilterProvider filters) {
|
||||
this.builder.filters(filters);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBeanClassLoader(ClassLoader beanClassLoader) {
|
||||
this.builder.moduleClassLoader(beanClassLoader);
|
||||
|
||||
@@ -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.
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.http.converter.json;
|
||||
|
||||
import com.fasterxml.jackson.databind.ser.FilterProvider;
|
||||
|
||||
/**
|
||||
* A simple holder for the POJO to serialize via
|
||||
* {@link MappingJackson2HttpMessageConverter} along with further
|
||||
@@ -37,6 +39,8 @@ public class MappingJacksonValue {
|
||||
|
||||
private Class<?> serializationView;
|
||||
|
||||
private FilterProvider filters;
|
||||
|
||||
private String jsonpFunction;
|
||||
|
||||
|
||||
@@ -81,6 +85,27 @@ public class MappingJacksonValue {
|
||||
return this.serializationView;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Jackson filter provider to serialize the POJO with.
|
||||
* @since 4.2
|
||||
* @see com.fasterxml.jackson.databind.ObjectMapper#writer(FilterProvider)
|
||||
* @see com.fasterxml.jackson.annotation.JsonFilter
|
||||
* @see Jackson2ObjectMapperBuilder#filters(FilterProvider)
|
||||
*/
|
||||
public void setFilters(FilterProvider filters) {
|
||||
this.filters = filters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the Jackson filter provider to use.
|
||||
* @since 4.2
|
||||
* @see com.fasterxml.jackson.databind.ObjectMapper#writer(FilterProvider)
|
||||
* @see com.fasterxml.jackson.annotation.JsonFilter
|
||||
*/
|
||||
public FilterProvider getFilters() {
|
||||
return this.filters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the name of the JSONP function name.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user