解组名称间隔SOAP XML参数的问题

时间:2019-03-27 18:21:45

标签: java xml soap

我在处理SOAP请求时遇到问题。我可以从信封中读取所有内容,但无法解析名称空间修饰参数(cs:measurand)。

在这里您可以找到SOAP信封:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:cs="urn://Ocpp/Cs/2012/06/" xmlns:soap-enc="http://www.w3.org/2003/05/soap-encoding" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soap:Header>
        <cs:identity>17083A00001101</cs:identity>
        <a:From>
            <a:Address>http://172.0.0.0:9080</a:Address>
        </a:From>
        <a:MessageID>urn:uuid:xxxxxxxxxxxx</a:MessageID>
        <a:ReplyTo>
            <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
        </a:ReplyTo>
        <a:To>http://172.0.0.0:8080/ws/ocp</a:To>
        <a:Action>/MValues</a:Action>
    </soap:Header>
    <soap:Body>
        <cs:mValuesRequest>
            <cs:id>1</cs:id>
            <cs:transactionId>1881</cs:transactionId>
            <cs:values>
                <cs:timestamp>2019-03-07T13:41:52.405Z</cs:timestamp>
                <cs:value cs:measurand="e.a.i.r" cs:unit="Wh">300</cs:value>
                <cs:value cs:measurand="c.i" cs:unit="Amp">38.5</cs:value>
                <cs:value cs:measurand="v" cs:unit="Volt">399.5</cs:value>
                <cs:value cs:measurand="p.a.i" cs:unit="W">15380</cs:value>
                <cs:value cs:measurand="t" cs:unit="Celsius">35</cs:value>
            </cs:values>
        </cs:mValuesRequest>
    </soap:Body>
</soap:Envelope>

以下是接收请求的服务:

    @Action("/MValues")
    @ResponsePayload
    public JAXBElement<MValuesResponse> receive(@RequestPayload MValuesRequest request,
            MessageContext messageContext) {
....
    }

这是MValuesRequest:

...

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



@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ValuesRequest", propOrder = {
    "id",
    "transactionId",
    "values"
})
public class MValuesRequest {

    protected int id;
    protected Integer transactionId;
    protected List<MValue> values;
// getters setters...


}

您的任何想法都会受到赞赏。

2 个答案:

答案 0 :(得分:0)

尝试一下,

MValuesRequest.java

@XmlRootElement(name="cs:mValuesRequest")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name ="ValuesRequest", propOrder = { "id", "transactionId", "values" })
public class MValuesRequest {

    @XmlElement(name="cs:id")
    protected int id;

    @XmlElement(name="cs:transactionId")
    protected Integer transactionId;

    @XmlElement(name="cs:values")
    protected Values values;
//  getters and setters...
}

Values.java

@XmlRootElement(name="cs:values")
public class Values{

    @XmlElement(name="cs:timestamp")
    protected String timestamp;

    @XmlElement(name="cs:value")
    protected List<Value> value;
//  getters and setters...
}

Value.java

@XmlRootElement(name="cs:value")
public class value{

    @XmlAttribute(name="measurand" namespace="http://www.w3.org/XML/1998/namespace")
    protected String measurand;

    @XmlAttribute(name="unit" namespace="http://www.w3.org/XML/1998/namespace")
    protected String unit;

    @XmlValue
    protected String elementValue;
//  getters and setters...
}

答案 1 :(得分:0)

我终于可以解决这个问题,这要归功于Rathnayake。

我不必添加@XmlRootElement,而只需将名称空间参数添加到@XmlAttribute。

所以目前我的XML参数如下:

    @XmlAttribute(name = "context", namespace="urn://Ocpp/Cs/2012/06/")
    protected ReadingContext context;
    @XmlAttribute(name = "format", namespace="urn://Ocpp/Cs/2012/06/")
    protected ValueFormat format;
    @XmlAttribute(name = "measurand", namespace="urn://Ocpp/Cs/2012/06/")
    protected Measurand measurand;
    @XmlAttribute(name = "location", namespace="urn://Ocpp/Cs/2012/06/")
    protected Location location;
    @XmlAttribute(name = "unit", namespace="urn://Ocpp/Cs/2012/06/")
    protected UnitOfMeasure unit;

记住这里是我的SOAP标头:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:cs="urn://Ocpp/Cs/2012/06/" xmlns:soap-enc="http://www.w3.org/2003/05/soap-encoding" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

然后我从标题中将其添加为namespace =“ ...”值xmlns:cs =“ ...”。