java - Spring ws - 每个属性都返回 null

标签 java spring spring-boot soap spring-ws

我到处搜索,但找不到这个问题的答案。
我正在使用 spring-ws 创建一个可以使用 SOAP 消息的 url。我关注了Producing a SOAP web service Spring 教程。

当我向该对象发送值时,所有属性均为空。我想要消息属性的值。我似乎无法弄清楚为什么所有属性都为空。
端点如下所示:

@PayloadRoot(namespace = NAMESPACE_URI, localPart = "UICMessage")
@ResponsePayload
public void getMessage(@RequestPayload JAXBElement<UICMessage> request) {
    System.out.println(request.getValue());

    JAXBContext jaxbContext;
    try {
        jaxbContext = JAXBContext.newInstance(UICMessage.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.setProperty("com.sun.xml.bind.characterEscapeHandler", (CharacterEscapeHandler) (ch, start, length, isAttVal, out) -> out.write(ch, start, length));
        StringWriter sw = new StringWriter();
        jaxbMarshaller.marshal(request, sw);
        String xmlString = sw.toString();

        System.out.println(xmlString);

        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        UICMessage uicMessage = (UICMessage) unmarshaller.unmarshal(new StringSource(xmlString));            

        System.out.println(uicMessage.getMessage());
    } catch (JAXBException e) {
        e.printStackTrace();
    }
}

我在 POST 请求中发送到上面/ws/ci 端点的 SOAP 消息

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:head="http://uic.cc.org/UICMessage/Header" xmlns:uic="http://uic.cc.org/UICMessage">    
   <soapenv:Header>    
      <head:signed>false</head:signed>   
      <head:encrypted>false</head:encrypted>    
      <head:compressed>false</head:compressed>    
      <head:messageIdentifier>xxx</head:messageIdentifier>    
   </soapenv:Header>    
   <soapenv:Body>    
      <uic:UICMessage>      
         <message>    
             <TrainCompositionMessage xmlns="http://taf-jsg.info/schemes" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">XMLVALUE</TrainCompositionMessage>    
          </message>   
          <senderAlias>USER</senderAlias>  
      </uic:UICMessage>    
   </soapenv:Body>    
</soapenv:Envelope>

我从端点获取的控制台日志。
首先,我尝试立即记录属性。所有属性均为空。接下来,我尝试再次将它们编码为字符串,属性是可见且良好的。
接下来,我尝试再次对它们进行解码,所有属性再次为空。

UICMessage{message=[message: null], signature=null, senderAlias=[senderAlias: null], encoding=null}

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:UICMessage xmlns:ns2="http://uic.cc.org/UICMessage">
    <message xmlns:head="http://uic.cc.org/UICMessage/Header" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:uic="http://uic.cc.org/UICMessage">
        <TrainCompositionMessage xmlns="http://taf-jsg.info/schemes" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">XMLVALUE</TrainCompositionMessage>    
    </message>
    <senderAlias xmlns:head="http://uic.cc.org/UICMessage/Header" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:uic="http://uic.cc.org/UICMessage">USER</senderAlias>
</ns2:UICMessage>

[message: null]

我使用的 wsdl 看起来像这样:

<wsdl:definitions xmlns:ns1="http://uic.cc.org/UICMessage/Header" xmlns:ns2="http://schemas.xmlsoap.org/soap/http"
                  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://uic.cc.org/UICMessage"
                  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                  name="LIReceiveMessageService" targetNamespace="http://uic.cc.org/UICMessage">
    <wsdl:documentation>
        This WSDL describes the communication protocol for sending TAF/TAP -TSI messages with standard xml Header node
        according TAF/TAP TSI Common Schema to partner using the Common Interface (CI).For more detailed Information
        please refer to the specification document
    </wsdl:documentation>
    <wsdl:types>
        <xs:schema xmlns:tns="http://uic.cc.org/UICMessage" xmlns:xs="http://www.w3.org/2001/XMLSchema"
                   elementFormDefault="unqualified" targetNamespace="http://uic.cc.org/UICMessage" version="1.0">
            <xs:element name="UICMessage" type="tns:UICMessage"/>
            <xs:element name="UICMessageResponse" type="tns:UICMessageResponse"/>
            <xs:complexType name="UICMessage">
                <xs:sequence>
                    <xs:element minOccurs="0" name="message" type="xs:anyType"/>
                    <xs:element minOccurs="0" name="signature" type="xs:anyType"/>
                    <xs:element minOccurs="0" name="senderAlias" type="xs:anyType"/>
                    <xs:element minOccurs="0" name="encoding" type="xs:anyType"/>
                </xs:sequence>
            </xs:complexType>
            <xs:complexType name="UICMessageResponse">
                <xs:sequence>
                    <xs:element minOccurs="0" name="return" type="xs:anyType"/>
                </xs:sequence>
            </xs:complexType>
        </xs:schema>
        <xsd:schema xmlns="http://uic.cc.org/UICMessage/Header" xmlns:tns="http://uic.cc.org/UICMessage"
                    xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified"
                    elementFormDefault="unqualified" targetNamespace="http://uic.cc.org/UICMessage/Header">
            <xsd:element name="messageIdentifier" nillable="true" type="xsd:string"/>
            <xsd:element name="messageLiHost" nillable="true" type="xsd:string"/>
            <xsd:element name="compressed" nillable="true" type="xsd:boolean"/>
            <xsd:element name="encrypted" nillable="true" type="xsd:boolean"/>
            <xsd:element name="signed" nillable="true" type="xsd:boolean"/>
        </xsd:schema>
    </wsdl:types>
    <wsdl:message name="UICMessage">
        <wsdl:part element="tns:UICMessage" name="parameters"></wsdl:part>
        <wsdl:part element="ns1:messageIdentifier" name="messageIdentifier"></wsdl:part>
        <wsdl:part element="ns1:messageLiHost" name="messageLiHost"></wsdl:part>
        <wsdl:part element="ns1:compressed" name="compressed"></wsdl:part>
        <wsdl:part element="ns1:encrypted" name="encrypted"></wsdl:part>
        <wsdl:part element="ns1:signed" name="signed"></wsdl:part>
    </wsdl:message>
    <wsdl:message name="UICMessageResponse">
        <wsdl:part element="tns:UICMessageResponse" name="parameters"></wsdl:part>
    </wsdl:message>
    <wsdl:portType name="UICReceiveMessage">
        <wsdl:operation name="UICMessage">
            <wsdl:documentation>
                This operation is used to send the message to the Remote CI.
            </wsdl:documentation>
            <wsdl:input message="tns:UICMessage" name="UICMessage"></wsdl:input>
            <wsdl:output message="tns:UICMessageResponse" name="UICMessageResponse"></wsdl:output>
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="LIReceiveMessageServiceSoapBinding" type="tns:UICReceiveMessage">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="UICMessage">
            <soap:operation soapAction="" style="document"/>
            <wsdl:input name="UICMessage">
                <soap:header message="tns:UICMessage" part="messageIdentifier" use="literal"></soap:header>
                <soap:header message="tns:UICMessage" part="messageLiHost" use="literal"></soap:header>
                <soap:header message="tns:UICMessage" part="compressed" use="literal"></soap:header>
                <soap:header message="tns:UICMessage" part="encrypted" use="literal"></soap:header>
                <soap:header message="tns:UICMessage" part="signed" use="literal"></soap:header>
                <soap:body parts="parameters" use="literal"/>
            </wsdl:input>
            <wsdl:output name="UICMessageResponse">
                <soap:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="LIReceiveMessageService">
        <wsdl:port binding="tns:LIReceiveMessageServiceSoapBinding" name="UICReceiveMessagePort">
            <soap:address
                    location="http://EXTERNALLOCATION"/>
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

有人可以帮我解决这个问题吗?我觉得这真的很奇怪。

最佳答案

目前我们以一种非常肮脏的方式解决了这个问题。我们首先将消息转换为 XML,然后使用子字符串获取正确的部分。接下来我们用 jaxb 将我们需要的部分转换回对象。如果有人知道一个干净的方法来做到这一点,请告诉我。

jaxbContext = JAXBContext.newInstance(UICMessage.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.setProperty("com.sun.xml.bind.characterEscapeHandler", (CharacterEscapeHandler) (ch, start, length, isAttVal, out) -> out.write(ch, start, length));
StringWriter sw = new StringWriter();
jaxbMarshaller.marshal(request, sw);
String xmlString = sw.toString();
String hackedString = xmlString.substring(xmlString.indexOf("<TrainCompositionMessage"), xmlString.indexOf("</message"));
jaxbContext = JAXBContext.newInstance(TrainCompositionMessage.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
TrainCompositionMessage errorMessage = (TrainCompositionMessage) unmarshaller.unmarshal(new StringSource(hackedString));

关于java - Spring ws - 每个属性都返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61636173/

相关文章:

java - 如何在 Jtree 中刷新 XML

java - 使用 JVM 内存默认值运行 Java Web 应用程序

Java 继承和泛型

java - 无法从字符串值实例化类型 [简单类型,类 java.time.LocalDate] 的值

java - 如何将十进制转换为以空格分隔千位

java - Spring MVC PATCH方法: partial updates

java - Spring 启动: running a multi module project

mysql - Google Cloud SQL 上的 Spring Boot - org.hibernate.HibernateException : Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

Spring Boot Data JPA - 修改更新查询 - 刷新持久性上下文

java - 无法执行目标 org.springframework.boot :spring-boot-maven-plugin:2. 2.5.RELEASE:run (default-cli)