如何使用MOXy请求XMLElement的子集? [英] How do I request a subset of XMLElements using MOXy?
问题描述
我有一个RESTful服务,如果选择器"与请求一起提交,则该服务仅需要返回几个XmlElement.该网址将采用以下形式:
I have a RESTful service that needs to return only a few of the XmlElements if "selectors" are submitted with the request. The URL will take the form of:
/merchants/{merchantId}/profile?selectors=<field1|field2|....|fieldN>
选择器是可选的,到目前为止,我已经实现了为未指定选择器的 {merchantId}
返回的完整元素集的服务.现在,我试图找出如何添加此添加的功能.我确定文档中已涵盖了此内容,但找不到位置.任何RTFM指针,将不胜感激.谢谢.
The selectors are optional, and so far I have implemented the service for the full set of elements to be returned for {merchantId}
without selectors specified. Now I'm trying to figure out how to add in this added functionality. I'm sure this is covered in documentation but I can't find where. Any RTFM pointers would be appreciated. Thanks.
推荐答案
EclipseLink JAXB(MOXy) 当前不提供一种机制来选择性地指示每个元组操作中包括哪些字段/属性.这听起来像是一个有趣的用例.如果您可以使用以下链接将其输入为增强请求,我们将不胜感激:
EclipseLink JAXB (MOXy) does not currently offer a mechanism to selectively indicate which fields/properties are included on a per marshal operation. This sounds like an interesting use case. I would appreciate if you could enter this as an enhancement request using the following link:
以下是通过利用 http://blog.bdoughan.com/2012/04/binding-to-json-xml-handling-null.html ).
FieldAdapter
由于我们将利用有状态的XmlAdapters,因此每个字段将需要一个.由于我们所有的XmlAdapter都将执行相同的逻辑,因此我们可以创建其他类可以从其扩展的超类.
Since we are going to leverage stateful XmlAdapters we're going to need one per field. Since all our XmlAdapters will perform the same logic we can create a super class that the others can extend from.
package forum13094195;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class FieldAdapter<T> extends XmlAdapter<T, T> {
private boolean include;
public FieldAdapter() {
this.include = true;
}
public FieldAdapter(boolean include) {
this.include = include;
}
@Override
public T marshal(T value) throws Exception {
if(include) {
return value;
}
return null;
}
@Override
public T unmarshal(T value) throws Exception {
return value;
}
}
Field1Adapter
package forum13094195;
public class Field1Adapter extends FieldAdapter<String> {
public Field1Adapter() {}
public Field1Adapter(boolean include) {
super(include);
}
}
Field2Adapter
package forum13094195;
public class Field2Adapter extends FieldAdapter<Integer>{
public Field2Adapter() {}
public Field2Adapter(boolean include) {
super(include);
}
}
Field3Adapter
package forum13094195;
public class Field3Adapter extends FieldAdapter<String> {
public Field3Adapter() {}
public Field3Adapter(boolean include) {
super(include);
}
}
商人
@XmlJavaTypeAdapter
批注用于在字段/属性上指定 XmlAdapter
.
The @XmlJavaTypeAdapter
annotation is used to specify an XmlAdapter
on a field/property.
package forum13094195;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Merchant {
@XmlJavaTypeAdapter(Field1Adapter.class)
String field1;
@XmlJavaTypeAdapter(Field2Adapter.class)
int field2;
@XmlJavaTypeAdapter(Field3Adapter.class)
String field3;
}
演示
下面的演示代码演示了如何在 Marshaller
上设置有状态的 XmlAdapter
.
The demo code below demonstrates how to set a stateful XmlAdapter
on the Marshaller
.
package forum13094195;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Merchant.class);
Merchant merchant = new Merchant();
merchant.field1 = "A";
merchant.field2 = 2;
merchant.field3 = "C";
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(merchant, System.out);
marshaller.setAdapter(new Field1Adapter(false));
marshaller.setAdapter(new Field2Adapter(false));
marshaller.setAdapter(new Field3Adapter(true));
marshaller.marshal(merchant, System.out);
}
}
输出
下面是运行演示代码的输出.默认情况下,将整个对象编组.编组的第二个文档不包含我们排除的字段.
Below is the output from running the demo code. By default the entire object is marshalled out. The second document marshalled does not contain the fields we excluded.
<?xml version="1.0" encoding="UTF-8"?>
<merchant>
<field1>A</field1>
<field2>2</field2>
<field3>C</field3>
</merchant>
<?xml version="1.0" encoding="UTF-8"?>
<merchant>
<field3>C</field3>
</merchant>
这篇关于如何使用MOXy请求XMLElement的子集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!