我的目标是配置objectMapper
它只序列化带注释的元素@JsonProperty
.
为了做到这一点,我按照这个解释说明了如何配置对象映射器.
我在这里描述了自定义objectmapper .
但是,当NumbersOfNewEvents
序列化类时,它仍然包含json中的所有属性.
有人有提示吗?提前致谢
杰克逊1.8.0春季3.0.5
CustomObjectMapper
public class CompanyObjectMapper extends ObjectMapper {
public CompanyObjectMapper() {
super();
setVisibilityChecker(getSerializationConfig()
.getDefaultVisibilityChecker()
.withCreatorVisibility(JsonAutoDetect.Visibility.NONE)
.withFieldVisibility(JsonAutoDetect.Visibility.NONE)
.withGetterVisibility(JsonAutoDetect.Visibility.NONE)
.withIsGetterVisibility(JsonAutoDetect.Visibility.NONE)
.withSetterVisibility(JsonAutoDetect.Visibility.DEFAULT));
}
}
Run Code Online (Sandbox Code Playgroud)
servlet.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="de.Company.backend.web" />
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper" ref="jacksonObjectMapper" />
</bean>
</list>
</property>
</bean>
<bean id="jacksonObjectMapper" class="de.Company.backend.web.CompanyObjectMapper" />
</beans>
Run Code Online (Sandbox Code Playgroud)
NumbersOfNewEvents
public class …
Run Code Online (Sandbox Code Playgroud) 我从ExtJS获得一个日期字符串格式:
"2011-04-08T09:00:00"
当我尝试反序列化此日期时,它会将时区更改为印度标准时间(将时间+5:30添加).这就是我如何反序化日期:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
getObjectMapper().getDeserializationConfig().setDateFormat(dateFormat);
Run Code Online (Sandbox Code Playgroud)
这样做也不会改变时区.我仍然在IST得到日期:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
getObjectMapper().getDeserializationConfig().setDateFormat(dateFormat);
Run Code Online (Sandbox Code Playgroud)
如何在没有时区麻烦的情况下对日期的日期进行反序列化?
我试图在Spring中编写自定义JSON反序列化器.我想在大多数字段中使用默认序列化程序,并为少数属性使用自定义反序列化程序.可能吗?我这样做是因为,大部分属性都是值,所以对于这些我可以让杰克逊使用默认的反序列化器; 但很少有属性是引用,所以在自定义反序列化器中,我必须查询数据库以获取引用名称并从数据库中获取引用值.
如果需要,我会展示一些代码.
由于对Spring仍然有点不熟悉,我遇到了一个问题,需要为Jackson实现我的自定义反序列化器.该过程在一个小教程中描述,但是,我坚持使用Spring.我不明白,在哪里
ObjectMapper mapper = new ObjectMapper();
Run Code Online (Sandbox Code Playgroud)
在Spring中,当json通过控制器类的方法反序列化时执行MVC.所以我不知道怎么做才能用自定义反序列化器替换默认的反序列化器.
任何建议最受欢迎.