gpt4 book ai didi

json - Spring 3.0 异常在 POST 上将 String 转换为 java.util.Date

转载 作者:行者123 更新时间:2023-12-04 21:53:30 25 4
gpt4 key购买 nike

我希望有人可以帮助我,因为几天来我一直在用头撞墙,这个问题看起来很简单,并且已在网络上的其他线程中记录。

我将 Smart GWT 客户端(3.0)与 Spring 3.1 服务器结合使用,并使用 JSON 进行通信(使用 Jackson API 1.9)。

问题是,当我尝试从 SmartGWT 客户端保存日期并将其发送到服务器时,出现以下异常:

org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'comment' on field 'dateAdded': rejected value [2012-06-27T10:57:47+0100]; codes [typeMismatch.comment.dateAdded,typeMismatch.dateAdded,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [comment.dateAdded,dateAdded]; arguments []; default message [dateAdded]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'dateAdded'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type java.util.Date for value '2012-06-27T10:57:47+0100'; nested exception is java.lang.IllegalArgumentException]
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:110)

我在其他几篇文章中看到了这个问题,但大多数与没有以正确格式格式化日期有关,但我尝试了各种格式:
- yyyy-MM-dd
- yyyy-MM-dd'T'HH:mm:ssZ
- yyyyMMddHHmmssZ(根据这里的建议:http://code.google.com/p/usersapi/issues/detail?id=8)

所以在我的代码中,我做了以下事情:

  • 配置了一个 CustomObjectMapper:

  • `
    公共(public)类 CustomObjectMapper 扩展 ObjectMapper {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");

    public CustomObjectMapper() {
    super();
    configure(Feature.WRITE_DATES_AS_TIMESTAMPS, false);
    setDateFormat(formatter);
    getDeserializationConfig().setDateFormat(formatter);
    }

    }
    `
  • 因此,Spring 应用程序上下文:

  • `
    <mvc:annotation-driven>
    <mvc:message-converters>
    <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
    <constructor-arg ref="jaxbMarshaller" />
    <property name="supportedMediaTypes" value="application/xml"/>
    </bean>
    <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
    <property name="objectMapper" ref="jacksonObjectMapper" />
    <property name="supportedMediaTypes" value="application/json" />
    </bean>
    </mvc:message-converters>
    </mvc:annotation-driven>

    <context:component-scan base-package="com.jpmorgan.creditriskreporting.server" />

    <bean id="marshallingConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
    <constructor-arg ref="jaxbMarshaller" />
    <property name="supportedMediaTypes" value="application/xml"/>
    </bean>


    <bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
    <property name="supportedMediaTypes" value="application/json" />
    <property name="objectMapper" ref="jacksonObjectMapper" />
    </bean>


    <bean id="jacksonObjectMapper" class="com.jpmorgan.creditriskreporting.server.util.CustomObjectMapper" />


    <!-- Client -->
    <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
    <property name="messageConverters">
    <list>
    <ref bean="marshallingConverter" />
    <ref bean="jsonConverter" />
    </list>
    </property>
    </bean>

    `
  • bean 对象:

  • `
    导入 java.util.Date;

    @JsonAutoDetect
    公共(public)类评论{
    private int id;
    private String comment;
    private Date dateAdded;

    public Comment() {}

    public Comment(int id) {
    this.id = id;
    }

    ...
    //@JsonSerialize(using=JsonDateSerializer.class) -- I had previously tried to use these custom Date serializer class
    public Date getDateAdded() {
    return dateAdded;
    }
    //@JsonDeserialize(using=JsonDateDeserializer.class)
    public void setDateAdded(Date dateAdded) {
    this.dateAdded = dateAdded;
    }

    `

    编辑:
  • Controller 类

  • 这可能是问题所在,因为当我使用 @RequestBody 时,它适用于我的集成测试,但是,SmartGWT 中的 Abstract RestDataSource 仅适用于 @ModelAttribute,所以我不确定如何继续。

    @RequestMapping(value="/", method=RequestMethod.POST)
    public @ResponseBody Comment createNewComment2(@ModelAttribute Comment comment) {
    log.info("calling createComment with comment: {}", comment);
    comment.setDateAdded(new Date());
    Comment added = commentDao.create(comment);
    log.info("created comment: {}", added);
    return commentDao.get(comment);

    }

    所以我可以从服务器获取数据,并且日期显示在 SmartGWT 中。只有当我添加数据时,我才会遇到问题。从 Smart GWT 开发者控制台:

    {
    "dataSource":"CommentDS",
    "operationType":"add",
    "componentId":"isc_DynamicForm_1",
    "data":{
    "userAdded":"sharper",
    "dateAdded":"2012-06-27T10:57:47+0100",
    "comment":"sample"
    },
    "callback":{
    "target":[DynamicForm ID:isc_DynamicForm_1],
    "methodName":"saveEditorReply"
    },
    "showPrompt":true,
    "prompt":"Saving form...",
    "oldValues":{
    },
    "clientContext":{
    },
    "requestId":"CommentDS$6272"
    }

    非常感谢您对此的任何帮助。

    干杯,
    史蒂夫

    最佳答案

    感谢http://vkubushyn.wordpress.com/2011/05/31/smart-gwt-restful-spring-mvc,我发现了这个问题

    不得不使用 Spring 的 InitBinder

    @InitBinder
    public void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    dateFormat.setLenient(false);
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }

    关于json - Spring 3.0 异常在 POST 上将 String 转换为 java.util.Date,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11224028/

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