Hei*_*VDS 9 java spring json jackson
我正在编写一个Java 8 Spring MVC应用程序,它使用REST服务与传统的Progress OpenEdge应用程序进行通信(我正在使用Spring的RestTemplate).我需要读取和写入Progress应用程序的数据包含一些日期.在Java应用程序中,我使用java.time.LocalDate数据类型来表示这些字段,我使用Jackson将数据序列化/反序列化到Json中/从Json中反序列化.
我遇到的问题如下.当我从Progress Application发送数据时,日期将作为'2015-01-02'发送,并按预期存储在我的Java实体中作为LocalDate.当数据发送到Web前端时,Json还包含相同格式的日期.当我在Web前端更改信息并应用它时,它也会以'2015-01-02'的形式发送回Java应用程序,并再次存储为LocalDate而不会出现问题.但是,当我将数据发送到Progress应用程序时,我收到的Json不包含日期为'2015-01-02',而是包含三个字段(2015.0,1.0,2.0)的数组,并且Progress应用程序是无法将其分配回数据库中的日期字段.
Offcourse我可以在Progress端编写一个转换来将数组转换回日期,但是我想避免这种情况,因为我希望日期始终以ISO 8601格式发送.
根据我在Jackson上找到的信息,当启用WRITE_DATES_AS_TIMESTAMPS时,java.time.LocalDate表示为一个数组,但我禁用了此功能(当日期发送到Web前端时,它不会被发送为阵列...)
这是一些相关的代码:
CustomObjectMapper:
@Service
public class CustomObjectMapper extends ObjectMapper
{
public CustomObjectMapper()
{
this.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,
false);
}
}
Run Code Online (Sandbox Code Playgroud)
我的servlet.xml中的配置
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="customObjectMapper"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="customObjectMapper" class="com.msoft.utility.CustomObjectMapper"/>
Run Code Online (Sandbox Code Playgroud)
字段定义:
@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonSerialize(using = LocalDateSerializer.class)
private LocalDate deliveryDate;
Run Code Online (Sandbox Code Playgroud)
我正在使用带有Spring 4.1.5和Jackson 2.5.1的Java 8.
任何提示我如何使这个工作或寻找解决方案将非常感激,因为我已经在这2天工作了...
谢谢
编辑:
一些额外的信息......
我在我的pom.xml中包含了Jackson JSR-310模块.这是我的pom文件的相关部分......
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.5.RELEASE</version>
<scope>test</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
此外,如果我理解正确,Spring会在检测到时自动注册该模块.请参阅Jackson2ObjectMapperFactoryBean上的信息
请注意,Jackson的JSR-310和Joda-Time支持模块将在可用时自动注册(当Java 8和Joda-Time本身可用时).
所以我认为我不应该做任何其他事情来使这个工作正常,但显然我仍然缺少一些东西......
这对我有用,我没有使用Spring启动.
@Configuration
@EnableWebMvc
public class SpringMvcConfiguration extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
converters.add(new MappingJackson2HttpMessageConverter(builder.build()));
}
}
Run Code Online (Sandbox Code Playgroud)
我想你只需要注册JSR-310(Java 8 Date/Time)模块:
https://github.com/FasterXML/jackson-datatype-jsr310/
因为Jackson核心不能依赖任何Java 8特性(此时基线为2.5,是Java 6).对大多数数据类型库的后续支持无论如何都将通过插件模块.
| 归档时间: |
|
| 查看次数: |
16030 次 |
| 最近记录: |