Java 春季无法通过 requestBody 将字符串转换为日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25646564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to convert String to Date by requestBody in spring
提问by user3745727
I have the below Code :
我有以下代码:
DTO :
DTO :
Class MyDTO {
import java.util.Date;
private Date dateOfBirth;
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
}
Controller
控制器
public void saveDOB(@RequestBody MyDTO myDTO, HttpServletRequest httprequest, HttpServletResponse httpResponse) {
System.out.println("Inside Controller");
System.out.println(myDTO.getDateOfBirth());
}
JSON Request :
JSON 请求:
{
"dateOfBirth":"2014-09-04",
}
If I send the request as yyyy-mm-dd automatic conversion to date object happens.output in controller:-dateOfBirth= Thu Sep 04 05:30:00 IST 2014
如果我将请求作为 yyyy-mm-dd 自动转换为日期对象发送。控制器中的输出:-dateOfBirth= Thu Sep 04 05:30:00 IST 2014
But when I send DateofBirth in dd-mm-yyyy format It does not convert String to Date automatically.So how i can i handle this case.
但是当我以 dd-mm-yyyy 格式发送 DateofBirth 时,它不会自动将字符串转换为日期。所以我该如何处理这种情况。
JSON Request :
JSON 请求:
{
"dateOfBirth":"04-09-2014",
}
Output:No Output in console does not even reaches controller.
输出:控制台中没有输出甚至没有到达控制器。
I have tried with @DateTimeFormat but its not working.
我试过 @DateTimeFormat 但它不起作用。
I am using Spring 4.02 Please suggest is there any annotation we can use.
我正在使用 Spring 4.02 请建议我们可以使用任何注释。
回答by Sotirios Delimanolis
@DateTimeFormat
is for form backing (command) objects. Your JSON is processed (by default) by Hymanson's ObjectMapper
in Spring's MappingHymanson2HttpMessageConverter
(assuming the latest version of Hymanson). This ObjectMapper
has a number of default date formats it can handle. It seems yyyy-mm-dd
is one of them, but dd-mm-yyyy
is not.
@DateTimeFormat
用于表单支持(命令)对象。您的 JSON 由 HymansonObjectMapper
在 Spring 中处理(默认情况下)MappingHymanson2HttpMessageConverter
(假设是最新版本的 Hymanson)。这ObjectMapper
有许多可以处理的默认日期格式。似乎yyyy-mm-dd
是其中之一,但dd-mm-yyyy
不是。
You'll need to register your own date format with a ObjectMapper
and register that ObjectMapper
with the MappingHymanson2HttpMessageConverter
. Here are various ways to do that :
您需要使用 a 注册您自己的日期格式,ObjectMapper
并ObjectMapper
使用MappingHymanson2HttpMessageConverter
. 这里有多种方法可以做到这一点:
Alternatively, you can use a JsonDeserializer
on either your whole class or one of its fields (the date). Examples in the link below
或者,您可以JsonDeserializer
在整个班级或其中一个字段(日期)上使用 a 。下面链接中的例子
回答by Jaille Chen
List itemCreate a class to extend JsonDeserializer
列表项创建一个类来扩展 JsonDeserializer
public class CustomJsonDateDeserializer extends JsonDeserializer<Date> {
@Override
public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
String date = jsonParser.getText();
try {
return format.parse(date);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}
Use @JsonDeserialize(using = CustomJsonDateDeserializer.class)
annotation on setter
methods.
@JsonDeserialize(using = CustomJsonDateDeserializer.class)
在setter
方法上使用注释。
Thanks @Varun Achar
answer, url
谢谢@Varun Achar
回答,网址