0

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 を使用しているため、オブジェクトをシリアル化してクライアントにプッシュする必要があります。より高速な jackson に依存しています。

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 に答える