0

想分享有关使用杰克逊使用 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,它只需要字符串格式的消息,所以我需要序列化对象并推送到客户端,我依靠更快的杰克逊来做到这一点,就这样,

4

1 回答 1

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;
}
于 2015-07-16T10:29:03.803 回答