soapfaultclientexception:找不到标头

ttp71kqs  于 2021-07-16  发布在  Java
关注(0)|答案(1)|浏览(564)

soapweb服务,它接受以下格式的请求-

<?xml version = "1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV = "http://www.w3.org/2001/12/soap-envelope"
                   xmlns:ns="http://...." xmlns:ns1="http://...." xmlns:ns2="http://...."
                   xmlns:ns3="http://....">
    <SOAP-ENV:Header>
        <ns:EMContext>
            <messageId>1</messageId>
            <refToMessageId>ABC123</refToMessageId>
            <session>
                <sessionId>3</sessionId>
                <sessionSequenceNumber>2021-02-24T00:00:00.000+5:00</sessionSequenceNumber>
            </session>
            <invokerRef>CRS</invokerRef>
        </ns:EMContext>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <ns1:getEmployee>
            <ns:empId>111</ns:empId>
        </ns1:getEmployee>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

当尝试使用jaxb2向它发出soap请求时,它给出了 org.springframework.ws.soap.client.SoapFaultClientException: EMContext Header is missing 我正在使用
Spring启动机
spring boot starter web服务
org.jvnet.jaxb2.maven2:maven-jaxb2-plugin:0.14.0

客户-

public class MyClient extends WebServiceGatewaySupport {

    public GetEmployeeResponse getEmployee(String url, Object request){

        GetEmployeeResponse res = (GetEmployeeResponse) getWebServiceTemplate().marshalSendAndReceive(url, request);
        return res;
    }
}

配置-

@Configuration
public class EmpConfig {

    @Bean
    public Jaxb2Marshaller marshaller(){
        Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
        jaxb2Marshaller.setContextPath("com.crsardar.java.soap.client.request");
        return jaxb2Marshaller;
    }

    @Bean
    public MyClient getClient(Jaxb2Marshaller jaxb2Marshaller){
        MyClient myClient = new MyClient();
        myClient.setDefaultUri("http://localhost:8080/ws");
        myClient.setMarshaller(jaxb2Marshaller);
        myClient.setUnmarshaller(jaxb2Marshaller);
        return myClient;
    }
}

应用程序-

@SpringBootApplication
public class App {

    public static void main(String[] args) {

        SpringApplication.run(App.class, args);
    }

    @Bean
    CommandLineRunner lookup(MyClient myClient){

        return args -> {

            GetEmployeeRequest getEmployeeRequest = new GetEmployeeRequest();
            getEmployeeRequest.setId(1);
            GetEmployeeResponse employee = myClient.getEmployee("http://localhost:8080/ws", getEmployeeRequest);
            System.out.println("Response = " + employee.getEmployeeDetails().getName());
        };
    }
}

如何将emcontext头添加到soap请求?

dddzy1tm

dddzy1tm1#

服务器正在抱怨,因为您的web服务客户端没有发送 EMContext soap消息中的soap头。
不幸的是,目前springweb服务缺乏对包含soap头的支持,这种支持与使用jaxb处理soap主体信息的方式类似。
作为解决方法,您可以使用 WebServiceMessageCallback . 从文档中:
为了适应消息上soap头和其他设置的设置,webservicemessagecallback接口允许您在消息创建之后、发送之前访问该消息。
在您的情况下,您可以使用以下内容:

public class MyClient extends WebServiceGatewaySupport {

  public GetEmployeeResponse getEmployee(String url, Object request){

    // Obtain the required information
    String messageId = "1";
    String refToMessageId = "ABC123";
    String sessionId = "3";
    String sessionSequenceNumber = "2021-02-24T00:00:00.000+5:00";
    String invokerRef = "CRS";

    GetEmployeeResponse res = (GetEmployeeResponse) this.getWebServiceTemplate().marshalSendAndReceive(url, request, new WebServiceMessageCallback() {

      @Override
      public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException {
        // Include the SOAP header content for EMContext
        try {
          SoapMessage soapMessage = (SoapMessage)message;
          SoapHeader header = soapMessage.getSoapHeader();
          StringSource headerSource = new StringSource(
            "<EMContext xmlns:ns=\"http://....\">" +
              "<messageId>" + messageId + "</messageId>" +
              "<refToMessageId>" + refToMessageId + "</refToMessageId>" +
              "<session>" +
                "<sessionId>" + sessionId + "</sessionId>" +
                "<sessionSequenceNumber>" + sessionSequenceNumber + "</sessionSequenceNumber>" +
              "</session>" +
              "<invokerRef>" + invokerRef + "</invokerRef>" +
            "</EMContext>"
          );

          Transformer transformer = TransformerFactory.newInstance().newTransformer();
          transformer.transform(headerSource, header.getResult());
        } catch (Exception e) {
          // handle the exception as appropriate
          e.printStackTrace();
        }
      }
    });
    return res;
  }
}

类似的问题也被贴在了so上。例如,考虑一下这个或那个。

相关问题