Json ObjectMapper无法读取Json值

时间:2015-05-31 20:14:59

标签: java json spring jackson

我有一个枚举类

public enum CacheType {
    NONE("none"), NONSTRICT_READ_WRITE("nonrestrictreadwrite"), READ_ONLY("readonly"), READ_WRITE("readwrite");

    private final String text;

    private CacheType(final String text) {
        this.text = text;
    }
    @JsonValue
    public String getText()
    {
        return text;
    }
}

现在我有Table类

public class Table {
    private CacheType cachetype;
    .... getters and setters
}

现在,如果将json发送到spring控制器,则Table的设置正确cachetype。直到现在一切都还好。

现在我在做Unit Test所以我读了json测试文件,如下所示,并将其转换为Object:

import com.fasterxml.jackson.databind.ObjectMapper;
private final ObjectMapper mapper = new ObjectMapper();
table= mapper.readValue(file, Table.class);

此处cachetype始终设为NULL。其余的表属性设置正确。我如何正确阅读cachetype

{
 "tables":[
   {
      "name": "DummyUsers",
      "namespace": "",
      "cachetype": "readwrite"
   },
   {
      "name": "AdminUsers",
      "namespace": "",
      "cachetype": "nonrestrictreadwrite"
   },
   {
      "name": "SuperUsers",
      "namespace": "",
      "cachetype": "none"
   }
  ]
 }

0 个答案:

没有答案