如何从XML Soap响应手动创建POJO类?

时间:2019-05-16 09:15:22

标签: java spring-boot soap-client maven-jaxb2-plugin

我开始使用Spring Boot学习和实现SOAP Client,以使用SOAP Service中的一些数据。

我无法像这样从WSDL生成POJO类:

<?xml version="1.0" encoding="ISO-8859-1"?>
<definitions xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:Bakoelbuzz" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="urn:Bakoelbuzz">
    <types>
        <xsd:schema targetNamespace="urn:Bakoelbuzz">
            <xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/" />
            <xsd:import namespace="http://schemas.xmlsoap.org/wsdl/" />
        </xsd:schema>
    </types>
    <message name="mitraInfoRequest"></message>
    <message name="mitraInfoResponse">
        <part name="return" type="xsd:Array" />
    </message>
...

Chrome浏览器上的标签太多了之后 我创建了一个类来发出请求,它可以正常工作

package bbl.wsdl;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "quota"
})
@XmlRootElement(name = "ppMitraInfo")
public class PpMitraInfo {

    @XmlElement(name = "quota", required = true)
    protected String quota;

    public String getQuota() { return quota; }

    public void setQuota(String quota) { this.quota = quota; }

}

它生成这样的请求xml:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header />
    <SOAP-ENV:Body>
        <ns3:ppMitraInfo xmlns:ns3="http://xxxxxx" xmlns:sn3="urn:Bakoelbuzz" />
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我得到了这样的答复:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
    <SOAP-ENV:Body>
        <ns1:ppMitraInfoResponse xmlns:ns1="http://xxxxxx">
            <return>
                <quota xsi:type="xsd:string">7738143573</quota>
                <Description xsi:type="xsd:string">DEVELMODE BY WHO</Description>
            </return>
        </ns1:ppMitraInfoResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我的挫败感仍在继续,如何获取响应数据?
我试图创建一个,只是给了我null

package bbl.wsdl;

import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {
    "quota", "Description"
})
@XmlRootElement(name = "ppMitraInfoResponse")
public class PpMitraInfoResponse {

    @XmlElement(required = true)
    protected String quota;

    @XmlElement(required = true)
    protected String Description;

    public String getQuota() { return quota; }
    public void setQuota(String quota) { this.quota = quota; }

    public String getDescription() { return Description; }
    public void setDescription(String Description) { this.Description = Description; }

    void beforeUnmarshal(Unmarshaller unmarshaller, Object parent) {
        System.out.println("Before Unmarshaller Callback -> ");
    }

    void afterUnmarshal(Unmarshaller unmarshaller, Object parent) {
        System.out.println("After Unmarshaller Callback");
    }

}

如何创建一个处理响应的类?

1 个答案:

答案 0 :(得分:0)

请使用命令提示符并尝试以下尝试使用wsdl url生成请求和响应POJO类:

wsimport -keep -verbose <wsdl url>

以供参考:Sample