使用更快的jackson序列化具有oneToMany映射的对象

时间:2015-07-16 10:29:04

标签: java json serialization fasterxml

希望分享使用jackson通过oneToMany映射序列化对象并以字符串格式提供响应的知识,

我的班级结构:

@Entity
public class Order {    
    /*
        All the fields associated with this class
    */      
    @OneToMany(fetch = FetchType.EAGER, mappedBy = "orderId")
    private Set<OrderDetails> orderDetails;

    //Getters for all properties defined in this class as jackson would depend on this thing
}

在我的情况下,我使用的是一个textWebSocket,它只能使用String格式的消息,所以我需要序列化对象并推送到客户端, 我依赖于更快的杰克逊来做这件事,在这里,

1 个答案:

答案 0 :(得分:0)

public String getObjectAsString()
{

    //orderObjs : Considering this is the list of objects of class Order

    ObjectMapper objMapper = new ObjectMapper();
            returnValue = objMapper.writerWithType(
                    objMapper.getTypeFactory().constructCollectionType(
                            List.class, Order.class)).writeValueAsString(
                    orderObjs);
    return returnValue;
}