3

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

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

当此对象通过 ajax 发送到云端点时,我希望触发自定义脱轨器,但我只是得到:

{
  "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: 
   } ]
 }
}

谢谢!

4

1 回答 1

1

@JsonDeserializeEndpoints 中不支持,但您可以使用 an@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]));
  }
}
于 2013-10-17T18:02:41.510 回答