使用更快的杰克逊序列化具有 oneToMany 映射的对象

Use faster jackson to serialize object with oneToMany mappings

想分享有关使用 jackson 通过 oneToMany 映射序列化对象并以字符串格式给出响应的知识,

我的Class结构:

@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 格式的消息,所以我需要序列化对象并推送到客户端, 我依靠更快的杰克逊来做这件事,就这样,

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;
}