支持@JsonView注解,自定义过滤Json视图输出字段。这在移动端API节省流量,隐私信息,或特殊场景下非常有用。

This commit is contained in:
thinkgem
2018-08-23 22:11:55 +08:00
parent e1b6cdc831
commit 7fdcecb912
2 changed files with 7 additions and 1 deletions

View File

@@ -11,6 +11,7 @@ import java.util.TimeZone;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonGenerator;
@@ -46,6 +47,8 @@ public class JsonMapper extends ObjectMapper {
}
public JsonMapper() {
// Spring ObjectMapper 初始化配置,支持 @JsonView
new Jackson2ObjectMapperBuilder().configure(this);
// 为Null时不序列化
this.setSerializationInclusion(Include.NON_NULL);
// 允许单引号
@@ -61,7 +64,7 @@ public class JsonMapper extends ObjectMapper {
@Override
public void serialize(Object value, JsonGenerator jgen,
SerializerProvider provider) throws IOException, JsonProcessingException {
jgen.writeString("");
jgen.writeString(StringUtils.EMPTY);
}
});
// // 统一默认Date类型转换格式。如果设置Bean中的@JsonFormat将失效

View File

@@ -21,6 +21,7 @@ import org.dom4j.Element;
import org.dom4j.Namespace;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import com.fasterxml.jackson.databind.JavaType;
@@ -46,6 +47,8 @@ public class XmlMapper extends com.fasterxml.jackson.dataformat.xml.XmlMapper{
* 构造方法
*/
public XmlMapper() {
// Spring ObjectMapper 初始化配置,支持 @JsonView
new Jackson2ObjectMapperBuilder().configure(this);
// 设置时区
this.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
}