如何在 Spring 引导中捕获自定义 SOAP Header localPart 请求

How to catch custom SOAP Header localPart Request in Spring Boot

我为 SOAP Header.

命名了 POJO class RequestSOAPHeader

在@Endpoint 中,我正在捕获如下所示的 header

@SoapHeader("{" + RequestSOAPHeader.AUTH_NS + "}RequestSOAPHeader")SoapHeaderElement auth

我的问题是当我试图在 POJO class 中解组我的 header 时出现以下错误。

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.test.com/schema/common/v2_1", local:"RequestSOAPHeader"). Expected elements are <{http://www.test.com/schema/common/v2_1}requestSOAPHeader>

我正在使用以下方法解组我的 header

 private RequestSOAPHeader getAuthentication(SoapHeaderElement header){
    RequestSOAPHeader authentication = null;
    try {

        JAXBContext context = JAXBContext.newInstance(RequestSOAPHeader.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        authentication = (RequestSOAPHeader) unmarshaller.unmarshal(header.getSource());

    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return authentication;
}

根据错误,我已将我的 header localPart 名称更改为 requestSOAPHeader,它工作正常。

@SoapHeader("{" + RequestSOAPHeader.AUTH_NS + "}requestSOAPHeader")SoapHeaderElement auth

但我想使用 RequestSOAPHeader 作为 header 的 localPart,而不是 requestSOAPHeader

我已经找到解决方法了

只需在 header class 之上添加以下代码段 @annotation 值。希望对你有帮助

@XmlRootElement(namespace = "http://www.gooogle.com", name = "RequestSOAPHeader")
public class RequestSOAPHeader{
....
}