在表单提交方面,我对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坏请求
问题是,不调用自定义DateTimePropertyEditor (使用@InitBinder注册),也不传递字符串格式的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
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(DateTime.class, this.dateTimeEditor);
}发布于 2013-01-20 15:10:54
我用@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;
}
}
}发布于 2013-01-19 16:54:02
这不是由属性编辑器处理的,它作用于表单字段,而不是json主体。要处理json中的非标准日期格式,您必须自定义基础ObjectMapper。假设您使用的是jackson 2.0+,那么您可以这样做:
a.用一个注释标记publishedSince字段,该注释告诉对象映射程序基于这里指令的日期格式。
public class Article{
...
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="MM.dd.yyyy HH:mm")
private Date publishedSince;
}或者第二个选择是修改ObjectMapper本身,这可能是全局的,所以可能不适用于您:
public class CustomObjectMapper extends ObjectMapper {
public CustomObjectMapper(){
super.setDateFormat(new SimpleDateFormat("MM.dd.yyyy hh:mm"));
}
}并使用Spring对其进行配置:
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="..CustomObjectMapper"/>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>发布于 2015-10-07 03:10:44
使用Spring 4.2.1.RELEASE,您需要使用新的Jackson2依赖项,如下所示,反序列化器才能工作。
不要用这个
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.12</version>
</dependency> 用这个代替。
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.2</version>
</dependency> 还可以使用com.fasterxml.jackson.databind.JsonDeserializer和com.fasterxml.jackson.databind.annotation.JsonDeserialize进行反序列化,而不是使用org.codehaus.jackson的类。
https://stackoverflow.com/questions/14416300
复制相似问题