【发布时间】:2017-11-04 07:18:30
【问题描述】:
我有一个将数据存储在数据库中的休息服务。在我的 DTO 对象中,我有一个类型为 DOB 的字段: 私人 ZonedDateTime dateOfBirth;
@RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> createStudent(@Valid @RequestBody StudentDTO studentDto) {
//I have some more fields in StudentDTO along with dob field.
}
我收到JsonMappingException(Index cannot be parsed) 当我从邮递员发送具有价值的请求时:
dateOfBirth":"2017-10-01T01:00
问题:如何将日期时间格式转换为所需格式? 如果我必须转换代码,我应该在哪里写?因为当我从邮递员发送请求时,数据会自动使用@RequestBody绑定到对象。那么,我该如何克服这个问题?
【问题讨论】:
-
您的依赖项中有
jackson-datatype-jsr310吗?这引入了一组允许使用 java-8 日期时间类的序列化程序。 -
我使用@JsonDeserialize 编写了自定义反序列化器。下面是代码:@JsonDeserialize(using = ZonedDateDeserializer.class) private ZonedDateTime date; public class ZonedDateDeserializer extends StdDeserializer
{ public ZonedDateTime deserialize(JsonParser parser, DeserializationContext context) throws IOException { return ZonedDateTime.parse(parser.readValueAs(String.class));我收到以下错误:无法读取文档:无法在索引 16 处解析文本“2017-10-01T01:00+0000”(通过参考链:
标签: java json rest spring-mvc