gpt4 book ai didi

java - PropertyEditor 未根据 AJAX (JSON) 请求调用

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:29:29 24 4
gpt4 key购买 nike

我在提交表单时遇到 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。你知道如何解决这个问题吗?

Controller 映射方法,即处理请求

@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;
}

初始化绑定(bind)器

 @InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(DateTime.class, this.dateTimeEditor);
}

最佳答案

我使用 @JsonDeserialize 解决了这个问题

@JsonDeserialize(using=DateTimeDeserializer.class)
public DateTime getPublishedUntil() {
return publishedUntil;
}

我必须实现自定义反序列化器。

    public class DateTimeDeserializer extends StdDeserializer<DateTime> {

private DateTimeFormatter formatter = DateTimeFormat.forPattern(Constants.DATE_TIME_FORMAT);

public DateTimeDeserializer(){
super(DateTime.class);
}

@Override
public DateTime deserialize(JsonParser json, DeserializationContext context) throws IOException, JsonProcessingException {
try {
if(StringUtils.isBlank(json.getText())){
return null;
}
return formatter.parseDateTime(json.getText());
} catch (ParseException e) {
return null;
}
}
}

关于java - PropertyEditor 未根据 AJAX (JSON) 请求调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14416300/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com