2016-05-05 7 views
2

に文字列を解析:応答で春、私はこのようなモデルとフィールドを持っているのLocalDateTime

@Element(name = "TIMESTAMP") 
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) 
private LocalDateTime date; 

私が受け取っ:

<TIMESTAMP>2016-05-04T13:13:42.000</TIMESTAMP> 

が、モデル化するために、XMLを解析中に、私はエラーを持っている:

"message": "org.simpleframework.xml.core.PersistenceException: Constructor not matched for class java.time.LocalDateTime", 

私もしてみました

これはまだ動作しません。何か案が ?私はspringframework.xml libを使用しています。

+0

どの版で提供されていますあなたは春のシオンですか? – Enigo

+0

スプリングブート1.2.5.RELEASE、Spring 4.1.7.RELEASE – user3528733

答えて

3

問題は、デフォルトではsimplexml libは新しいJava8の日付タイプをシリアライズ/デシリアライズする方法を知りません。

成功するには、カスタムコンバータを使用する必要があります。

例エンティティ(特別@Convert注釈を参照)

public class Entity { 

    @Element(name = "TIMESTAMP") 
    @Convert(LocalDateTimeConverter.class) 
    private LocalDateTime date; 

    // omitted 
} 

特別コンバータ

public class LocalDateTimeConverter implements Converter<LocalDateTime> { 

public LocalDateTime read(InputNode node) throws Exception { 
    String name = node.getValue(); 
    return LocalDateTime.parse(name, DateTimeFormatter.ISO_LOCAL_DATE_TIME); 
} 

public void write(OutputNode node, LocalDateTime input) { 
    String value = input.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME); 
    node.setValue(value); 
} 
} 

使用

 Strategy strategy = new AnnotationStrategy(); 
     Persister persister = new Persister(strategy); 
     Entity serializedEntity = persister.read(Entity.class, xmlInputStream); 

完全なソースはGitHub

関連する問題