[英]Using custom JAXB context with Jersey client
I'm using Jersey client 1.8 + JAXB. 我正在使用Jersey客户端1.8 + JAXB。 I want to post a JSON payload that looks like the following:
我想发布一个如下所示的JSON负载:
{
"transactions": [
{
"amount":
{
"currency":"EUR","total":"1"
}
...
}
],
}
I have a DTO that looks like this: 我有一个如下所示的DTO:
@XmlRootElement
public class PaymentRequestDTO
{
@XmlElement(name="transactions")
public List<TransactionDTO> getTransactions()
{
return transactions;
}
}
When the list contains only one transaction though what I post is: 当列表仅包含一项交易时,尽管我发布的内容是:
{
"transactions": {
"amount":
{
"currency":"EUR","total":"1"
}
...
}
}
Transactions are not treated as array... I've lost couple of hours to research this issue incl. 事务不被视为数组...我已经花了几个小时来研究此问题,包括。 lots of searching in SO and I've read that I have to register my own context resolver -> here .
地段SO搜索,我读过,我必须要注册自己的上下文解析器- > 点击这里 。
So here it is: 所以这里是:
@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;
}
}
The registration itself: 注册本身:
ClientConfig cc = new DefaultClientConfig();
cc.getClasses().add(XXXJAXBContextResolver.class);
client = Client.create(cc);
I've used the so called natural notation: 我使用了所谓的自然符号:
The natural JSON notation, leveraging closely-coupled JAXB RI integration.
利用紧密耦合的JAXB RI集成的自然JSON表示法。
Example JSON expression:
JSON表达式示例:
{"columns":[{"id":"userid","label":"UserID"},{"id":"name","label":"User Name"}],"rows":[{"userid":1621,"name":"Grotefend"}]}
{“ columns”:[{“ id”:“ userid”,“ label”:“ UserID”},{“ id”:“ name”,“ label”:“ User Name”}]],“ rows”:[{ “用户id”:1621年, “名”: “Grotefend”}]}
Still the same result - no array in the payload. 结果仍然相同-有效负载中没有数组。 Am I missing something?
我想念什么吗?
For information: 有关信息:
I've activated the POJO mapping (means that jackson is used underneath) and the JSON generated is what I need...... hmmm... 我已经激活了POJO映射(意味着在下面使用了jackson),生成的JSON是我所需要的……嗯……
ClientConfig cc = new DefaultClientConfig();
cc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.