我在提交表单时遇到 Ajax 请求问题。该表单包含这些字符串化 JSON 数据:
{"articleContent":"<p>aaa</p>","title":"Po vyplnění titulku aktuality budete","header":"aa","enabled":false,"timestamp":"1358610697521","publishedSince":"03.01.2013 00:00","publishedUntil":"","id":"10"}
当 json 包含“03.01.2013 00:00”值时,服务器响应为400 Bad Request
问题是,没有调用自定义的 DateTimePropertyEditor(使用@InitBinder 注册),并且没有调用 String 格式的 DateTime。你知道如何解决这个问题吗?
控制器映射方法,即处理请求
@RequestMapping( value = "/admin/article/edit/{articleId}", method = RequestMethod.POST, headers = {"content-type=application/json"})
public @ResponseBody JsonResponse processAjaxUpdate(@RequestBody Article article, @PathVariable Long articleId){
JsonResponse response = new JsonResponse();
Article persistedArticle = articleService.getArticleById(articleId);
if(persistedArticle == null){
return response;
}
List<String> errors = articleValidator.validate(article, persistedArticle);
if(errors.size() == 0){
updateArticle(article, persistedArticle);
response.setStatus(JsonStatus.SUCCESS);
response.setResult(persistedArticle.getChanged().getMillis());
}else{
response.setResult(errors);
}
return response;
}
初始化绑定器
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(DateTime.class, this.dateTimeEditor);
}