2015-02-07 209 views
0

我試圖反序列化使用傑克遜用下列JSON有效載荷:傑克遜JSON反序列化

[{ 
    "owner" : "345MWyh7w4hY98W6", 
    "url" : "http://www.google.com", 
    "items" : [{ 
    "createdAt" : 1342099411415, 
    "amount" : 1, 
    "note" : "item 1", 
    "product" : "car" 
    }, { 
    "createdAt" : 1342100231111, 
    "amount" : 4, 
    "note" : "item 2", 
    "product" : "wheels" 
    }], 
    "createdAt" : 1342096943777, 
    "title" : "Car order", 
    "description" : "car order", 
    "id" : "98hw85Y945e6U358" 
}] 

我使用下面的代碼進行反序列化:

ObjectMapper mapper = new ObjectMapper().configure(
     DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); 

List<Order> result = null; 
try { 
    result = mapper.readValue(jsonString,new TypeReference<List<Order>>() { }); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

不過,我得到以下錯誤:

org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo 
SEVERE: MessageBodyWriter not found for media type=application/json, type=class org.glassfish.jersey.client.InboundJaxrsResponse, genericTyp 
e=class org.glassfish.jersey.client.InboundJaxrsResponse. 
Feb 06, 2015 8:14:45 PM org.glassfish.jersey.filter.LoggingFilter log 

該類Orders是由RAML - > JAX-RS maven插件生成的,它沒有零參數構造函數。除了修改生成的類以添加零參數構造函數之外,是否有任何方法可以執行此反序列化?

+0

檢查這個http://stackoverflow.com/questions/23601842/jersey-messagebodywriter-not-found-for-media-type-application-json-type-class 你有MessageBodyWrite沒有發現異常。 – 2015-02-07 04:33:20

回答

0

能夠解決問題。我創建了一個包裝DTO在命令如下:

@XmlRootElement 
public class OrderDTO { 

    private List<Order> orderList; 

    public List<Order> getOrderList() { 
     return orderList; 
    } 

    public void setOrderList(List<Order> orderList) { 
     this.orderList = orderList; 
    } 

    public void addOrder(Order order){ 
     orderList.add(order); 
    } 

    public OrderDTO() { 
     super(); 
     orderList = new ArrayList<Order>(); 
    } 
} 

在這裏,我能夠加入零參數的構造函數。現在它工作:)。