如何在端点类上实现自定义@JsonDeserialize方法?

时间:2013-10-03 20:01:49

标签: java google-app-engine jquery google-cloud-endpoints

我有一个类,我有一个自定义反序列化器注释:

@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: 
   } ]
 }
}

谢谢!

1 个答案:

答案 0 :(得分: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]));
  }
}