我知道,这里有很多来自 Spring MVC 客户端的 JSON 日期格式的示例,但在我的例子中,除了一个具有复合契约的特定情况之外,一切正常。
问题在于,在 curObj
的客户端 JSON 响应端,以字符串格式接收到日期 currencyOpenedDate
,同时depObj.subdivisionClosedDate
日期中的时间以时间戳格式指定。
您可以在问题底部找到答复屏幕。
问题是如何接收时间戳格式的curObj.currencyOpenedDate
?
Spring 配置
<context:component-scan base-package="ru.iteco.kursval.server.rest"/>
<context:annotation-config/>
<!-- Configures the @Controller programming model -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</list>
</property>
</bean>
<!-- Total customization - see below for explanation. -->
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false"/>
<property name="favorParameter" value="true"/>
<property name="parameterName" value="mediaType"/>
<property name="ignoreAcceptHeader" value="true"/>
<property name="useJaf" value="false"/>
<property name="defaultContentType" value="application/json"/>
<property name="mediaTypes">
<map>
<entry key="json" value="application/json"/>
<entry key="xml" value="application/xml"/>
</map>
</property>
</bean>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean:bean class="ru.iteco.kursval.server.rest.config.ApiInterceptor" autowire="constructor">
</bean:bean>
</mvc:interceptor>
</mvc:interceptors>
<!-- Make this available across all of Spring MVC -->
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager"/>
复合
public class RegionalBankBlockCurrencyDto implements Serializable {
//.....
private Long curId;
/*----> here */ private CurrencyDto curObj;
private Long depId;
private SubdivisionDto depObj;
// .... getters and setters
}
货币对象
public class CurrencyDto implements Serializable {
private Date currencyOpenedDate;
private Date currencyClosedDate;
public Date getCurrencyOpenedDate() {
return currencyOpenedDate;
}
public void setCurrencyOpenedDate(Date currencyOpenedDate) {
this.currencyOpenedDate = currencyOpenedDate;
}
public Date getCurrencyClosedDate() {
return currencyClosedDate;
}
public void setCurrencyClosedDate(Date currencyClosedDate) {
this.currencyClosedDate = currencyClosedDate;
}
}
货币和分割 DTO 很简单,没有任何注释,两种情况下的日期属性初始化如下所示:
Date openDate = null;
if (currency.getOpenDate() != null) {
openDate = new Date(currency.getOpenDate().getTime());
}
服务器端实例
JSON 响应
最佳答案
按如下方式配置您的对象映射器;
@Autowired
private ObjectMapper objectMapper;
objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, true);
关于java - Spring Rest JSON 日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40968134/