【问题标题】:Making java class for soap xml string为soap xml字符串制作java类
【发布时间】:2020-06-28 07:55:22
【问题描述】:

如何将此 xml 字符串转换为 java 对象?我尝试了各种各样的东西,也许是xml的格式?

我使用的是 java 11,我通过发送一个 post 请求得到了一个 SOAP 响应。我希望能够使用 outXML 字符串。我确实有其他的信封、正文、传输响应、outXML 类。但我希望至少能够先调用根。

错误:

javax.xml.bind.UnmarshalException: unexpected element 
(uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Envelope"). 
Expected elements are <{http://schemas.xmlsoap.org/soap/envelope/}SOAP-ENV:Envelope>

xml 文件

<?xml version="1.0"?>
<SOAP-ENV:Envelope 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 SOAP-ENC:encodingStyle="http://schemas.xmlsoap.org/soap/envelope/">
        <NS1:TransmitResponse xmlns:NS1="urn:TransmitterIntf-ITransmitter">
            <return xsi:type="xsd:int">0</return>
            <outXML xsi:type="xsd:string">message</outXML>
        </NS1:TransmitResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

信封类:

package com.example

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

@XmlType(name = "", propOrder = { "body" })
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "SOAP-ENV:Envelope", namespace = "http://schemas.xmlsoap.org/soap/envelope/")
public class Envelope {
    
    @XmlElement(name = "SOAP-ENV:Body")
    String body;

    // Getter Methods

    public String getBody() {
        return body;
    }

    // Setter Methods

    public void setBody(String body) {
        this.body = body;
    }

}

实施:

            JAXBContext jaxbContext = JAXBContext.newInstance( Envelope.class );
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            StringBuffer xmlStr = new StringBuffer( response.getBody() );
            
            Envelope itransmit = (Envelope) jaxbUnmarshaller.unmarshal( new StreamSource( new StringReader(xmlStr.toString())));
            
            System.out.println(itransmit.getBody());
            return response.toString();

【问题讨论】:

    标签: java xml spring web-services jaxb


    【解决方案1】:

    生成类的最简单方法是使用 wsdl to java 插件。例如,如果您使用 CXF 和 maven,您可以使用 https://cxf.apache.org/docs/maven-cxf-codegen-plugin-wsdl-to-java.html。这将为您生成 java 代码。

    你也可以试试,这是未经测试的代码:

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlRootElement(name = "Envelope", namespace = "http://schemas.xmlsoap.org/soap/envelope/")
    public class Envelope {
        
        @XmlElement(name = "Body", namespace="http://schemas.xmlsoap.org/soap/envelope/")
        String body;
    
        // Getter Methods
    
        public String getBody() {
            return body;
        }
    
        // Setter Methods
    
        public void setBody(String body) {
            this.body = body;
        }
    
    }
    

    【讨论】:

    • 感谢您的回复! 1. 很遗憾,我无法访问 wsdl 文件。我们签署请求和一切。在 outXML 内部,我们拥有带有所需信息的加密真实响应。 2.我改成你显示的样子了,再也没有错误了!但是,我有一个新错误:nulljava.lang.NullPointerException。 xml字符串是完整的但是程序好像找不到?
    • 暂时忘记它是网络服务。尝试通过 jaxb 仅序列化/反序列化 xml 并查看映射是否正确。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-08
    • 1970-01-01
    • 2017-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多