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

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

我开始使用 Spring Boot 学习和实现 SOAP 客户端,以使用来自 SOAP 服务的一些数据。

我无法像这样从 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");
    }

}

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

最佳答案

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

wsimport -keep -verbose <wsdl url>

引用:Sample

关于java - 如何从 XML Soap 响应手动创建 POJO 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56164989/

相关文章:

java - Tomcat 7 - 连接重置

java - 使用 RestTemplate、查询参数和请求正文进行 POST

spring - Docker-compose从链接迁移到网络MongoDB数据库问题

spring-boot - Spring Boot 2.0 中 Spring Boot 集成 java shell ('CRaSH' 的后继者?

wcf - OperationContext.Current 的值不是此OperationContextScope 安装的OperationContext 值

java - SOAP 消息处理程序

java - 如何在 Java 中写入锁定文件?

java - 将 map 转换为 map 列表

java - 我怎样才能归还我的卡片阵列?

wsdl - org.apache.cxf.service.factory.ReflectionServiceFactoryBean 是否可以配置为缓存 WSDL?