希望分享使用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格式的消息,所以我需要序列化对象并推送到客户端, 我依赖于更快的杰克逊来做这件事,在这里,
答案 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;
}