序列化后不能用jersey反序列化json?

时间:2015-03-06 14:31:33

标签: java json serialization jersey

我有这堂课:

public class StatsResult {



    private final EditorUrlGenerator editorUrlGenerator;
    private final LiveMapUrlGenerator liveMapUrlGenerator;

    public String saveDate;

    public Map<String, String> idsToEditorUrls;
    public Map<String, String> idsToLiveMapUrls;

    public List<String> nonIdenticalInstructions;
    public List<String> nonIdenticalRouteNames;

    public List<String> distanceLargeDeltaComparedToBL;
    public List<String> distanceSmallDeltaComparedToBL;

    //TODO: maybe array[Delta][]
    public List<String> timeLargeDeltaComparedToBL;
    public List<String> timeSmallDeltaComparedToBL;

    public List<Integer> idsWithHighLatency;

    public List<LatencyBulk> tillOneSecondBulks;
    public List<LatencyBulk> tillFiveSecondBulks;
    public LatencyBulk moreThanFiveSecondBulks;

我已经序列化了一个实例并成功保存到文件中。

稍后我尝试从这个文件中读取,然后反序列化但是失败了。

org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class com.waze.routing.automation.dto.stats.StatsResult]: can not instantiate from JSON object (need to add/enable type information?)
    at [Source: src/main/resources/latency_recorder; line: 2, column: 3]

文件:

{
  "saveDate" : "04:26:20 06-Mar-02015",
  "idsToEditorUrls" : { },
  "idsToLiveMapUrls" : { },
  "nonIdenticalInstructions" : [ ],
  "nonIdenticalRouteNames" : [ ],
  "distanceLargeDeltaComparedToBL" : [ ],
  "distanceSmallDeltaComparedToBL" : [ ],
  "timeLargeDeltaComparedToBL" : [ ],
  "timeSmallDeltaComparedToBL" : [ ],
  "idsWithHighLatency" : [ ],
  "tillOneSecondBulks" : [ {
    "bulkName" : "[0,0.2) seconds",
    "ids" : [ ],
    "count" : 0,
    "countOfCurrentAndSlower" : 2
  }, {
    "bulkName" : "[0.2,0.4) seconds",
    "ids" : [ ],
    "count" : 0,
    "countOfCurrentAndSlower" : 2
  }, {
    "bulkName" : "[0.4,0.6) seconds",
    "ids" : [ "id:1 millis:563" ],
    "count" : 1,
    "countOfCurrentAndSlower" : 2
  }, {
    "bulkName" : "[0.6,0.8) seconds",
    "ids" : [ ],
    "count" : 0,
    "countOfCurrentAndSlower" : 1
  }, {
    "bulkName" : "[0.8,1) seconds",
    "ids" : [ ],
    "count" : 0,
    "countOfCurrentAndSlower" : 1
  } ],
  "tillFiveSecondBulks" : [ {
    "bulkName" : "[1,1.5) seconds",
    "ids" : [ "id:0 millis:1289" ],
    "count" : 1,
    "countOfCurrentAndSlower" : 1
  }, {
    "bulkName" : "[1.5,2) seconds",
    "ids" : [ ],
    "count" : 0,
    "countOfCurrentAndSlower" : 0
  }, {
    "bulkName" : "[2,2.5) seconds",
    "ids" : [ ],
    "count" : 0,
    "countOfCurrentAndSlower" : 0
  }, {
    "bulkName" : "[2.5,3) seconds",
    "ids" : [ ],
    "count" : 0,
    "countOfCurrentAndSlower" : 0
  }, {
    "bulkName" : "[3,3.5) seconds",
    "ids" : [ ],
    "count" : 0,
    "countOfCurrentAndSlower" : 0
  }, {
    "bulkName" : "[3.5,4) seconds",
    "ids" : [ ],
    "count" : 0,
    "countOfCurrentAndSlower" : 0
  }, {
    "bulkName" : "[4,4.5) seconds",
    "ids" : [ ],
    "count" : 0,
    "countOfCurrentAndSlower" : 0
  }, {
    "bulkName" : "[4.5,5) seconds",
    "ids" : [ ],
    "count" : 0,
    "countOfCurrentAndSlower" : 0
  } ],
  "moreThanFiveSecondBulks" : {
    "bulkName" : "5 seconds",
    "ids" : [ ],
    "count" : 0,
    "countOfCurrentAndSlower" : 0
  }
}

它能成为什么?

1 个答案:

答案 0 :(得分:0)

我想,StatsResult类有一个参数化构造函数? (您在问题中添加的代码似乎已缩短)。如果是的话:

错误消息说,Jersey想要创建StatsResult类的实例,但无法找到合适的构造函数。在大多数情况下,你只需要在你的类中添加一个no-Args构造函数,这样Jersey就可以实现它:

protected StatsResult() {
    // for jersey
}

当然,泽西岛不能为&#34; editorUrlGenerator&#34;设置值。和#34; liveMapUrlGenerator&#34;,所以你需要在no-args构造函数中处理它们(赋值null?)。