5

単純に見え、ウェブ上の他のスレッドに文書化されている問題について、数日間壁に頭をぶつけていたので、誰かが私を助けてくれることを願っています.

Spring 3.1 サーバーと組み合わせて Smart GWT クライアント (3.0) を使用し、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 )

したがって、私のコードでは次のことを行いました。

  1. CustomObjectMapper を構成しました:

` public class CustomObjectMapper extends 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);
}

} `

  1. したがって、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>

`

  1. Bean オブジェクト:

` import java.util.Date;

@JsonAutoDetect パブリック クラス コメント {

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

`

編集:

  1. コントローラ クラス

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

これに関するヘルプは大歓迎です。

乾杯、スティーブ

4

3 に答える 3

13

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

于 2012-07-05T12:54:49.780 に答える
0

私が間違っているかもしれませんが、私が覚えている限り、Z は ISOwhoknowswhatformat のタイムゾーンを表します。そして、それは4文字幅なので、これを試してみます:

new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZ");

ちなみに、これが問題である場合は、単体テストでキャッチする必要があります。単体テストCustomObjectMapperはありますよね?:P

于 2012-06-27T10:35:32.353 に答える