为什么消息转换器不使用对象映射器日期 fromat 进行日期转换? [英] why is the Object Mapper date fromat not used by message converters for date transform?
问题描述
我遇到了 JSON 的自定义日期格式问题,它当然可以在测试中运行,但在部署的应用程序中失败.我想使用日期模式作为 dd-MM-yyyy
几乎标准以及它在印度的预期方式.还配置了一个日期格式化程序,并像这样注入配置
I am facing an issue with customised date formatting for JSON, where it of-course works in tests but fails on the deployed application. I want to use date pattern as dd-MM-yyyy
pretty much standard and how it is expected here in India. There is a date formatter configured as well, and injected in the configuration like so
@Configuration
public class RepositoryWebConfiguration extends RepositoryRestMvcConfiguration {
private static final Logger LOGGER = LoggerFactory.getLogger(RepositoryWebConfiguration.class);
@Override
public void configureJacksonObjectMapper(ObjectMapper objectMapper) {
LOGGER
.debug("Configuring dd-MM-yyyy as default date pattern for all JSON representations in Rest DATA Repositories");
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.indentOutput(true).dateFormat(new SimpleDateFormat("dd-MM-yyyy"));
objectMapper = builder.build();
}
}
现在这应该适用于 JSON,因为我正在注入特定的日期格式,在我的测试中,我首先创建了一个具有相同格式的映射器
Now this should work for JSON since I am injecting a specific date formatting, in my tests I first create a mapper with the same format
private ObjectMapper halObjectMapper() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.indentOutput(true).dateFormat(new SimpleDateFormat("dd-MM-yyyy"));
ObjectMapper objectMapper = builder.build();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.registerModule(new Jackson2HalModule());
return objectMapper;
}
然后我使用这个映射器为 POST 请求生成 JSON.JSON 生成一切正常,我期望 dd-MM-yyyy
格式,我得到了
I then use this mapper to generate the JSON for POST request. The JSON is generated all fine, I expected the format dd-MM-yyyy
and I get exactly that
{
"id":null,
"name":"KABADI",
"seatsAvailable":40,
"workshopType":"KABADI FOUNDATION",
"date":"16-08-2015",
"venue":"http://localhost:8080/venues/2"
}
注册了 ObjectMapper
后,我希望这个 JSON 可以毫无问题地转换为 Workshop
对象 &日期格式为 dd-MM-yyyy
.但是,由于格式异常,POST 请求失败,Jackson 抱怨说它无法将 dd-MM-yyyy
转换为 Date
,因为可用格式为 "仅
With the ObjectMapper
registered I expect this JSON to be transformed to Workshop
object without any issues & with the date format dd-MM-yyyy
. However, the POST request fails due to format exception, and Jackson complains it cannot transform dd-MM-yyyy
to Date
as the available formats are "only"
("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE,dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd")
("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd")
这是日志
Caused by: com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of java.util.Date from String value '16-08-2015': not a valid representation (error: Failed to parse Date value '16-08-2015': Can not parse date "16-08-2015": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
at [Source: HttpInputOverHTTP@54153158; line: 5, column: 53] (through reference chain: com.agilityroots.doi.workshop.entity.Workshop["date"])
at com.fasterxml.jackson.databind.exc.InvalidFormatException.from(InvalidFormatException.java:55)
at com.fasterxml.jackson.databind.DeserializationContext.weirdStringException(DeserializationContext.java:810)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer._parseDate(StdDeserializer.java:740)
at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateBasedDeserializer._parseDate(DateDeserializers.java:176)
at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateDeserializer.deserialize(DateDeserializers.java:262)
at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateDeserializer.deserialize(DateDeserializers.java:246)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:538)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:99)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:238)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:118)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3066)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2221)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:205)
... 46 common frames omitted
不必像在通常的 Spring WebMVC + Boot 场景中那样更早地查看这些覆盖,这个属性用于解决问题
never had to look too much into these overrides earlier as in the usual Spring WebMVC + Boot scenario, this property used to the do the trick
spring.jackson.date-format=dd-MM-yyyy
spring.jackson.date-format=dd-MM-yyyy
所以我可能在这里遗漏了一些东西,或者我以错误的方式配置了 objectMapper,因为它没有被注入?如何让 JSON 转换器接受 dd-MM-yyyy
格式?
So I might as well be missing something here, or I am configuring objectMapper in the wrong way in the sense that it is not injected? How can I get JSON transformers to accept dd-MM-yyyy
format?
推荐答案
做的太多了,暂时解决了这个问题.JSON 对象仍然不是 dd-MM-yyyy 格式,被解释为一个单独的日期,例如 16-08-2015
返回为 February X in 22
将调查那个
Was doing too much, this fixed the test for now. Object to JSON is still not in dd-MM-yyyy format, gets interpreted as a separate date for example 16-08-2015
returns as February X in 22
will look into that
@Override
public void configureJacksonObjectMapper(ObjectMapper objectMapper) {
objectMapper.setDateFormat(new SimpleDateFormat("dd-MM-yyyy"));
}
更新:
另一个问题是使用 @Temporal(TemporalType.DATE)
注释 Workshop 中的日期字段,导致日期保存为 2015-08-16
,已删除那个注释也是.
The other problem was annotation the date field in Workshop with @Temporal(TemporalType.DATE)
which caused the date to be saved as 2015-08-16
, removed that annotation too.
这篇关于为什么消息转换器不使用对象映射器日期 fromat 进行日期转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!