使用@DateTimeFormat,客户端发送的请求在语法上不正确 [英] The request sent by the client was syntactically incorrect using @DateTimeFormat
问题描述
我有一个JSON格式的字符串,我使用HTTP-PUT将其发送到具有Spring MVC和Hibernate的服务器。
$ b
因此 Controller < code $:
$ b $ pre $ @RequestMapping(value =/,method = RequestMethod.PUT)
public ResponseEntity< Map< String,Object>> myTest(
@RequestHeader(a)字符串a,
@RequestBody MyTestClass b){...}
JSON
:
{
number:123,
test:11/14
}
test
是一个java.util.Date(MySQL - > date),并且我注释了POJO:
@Column(name =TEST)
@DateTimeFormat(pattern =MM / yy)
私人日期测试;
test
应格式化为月/年。但是我用Firefox RESTClient试过了,我总是得到
客户端发送的请求在语法上是不正确的。
删除 test $ c
看起来,用 @DateTimeFormat(pattern =MM / yy)看起来,code>
因为您使用的是 RequestBody
application / json 内容类型,Spring将使用它的 MappingJackson2HttpMessageConverter
将JSON转换为一个对象你的类型。但是,您提供的日期字符串 11/14
与任何预先配置的日期模式都不匹配,因此无法正确解析。执行这项工作的 MappingJackson2HttpMessageConverter
,或者更确切地说是 ObjectMapper
并不知道有关 @DateTimeFormat
,一个Spring注释。
您需要告诉Jackson您要使用哪个日期模式。您可以使用自定义日期反序列化器来完成这项工作。
public class CustomDateDeserializer extends JsonDeserializer< Date> {
@Override
public Date deserialize(JsonParser jp,DeserializationContext ctxt)
throws IOException,JsonProcessingException {
SimpleDateFormat format = new SimpleDateFormat(MM / yy);
String date = jp.getText();
尝试{
return format.parse(date);
} catch(ParseException e){
throw new JsonParseException(e);
$ b $ p然后简单地注释你的字段杰克逊知道如何反序列化它。 @JsonDeserialize(使用= CustomDateDeserializer.class)
私人日期测试;
您可以使用 @DateTimeFormat
使用带有 @ModelAttribute
的url编码表单参数。 Spring注册了一些转换器,可以将字符串值从请求参数转换为 Date
对象。 这在deocumentation中有描述。
I have a JSON formatted String which I send with a HTTP-PUT to a server with Spring MVC and Hibernate.
Controller
:
@RequestMapping(value = "/", method = RequestMethod.PUT)
public ResponseEntity<Map<String, Object>> myTest(
@RequestHeader("a") String a,
@RequestBody MyTestClass b) { … }
JSON
:
{
"number":"123",
"test":"11/14"
}
test
is a java.util.Date (MySQL -> date) and I annotated the POJO like this:
@Column(name = "TEST")
@DateTimeFormat(pattern = "MM/yy")
private Date test;
So test
should be formatted as month/year. But I tried it with Firefox RESTClient and I always get
The request sent by the client was syntactically incorrect.
Removing test
, everything is okay and works as expected.
So it seems, that with @DateTimeFormat(pattern = "MM/yy")
is something wrong?
解决方案 Because you are using RequestBody
with an application/json
content type, Spring will use its MappingJackson2HttpMessageConverter
to convert your JSON to an object of your type. However the date string you provide, 11/14
does not match any of the pre-configured date patterns and therefore it can't parse it correctly. The MappingJackson2HttpMessageConverter
, or more specifically the ObjectMapper
that does the job, doesn't know anything about @DateTimeFormat
, a Spring annotation.
You will need to tell Jackson which date pattern you want to use. You can do so with a custom date deserializer
public class CustomDateDeserializer extends JsonDeserializer<Date> {
@Override
public Date deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
SimpleDateFormat format = new SimpleDateFormat("MM/yy");
String date = jp.getText();
try {
return format.parse(date);
} catch (ParseException e) {
throw new JsonParseException(e);
}
}
}
Then simply annotate your field so that Jackson knows how to deserialize it.
@JsonDeserialize(using = CustomDateDeserializer.class)
private Date test;
You could use @DateTimeFormat
if you were using url-encoded form parameters with @ModelAttribute
. Spring registers some converters that can convert a String value from a request parameters into a Date
object. This is described in the deocumentation.
这篇关于使用@DateTimeFormat,客户端发送的请求在语法上不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!