- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试使用 @DateTimeFormat
注释 LocalDateTime
对象为什么它不识别它?
我的主要想法是,一旦 Controller 接收到一个字符串,它就会将其转换为 LocalDateTime 对象
目前我得到:
{
"timestamp": 1493708443198,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.http.converter.HttpMessageNotReadableException",
"message": "Could not read JSON document: Can not construct instance of java.time.LocalDateTime: no String-argument constructor/factory method to deserialize from String value ('2015-09-26T01:30:00.000')\n at [Source: java.io.PushbackInputStream@3233297a; line: 5, column: 23] (through reference chain: net.petrikainulainen.spring.trenches.model.Topic[\"localDateTime\"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of java.time.LocalDateTime: no String-argument constructor/factory method to deserialize from String value ('2015-09-26T01:30:00.000')\n at [Source: java.io.PushbackInputStream@3233297a; line: 5, column: 23] (through reference chain: net.petrikainulainen.spring.trenches.model.Topic[\"localDateTime\"])",
"path": "/api/topics"
}
尝试发帖时
{
"id": "javaw2",
"name": "java code",
"descript2ion": "java description",
"localDateTime": "2015-09-26T01:30:00.000"
}
这是我的 Controller :
@RequestMapping(method = RequestMethod.POST, value = "/topics")
public void addTopic(@RequestBody Topic topic) {
topicService.addTopic(topic);
}
最佳答案
Can not construct instance of java.time.LocalDateTime: no String-argument constructor/factory method to deserialize from String value ('2015-09-26T01:30:00.000')
错误指出 LocalDateTime 类没有 String 参数构造函数/工厂方法,因此您必须编写自己的 反序列化器 将 Date
字符串表示反序列化为 LocalDateTime
对象。
类似的东西:
@JsonDeserialize(using = MyDateDeserializer.class)
private LocalDateTime localDateTime;
然后是MyDateDeserializer
实现
public class MyDateDeserializer extends JsonDeserializer< LocalDateTime > {
@Override
public LocalDateTime deserialize(JsonParser jp, DeserializationContext ctxt) throws Exception {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("your pattern");
String date = jp.getValueAsString();
LocalDateTime localDateTime = LocalDateTime.parse(date, formatter);
return localDateTime;
}
}
关于java - @DateTimeFormat 无法识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43732143/
我正在使用: @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) @JsonFormat( pattern = "MM-dd-yyyy" ) private
我看不到日期格式被错误地传递到哪里导致 RangeError? 我似乎无法解析 user.created .实际上,用户详细信息是从 json 响应中引入的,但我在本示例中将它们显示为变量。 这是我的
我有这种结构的时间码 hh:mm:ss.SSS 我有一个自己的类,实现时间接口(interface)。它具有自定义字段 TimecodeHour 字段,允许小时值大于 23。我想用 DateTimeF
我有一个允许用户输入出生日期的表单: 即:4/16/40 然后,当用户处理表单时,有一个函数检查其长度,添加前导零,解析日期,然后使用 FormatDateTime 返回出生年份: strTemp :
我正在尝试使用 GWT 验证日期。对于大多数目的,使用下面的代码可以正常工作: boolean valid = true; try{ DateTimeFormat.getFor
在我的 ASP.NET MVC 项目的 global.asax.cs 的 Application_BeginRequest() 方法中有代码: Thread.CurrentThread.Current
在我的课上我有日期字段: @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime date; 我使用 DataTimeFor
我需要有关日期格式的帮助,我看不到我缺少的内容。 格式:EEEE d'th' 'of' MMMM yyyy hh:mm:ss a 日期:2016 年 4 月 6 日星期三 06:10:08 AM尝试过
我正在将一些依赖于 moment-timezone 的时间检查转换为使用 INTL.DateTimeFormat。 对于上下文,代码采用以毫秒为单位的用户当前时间 (Date.now()),并根据三个
您好,我尝试创建一个 Controller ,它将接受请求参数作为 LocalDateTime。 ex: /api/actions?page=0&size=10&from=2018-05-02T20:
我在使用 @DateTimeFormat 注释转换实际不存在的日期时遇到问题。 例如,当我设置日期 15/10/2017 时,我的实体中的注释如下: @Column(nullable = false)
我正在尝试使用 @DateTimeFormat 注释 LocalDateTime 对象为什么它不识别它? 我的主要想法是,一旦 Controller 接收到一个字符串,它就会将其转换为 LocalDa
我得到了使用 dateTimeFormat 函数的代码。最初的开发者使用了“MM-HH-YY-dd-NN”的掩码。此代码可在他的机器上运行。它适用于我们的测试服务器。但它在我的本地机器上不起作用。我只
代码: DateTimeFormat dateFormat = DateTimeFormat .getFormat("EEE MMM dd HH:mm:ss zzz y
我正在使用 Spring 3.1.1.RELEASE。我有一个模型,我提交给我的一个 Controller 。在其中,是以下字段 @DateTimeFormat(pattern = "#{appPro
我在 SpringBootApplication 中使用 @DateTimeFormat 时遇到问题。下面是我遇到问题的代码片段` package com.example.demo; import o
我在 SpringBootApplication 中使用 @DateTimeFormat 时遇到问题。下面是我遇到问题的代码片段` package com.example.demo; import o
在不同的服务器上部署 Web 应用程序时遇到问题。似乎有些不一致 DateTimeFormat模式,如 ShortDatePattern , 使用相同的文化 (pt-BR)。 在我的开发机器(安装了
昨晚我们将 Web 服务层从物理 Windows 2008 r2 迁移到虚拟 Windows 2012。 我们在日志中收到大量关于 DateTime 格式无效的事件,这很奇怪,因为我们仔细检查了我们的
https://jsfiddle.net/prd9Lz18/11 March 被返回了两次。有谁知道为什么?我现在正在使用不同的解决方案(带有月份名称的数组),但我真的很好奇为什么会这样,我觉得这不是
我是一名优秀的程序员,十分优秀!