支持@JsonView注解,自定义过滤Json视图输出字段。这在移动端API节省流量,隐私信息,或特殊场景下非常有用。
This commit is contained in:
@@ -11,6 +11,7 @@ import java.util.TimeZone;
|
|||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||||
import com.fasterxml.jackson.core.JsonGenerator;
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
@@ -46,6 +47,8 @@ public class JsonMapper extends ObjectMapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public JsonMapper() {
|
public JsonMapper() {
|
||||||
|
// Spring ObjectMapper 初始化配置,支持 @JsonView
|
||||||
|
new Jackson2ObjectMapperBuilder().configure(this);
|
||||||
// 为Null时不序列化
|
// 为Null时不序列化
|
||||||
this.setSerializationInclusion(Include.NON_NULL);
|
this.setSerializationInclusion(Include.NON_NULL);
|
||||||
// 允许单引号
|
// 允许单引号
|
||||||
@@ -61,7 +64,7 @@ public class JsonMapper extends ObjectMapper {
|
|||||||
@Override
|
@Override
|
||||||
public void serialize(Object value, JsonGenerator jgen,
|
public void serialize(Object value, JsonGenerator jgen,
|
||||||
SerializerProvider provider) throws IOException, JsonProcessingException {
|
SerializerProvider provider) throws IOException, JsonProcessingException {
|
||||||
jgen.writeString("");
|
jgen.writeString(StringUtils.EMPTY);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// // 统一默认Date类型转换格式。如果设置,Bean中的@JsonFormat将失效
|
// // 统一默认Date类型转换格式。如果设置,Bean中的@JsonFormat将失效
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import org.dom4j.Element;
|
|||||||
import org.dom4j.Namespace;
|
import org.dom4j.Namespace;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.JavaType;
|
import com.fasterxml.jackson.databind.JavaType;
|
||||||
|
|
||||||
@@ -46,6 +47,8 @@ public class XmlMapper extends com.fasterxml.jackson.dataformat.xml.XmlMapper{
|
|||||||
* 构造方法
|
* 构造方法
|
||||||
*/
|
*/
|
||||||
public XmlMapper() {
|
public XmlMapper() {
|
||||||
|
// Spring ObjectMapper 初始化配置,支持 @JsonView
|
||||||
|
new Jackson2ObjectMapperBuilder().configure(this);
|
||||||
// 设置时区
|
// 设置时区
|
||||||
this.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
|
this.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user