首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在AJAX (JSON)请求中不调用PropertyEditor

在AJAX (JSON)请求中不调用PropertyEditor
EN

Stack Overflow用户
提问于 2013-01-19 16:24:28
回答 3查看 1.5K关注 0票数 3

在表单提交方面,我对Ajax请求有问题。该表单包含以下字符串JSON数据:

代码语言:javascript
复制
{"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。你知道如何解决这个问题吗?

控制器映射方法,它处理请求

代码语言:javascript
复制
@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

代码语言:javascript
复制
 @InitBinder
        public void initBinder(WebDataBinder binder) {
            binder.registerCustomEditor(DateTime.class, this.dateTimeEditor);
        }
EN

回答 3

Stack Overflow用户

发布于 2013-01-20 15:10:54

我用@JsonDeserialize解决了这个问题

代码语言:javascript
复制
@JsonDeserialize(using=DateTimeDeserializer.class)
public DateTime getPublishedUntil() {
    return publishedUntil;
}

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

代码语言:javascript
复制
    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;
            }
    }
}
票数 5
EN

Stack Overflow用户

发布于 2013-01-19 16:54:02

这不是由属性编辑器处理的,它作用于表单字段,而不是json主体。要处理json中的非标准日期格式,您必须自定义基础ObjectMapper。假设您使用的是jackson 2.0+,那么您可以这样做:

a.用一个注释标记publishedSince字段,该注释告诉对象映射程序基于这里指令的日期格式。

代码语言:javascript
复制
public class Article{
    ...
    @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="MM.dd.yyyy HH:mm")
    private Date publishedSince;
}

或者第二个选择是修改ObjectMapper本身,这可能是全局的,所以可能不适用于您:

代码语言:javascript
复制
public class CustomObjectMapper extends ObjectMapper {
    public CustomObjectMapper(){
        super.setDateFormat(new SimpleDateFormat("MM.dd.yyyy hh:mm"));
    }   
}

并使用Spring对其进行配置:

代码语言:javascript
复制
<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>
票数 2
EN

Stack Overflow用户

发布于 2015-10-07 03:10:44

使用Spring 4.2.1.RELEASE,您需要使用新的Jackson2依赖项,如下所示,反序列化器才能工作。

不要用这个

代码语言:javascript
复制
<dependency>  
            <groupId>org.codehaus.jackson</groupId>  
            <artifactId>jackson-mapper-asl</artifactId>  
            <version>1.9.12</version>  
        </dependency>  

用这个代替。

代码语言:javascript
复制
<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的类。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14416300

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档