2017-09-25 33 views
0

我收到這個錯誤,試圖從Spring RestController反序列化一些XML。Spring XML - 沒有字符串參數構造函數/工廠方法來反序列化字符串值

10:32:20.275 [main] WARN org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not construct instance of com.example.SomeThing: no String-argument constructor/factory method to deserialize from String value ('AAAA') 

這裏是類(改變名稱和包)

public final class SomeThing{ 
    public static final SomeThing AAAA = create("AAAA"); 

    public static SomeThing create(final String value) { 
     SomeThing result = new SomeThing(); 
     result.setValue(value); 
     return result; 
    } 
} 

那麼,如何改變這一類,所以它能夠被反序列化?

回答

1

你應該標記Something#create方法爲工廠方法所以它解析爲方法實例化新Something實例。

這裏是你的類的修改版本(注意,它已經被改變以匹配主OP聲明字段):

public final class SomeThing{ 

    private String val; 

    @JsonCreator 
    public static SomeThing create(final String value) { 
    SomeThing result = new SomeThing(); 
    result.setValue(value); 
    return result; 
    } 

    public void setValue(String value) { 
    this.val = value; 
    } 
} 
相關問題