2015-05-19 38 views
0

我正在使用Jersey客戶端1.8 + JAXB。我要發佈一個JSON有效載荷,看起來像下面這樣:在Jersey客戶端中使用自定義JAXB上下文

{ 
    "transactions": [ 
    { 
     "amount": 
     { 
      "currency":"EUR","total":"1" 
     } 
     ... 
    } 
    ], 
} 

我有一個DTO,看起來像這樣:

@XmlRootElement 
public class PaymentRequestDTO 
{ 
    @XmlElement(name="transactions") 
    public List<TransactionDTO> getTransactions() 
    { 
     return transactions; 
    } 
} 

當列表中只有一個,雖然我張貼的交易是:

{ 
    "transactions": { 
     "amount": 
     { 
      "currency":"EUR","total":"1" 
     } 
     ... 
    } 
} 

事務不被視爲數組...我已經失去了幾個小時來研究這個問題,很多在SO搜索,我讀過,我必須註冊我自己的上下文解析器 - >here

所以在這裏,它是:

@Provider 
public class XXXJAXBContextResolver implements ContextResolver<JAXBContext> 
{ 
    private JAXBContext context; 

    private Class<?>[] types = { PaymentRequestDTO.class, TransactionDTO.class, AmountDTO.class }; 

    public XXXJAXBContextResolver() throws Exception 
    { 
     this.context = new JSONJAXBContext(JSONConfiguration.natural().build(), types); 
    } 

    @Override 
    public JAXBContext getContext(Class<?> objectType) 
    { 
     for (Class<?> type : types) 
     { 
      if (type == objectType) 
      { 
       return context; 
      } 
     } 
     return null; 
    } 
} 

註冊本身:

ClientConfig cc = new DefaultClientConfig(); 
cc.getClasses().add(XXXJAXBContextResolver.class); 
client = Client.create(cc); 

我使用所謂的自然符號:

自然JSON對象,利用緊密 - 聯合JAXB RI 整合。

例JSON表達:

{ 「列」:[{ 「ID」: 「參數userid」, 「標籤」: 「用戶ID」},{ 「ID」: 「姓名」, 「標籤」:」用戶 名稱 「}],」 行 「:[{」 參數userid 「:1621,」 姓名 「:」 Grotefend「}]}

Link

仍然是相同的結果 - 有效載荷中沒有數組。我錯過了什麼嗎?

回答

0

的信息:

我已經激活了POJO映射(指傑克遜下使用)和產生的JSON是我需要的......嗯......

ClientConfig cc = new DefaultClientConfig(); 
cc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); 
相關問題