Spring休息JSON-日期格式
Spring Rest JSON-date format
我知道,这里有很多来自 Spring MVC 的 JSON-date 格式的客户端示例,但在我的情况下,一切正常,除了一个复合合同的特定情况。
问题是在客户端 JSON 响应端 curObj
收到的日期 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);
我知道,这里有很多来自 Spring MVC 的 JSON-date 格式的客户端示例,但在我的情况下,一切正常,除了一个复合合同的特定情况。
问题是在客户端 JSON 响应端 curObj
收到的日期 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);