Jackson JSON不会包装嵌套对象的属性 [英] Jackson JSON do not wrap attributes of nested object

查看:2414
本文介绍了Jackson JSON不会包装嵌套对象的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程:

public class Container {
    private String name;
    private Data data;
}

public class Data {
    private Long id;
}

当我序列化 Container 使用Jackson的课程我得到

When I serialize Container class using Jackson I get

{"name":"Some name","data":{"id":1}}

但我需要的结果是:

{"name":"Some name","id":1}

是否可以(不添加 Container.getDataId()方法)?如果是这样,怎么做?

Is it possible (without adding Container.getDataId() method)? If so, how to do it?

更新

我试图创建自定义 JsonSerializer< Data> 但结果与以前相同

I've tried to create custom JsonSerializer<Data> but the result was same as before

public class JsonDataSerializer extends JsonSerializer<Data> {

    private static Logger logger = Logger.getLogger(JsonDataSerializer.class);

    @Override
    public void serialize(Data value, JsonGenerator jgen, 
            SerializerProvider provider)
            throws IOException,JsonProcessingException {

        Long id = (value.getId() == null) ? 0l : value.getId();
        jgen.writeStartObject();
        jgen.writeNumberField("id", id);
        jgen.writeEndObject();
        logger.debug("Data id " + id + " serialized to JSON.");       
    }
}

我还尝试添加 @JsonSerialize 注释数据类,然后在 Container 类中的getter上方。正如我之前提到的,没有任何成功。使用我的序列化程序,记录器记录消息。

I've also tried to add @JsonSerialize annotation above Data class, then above getter in Container class. As I mentioned before without any success. My serializer is used, logger logs message.

更新2

当我删除 writeStartObject() writeEndObject()时,没有返回JSON ,只有 HTTP状态500 错误,除了我在调试输出中找到的内容外,不会抛出任何异常。

When I remove writeStartObject() and writeEndObject() then no JSON is returnesd, only HTTP Status 500 error and no exception is thrown except of what I found in debug output.

DEBUG: org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver - Resolving exception from handler [com.example.DataController@16be8a0]: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Can not write a field name, expecting a value; nested exception is org.codehaus.jackson.JsonGenerationException: Can not write a field name, expecting a value
DEBUG: org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver - Resolving exception from handler [com.example.DataController@16be8a0]: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Can not write a field name, expecting a value; nested exception is org.codehaus.jackson.JsonGenerationException: Can not write a field name, expecting a value
DEBUG: org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolving exception from handler [com.example.DataController@16be8a0]: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Can not write a field name, expecting a value; nested exception is org.codehaus.jackson.JsonGenerationException: Can not write a field name, expecting a value


推荐答案

Jackson 1.9推出了 JsonUnwrapped 注释,它可以满足您的需求。

Jackson 1.9 has introduced JsonUnwrapped annotation which does what you are looking for.

这篇关于Jackson JSON不会包装嵌套对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆