使用Java将请求对象设置为JAXBElement

时间:2015-04-20 10:25:54

标签: java jaxb

我尝试使用java设置一个请求对象。但是,它要求它只接受JAXBElement对象,我不确定如何创建这样的请求对象。我的请求对象有一组setter&干将。这是我的代码

LOSRequest losRequest = new LOSRequest();
losRequest.setData(request);

可能的解决方案:setData(javax.xml.bind.JAXBElement)

这是LOSRequest

@javax.xml.bind.annotation.XmlAccessorType(javax.xml.bind.annotation.XmlAccessType.FIELD)
@javax.xml.bind.annotation.XmlType(name = "LOSRequest", propOrder = {"data", "requestid", "userCtx"})
public class LOSRequest {
@javax.xml.bind.annotation.XmlElementRef(name = "data", namespace = "http://to.service.los/xsd", type = javax.xml.bind.JAXBElement.class)
protected javax.xml.bind.JAXBElement<java.lang.String> data;
@javax.xml.bind.annotation.XmlElementRef(name = "requestid", namespace = "http://to.service.los/xsd", type = javax.xml.bind.JAXBElement.class)
protected javax.xml.bind.JAXBElement<java.lang.String> requestid;
@javax.xml.bind.annotation.XmlElementRef(name = "userCtx", namespace = "http://to.service.los/xsd", type = javax.xml.bind.JAXBElement.class)
protected javax.xml.bind.JAXBElement<los.service.to.xsd.LOSUserContext> userCtx;

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

使用带注释的成员:

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "LOSRequest", propOrder = {"data", "requestid", "userCtx"})
public class LOSRequest {

    @XmlElementRef(name = "data", namespace = "http://to.service.los/xsd", type = JAXBElement.class)
    protected String data;

    // Getter/Setter
}

然后你应该能够做到

LOSRequest l = new LOSRequest();
l.setData("foo");

修改

这与成员被包装的代码不同:

protected JAXBElement<String> data;

这种包装当然使得无法将简单的String设置为data成员的值。