我想从杰克逊的 Json 更改日期格式,我已经做到了;我的 servlet.xml 包含:
....<mvc:resources mapping="/resources/**" location="/resources/" />
<util:properties id="cfgproperties" location="classpath:cfg.properties" />
<context:annotation-config />
<context:component-scan base-package="com.controller" />
<mvc:annotation-driven>
<mvc:message-converters register-defaults="false">
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper">
<bean class="com.controller.CustomObjectMapper" />
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:application.properties</value>
</property>
</bean>
我的 CustomObjectMapper 是:
public class CustomObjectMapper extends ObjectMapper {
private static Logger logger = Logger.getLogger(CustomObjectMapper.class.getName());
@Value("#{cfgproperties.dateformat}")
private String dateFormats;
public CustomObjectMapper() throws Exception{
logger.info(path);
super.configure(Feature.WRITE_DATES_AS_TIMESTAMPS, false);
SimpleDateFormat date = new SimpleDateFormat(dateFormats);
setDateFormat(date);
}
问题是字符串 dateFormats 是空的,但是如果我使用
@Value("#{cfgproperties.dateformat}")
在其他类中,该值不为空。怎么能解决?谢谢!