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:
@@ -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;
|
||||
@@ -52,10 +53,13 @@ import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
import com.fasterxml.jackson.databind.module.SimpleSerializers;
|
||||
import com.fasterxml.jackson.databind.ser.BasicSerializerFactory;
|
||||
import com.fasterxml.jackson.databind.ser.Serializers;
|
||||
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
|
||||
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.ClassSerializer;
|
||||
import com.fasterxml.jackson.databind.ser.std.NumberSerializer;
|
||||
import com.fasterxml.jackson.databind.type.SimpleType;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.DateTimeZone;
|
||||
import org.junit.Test;
|
||||
@@ -329,6 +333,22 @@ public class Jackson2ObjectMapperBuilderTests {
|
||||
assertSame(mixInSource, objectMapper.findMixInClassFor(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void filters() throws JsonProcessingException {
|
||||
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json()
|
||||
.filters(new SimpleFilterProvider().setFailOnUnknownId(false)).build();
|
||||
JacksonFilteredBean bean = new JacksonFilteredBean("value1", "value2");
|
||||
String output = objectMapper.writeValueAsString(bean);
|
||||
assertThat(output, containsString("value1"));
|
||||
assertThat(output, containsString("value2"));
|
||||
|
||||
objectMapper = Jackson2ObjectMapperBuilder.json().filters((new SimpleFilterProvider().setFailOnUnknownId(false)
|
||||
.setDefaultFilter(SimpleBeanPropertyFilter.serializeAllExcept("property2")))).build();
|
||||
output = objectMapper.writeValueAsString(bean);
|
||||
assertThat(output, containsString("value1"));
|
||||
assertThat(output, not(containsString("value2")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void completeSetup() throws JsonMappingException {
|
||||
NopAnnotationIntrospector annotationIntrospector = NopAnnotationIntrospector.instance;
|
||||
@@ -436,4 +456,36 @@ public class Jackson2ObjectMapperBuilderTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@JsonFilter("myJacksonFilter")
|
||||
public static class JacksonFilteredBean {
|
||||
|
||||
public JacksonFilteredBean() {
|
||||
}
|
||||
|
||||
public JacksonFilteredBean(String property1, String property2) {
|
||||
this.property1 = property1;
|
||||
this.property2 = property2;
|
||||
}
|
||||
|
||||
private String property1;
|
||||
private String property2;
|
||||
|
||||
public String getProperty1() {
|
||||
return property1;
|
||||
}
|
||||
|
||||
public void setProperty1(String property1) {
|
||||
this.property1 = property1;
|
||||
}
|
||||
|
||||
public String getProperty2() {
|
||||
return property2;
|
||||
}
|
||||
|
||||
public void setProperty2(String property2) {
|
||||
this.property2 = property2;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,6 +28,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;
|
||||
@@ -51,6 +52,8 @@ import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
import com.fasterxml.jackson.databind.module.SimpleSerializers;
|
||||
import com.fasterxml.jackson.databind.ser.BasicSerializerFactory;
|
||||
import com.fasterxml.jackson.databind.ser.Serializers;
|
||||
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
|
||||
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.ClassSerializer;
|
||||
import com.fasterxml.jackson.databind.ser.std.NumberSerializer;
|
||||
import com.fasterxml.jackson.databind.type.SimpleType;
|
||||
@@ -314,6 +317,18 @@ public class Jackson2ObjectMapperFactoryBeanTests {
|
||||
assertSame(mixinSource, objectMapper.findMixInClassFor(target));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void filters() throws JsonProcessingException {
|
||||
this.factory.setFilters(new SimpleFilterProvider().setFailOnUnknownId(false));
|
||||
this.factory.afterPropertiesSet();
|
||||
ObjectMapper objectMapper = this.factory.getObject();
|
||||
|
||||
JacksonFilteredBean bean = new JacksonFilteredBean("value1", "value2");
|
||||
String output = objectMapper.writeValueAsString(bean);
|
||||
assertThat(output, containsString("value1"));
|
||||
assertThat(output, containsString("value2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void completeSetup() {
|
||||
NopAnnotationIntrospector annotationIntrospector = NopAnnotationIntrospector.instance;
|
||||
@@ -429,4 +444,36 @@ public class Jackson2ObjectMapperFactoryBeanTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@JsonFilter("myJacksonFilter")
|
||||
public static class JacksonFilteredBean {
|
||||
|
||||
public JacksonFilteredBean() {
|
||||
}
|
||||
|
||||
public JacksonFilteredBean(String property1, String property2) {
|
||||
this.property1 = property1;
|
||||
this.property2 = property2;
|
||||
}
|
||||
|
||||
private String property1;
|
||||
private String property2;
|
||||
|
||||
public String getProperty1() {
|
||||
return property1;
|
||||
}
|
||||
|
||||
public void setProperty1(String property1) {
|
||||
this.property1 = property1;
|
||||
}
|
||||
|
||||
public String getProperty2() {
|
||||
return property2;
|
||||
}
|
||||
|
||||
public void setProperty2(String property2) {
|
||||
this.property2 = property2;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
@@ -24,9 +24,13 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFilter;
|
||||
import com.fasterxml.jackson.annotation.JsonView;
|
||||
import com.fasterxml.jackson.databind.JavaType;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.ser.FilterProvider;
|
||||
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
|
||||
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
@@ -42,6 +46,7 @@ import static org.junit.Assert.*;
|
||||
* Jackson 2.x converter tests.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
public class MappingJackson2HttpMessageConverterTests {
|
||||
|
||||
@@ -258,6 +263,24 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
assertThat(result, not(containsString("\"withoutView\":\"without\"")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void filters() throws Exception {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
JacksonFilteredBean bean = new JacksonFilteredBean();
|
||||
bean.setProperty1("value");
|
||||
bean.setProperty2("value");
|
||||
|
||||
MappingJacksonValue jacksonValue = new MappingJacksonValue(bean);
|
||||
FilterProvider filters = new SimpleFilterProvider().addFilter("myJacksonFilter",
|
||||
SimpleBeanPropertyFilter.serializeAllExcept("property2"));
|
||||
jacksonValue.setFilters(filters);
|
||||
this.converter.writeInternal(jacksonValue, outputMessage);
|
||||
|
||||
String result = outputMessage.getBodyAsString(Charset.forName("UTF-8"));
|
||||
assertThat(result, containsString("\"property1\":\"value\""));
|
||||
assertThat(result, not(containsString("\"property2\":\"value\"")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jsonp() throws Exception {
|
||||
MappingJacksonValue jacksonValue = new MappingJacksonValue("foo");
|
||||
@@ -407,4 +430,27 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
}
|
||||
}
|
||||
|
||||
@JsonFilter("myJacksonFilter")
|
||||
private static class JacksonFilteredBean {
|
||||
|
||||
private String property1;
|
||||
private String property2;
|
||||
|
||||
public String getProperty1() {
|
||||
return property1;
|
||||
}
|
||||
|
||||
public void setProperty1(String property1) {
|
||||
this.property1 = property1;
|
||||
}
|
||||
|
||||
public String getProperty2() {
|
||||
return property2;
|
||||
}
|
||||
|
||||
public void setProperty2(String property2) {
|
||||
this.property2 = property2;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user