2013-10-03 80 views
3

我有一個一流的,我有一個自定義解串器註釋:如何在端點類上實現自定義@JsonDeserialize方法?

@JsonDeserialize(using = ConvertToNullDeserializer.class) 
public void setObject(Integer pObj) { 
    this.pObj = pObj; 
} 

當這個對象是通過AJAX發送到雲終端,我想自定義deserailizer火,而是我只是得到:

{ 
    "error" : { 
"message" : "com.google.appengine.repackaged.org.codehaus.jackson.map.JsonMappingException: Can not construct instance of java.lang.Integer from String value 'null': not a valid Integer value\n at [Source: N/A; line: -1, column: -1] (through reference chain: 
"code" : 400, 
"errors" : [ { 
    "domain" : "global", 
    "reason" : "badRequest", 
    "message" : "com.google.appengine.repackaged.org.codehaus.jackson.map.JsonMappingException: Can not construct instance of java.lang.Integer from String value 'null': not a valid Integer value\n at [Source: N/A; line: -1, column: -1] (through reference chain: 
    } ] 
} 
} 

謝謝!

+0

我不知道我完全理解你的問題,雲終端一般需要照顧的序列化和反序列化的你。也許@JsonDeserialize註釋不是你想要的路徑,因爲通過端點你可以將json相關的東西委託給系統。你的解串器做什麼? – loosebazooka

回答

1

@JsonDeserialize在端點內不受支持,但您可以改爲使用@ApiTransformer

標記的轉化用類:

import com.google.api.server.spi.config.ApiTransformer; 

@ApiTransformer(BarTransformer.class) 
public class Bar { 
    private final int x; 
    private final int y; 

    public Bar(int x, int y) { 
    this.x = x; 
    this.y = y; 
    } 

    public int getX() { 
    return x; 
    } 

    public int getY() { 
    return y; 
    } 
} 

例變壓器:

import com.google.api.server.spi.config.Transformer; 

public class BarTransformer implements Transformer<Bar, String> { 
    public String transformTo(Bar in) { 
    return in.getX() + "," + in.getY(); 
    } 

    public Bar transformFrom(String in) { 
    String[] xy = in.split(","); 
    return new Bar(Integer.parseInt(xy[0]), Integer.parseInt(xy[1])); 
    } 
} 
+0

這適用於大多數情況,但是當你有一個動態json請求中斷了模型(即一個字段可以是一個字符串或一個對象) – Patrick

相關問題