杰克逊在地图中捕获未被识别的领域

时间:2017-05-31 15:08:34

标签: java jersey jackson

我在java Rest Api中使用Jackson来处理请求参数。

My Bean课程:

public class ZoneModifBeanParam extends ModifBeanParam<Zone> {
@FormParam("type")
private String type;

@FormParam("geometry")
private Geometry geometry;

@FormParam("name")
private String name;

...

我的API界面:

@POST
@Consumes("application/json")
@Produces("application/json; subtype=geojson")
@ApiOperation(value = "Create a zone", notes = "To create a zone")
public Response createZone(ZoneModifBeanParam zoneParam) {

...

这个工作正常但我需要接收我的Bean在地图中未指定的其他参数。 示例:

{
   "geometry": {...},
   "name": "A circle name",
   "type": "4",
   "hello": true
}

通过接收这个,我需要存储一个Map(名为unrecognizedFields并在我的bean中声明)这对夫妇(“你好”,真实)。

是否有任何注释或对象允许这样做?

2 个答案:

答案 0 :(得分:5)

只需使用@JsonAnySetter即可。这就是它的成就。这是一个测试用例

public class JacksonTest {

    public static class Bean {
        private String name;
        public String getName() { return this.name; }
        public void setName(String name) { this.name = name; }

        private Map<String, Object> unrecognizedFields = new HashMap<>();

        @JsonAnyGetter
        public Map<String, Object> getUnrecognizedFields() {
            return this.unrecognizedFields;
        }

        @JsonAnySetter
        public void setUnrecognizedFields(String key, Object value) {
            this.unrecognizedFields.put(key, value);
        }
    }

    private final String json
            = "{\"name\":\"paul\",\"age\":600,\"nickname\":\"peeskillet\"}";
    private final ObjectMapper mapper = new ObjectMapper();

    @Test
    public void testDeserialization() throws Exception {
        final Bean bean = mapper.readValue(json, Bean.class);
        final Map<String, Object> unrecognizedFields = bean.getUnrecognizedFields();

        assertEquals("paul", bean.getName());
        assertEquals(600, unrecognizedFields.get("age"));
        assertEquals("peeskillet", unrecognizedFields.get("nickname"));
    }
}

@JsonAnyGetter用于序列化方面。序列化bean时,您将看不到JSON中的unrecognizedFields。相反,地图中的所有属性都将序列化为JSON中的顶级属性。

答案 1 :(得分:2)

您可以通过配置ObjectMapper安全地忽略无法识别的字段,但是要专门将它们作为Map字段的键值对,您需要自己的反序列化器。

这是一个(大大简化)的例子:

鉴于您的POJO ......

@JsonDeserialize(using=MyDeserializer.class)
class Foo {
    // no encapsulation for simplicity
    public String name;
    public int value;
    public Map<Object, Object> unrecognized;
}

...以及您的自定义反序列化程序......

class MyDeserializer extends JsonDeserializer<Foo> {
    @Override
    public Foo deserialize(JsonParser p, DeserializationContext ctxt)
            throws IOException, JsonProcessingException {
        // new return object
        Foo foo = new Foo();
        // setting unrecognized container
        Map<Object, Object> unrecognized = new HashMap<>();
        foo.unrecognized = unrecognized;
        // initializing parsing from root node
        JsonNode node = p.getCodec().readTree(p);
        // iterating node fields
        Iterator<Entry<String, JsonNode>> it = node.fields();
        while (it.hasNext()) {
            Entry<String, JsonNode> child = it.next();
            // assigning known fields
            switch (child.getKey()) {
                case "name": {
                    foo.name = child.getValue().asText();
                    break;
                }
                case "value": {
                    foo.value = child.getValue().asInt();
                    break;
                }
                // assigning unknown fields to map
                default: {
                    foo.unrecognized.put(child.getKey(), child.getValue());
                }
            }

        }
        return foo;
    }
}

然后,某处......

ObjectMapper om = new ObjectMapper();
Foo foo = om.readValue("{\"name\":\"foo\",\"value\":42,\"blah\":true}", Foo.class);
System.out.println(foo.unrecognized);

<强>输出

{blah=true}