如何在spring soap客户端上添加自定义标头

时间:2017-01-03 11:43:26

标签: java spring soap client spring-ws

您好我想向spring soap客户端添加标头,我有一个ClientAppConfig类,如下所示;

@Configuration
public class ClientAppConfig {

    @Bean
    public Wss4jSecurityInterceptor wss4jSecurityInterceptor() {
        Wss4jSecurityInterceptor interceptor = new Wss4jSecurityInterceptor();
        interceptor.setSecurementActions("Timestamp");
        interceptor.setSecurementUsername("user");
        interceptor.setSecurementPassword("*******");
        return interceptor;
    }

    @Bean
    public Jaxb2Marshaller marshaller() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setPackagesToScan("com");

        return marshaller;
    }

    @Bean
    public SomeClient someClient(Jaxb2Marshaller marshaller) {
        SomeClient client = new SomeClient();
        client.setDefaultUri("http://test");
        client.setMarshaller(marshaller);
        client.setUnmarshaller(marshaller);

        return client;
    }
}

示例客户端:

@Component
public class SomeClient extends WebServiceGatewaySupport {

    public SomeResponse someMethod(ArrayOfLong arrayOfLong) {
        SomeRequest request = new SomeRequest();
        request.setsomeId(arrayOfLong);
        SomeResponse response = (SomeResponse) getWebServiceTemplate()
                .marshalSendAndReceive(request, new SoapActionCallback(
                        "http://soapaction"));
        return response;
    }
}

我有这样的wsdl请求;

<soapenv:Envelope xmlns:soapenv="http://envelope" xmlns:v1="http://Service">
   <soapenv:Header xmlns:as="http://Schema">
        <wsse:Security xmlns:wsse="http://wss-wssecurity-secext-1.0.xsd">
            <wsse:UsernameToken>
                <wsse:Username>user</wsse:Username>
                <wsse:Password>********</wsse:Password>
            </wsse:UsernameToken>
        </wsse:Security>
        <as:consumerContext xsi:type="as:IntWebAppContextType" xmlns:as="http://Schema" xmlns:xsi="http://XMLSchema-instance">

            <consumerCode>someCode</consumerCode>
        </as:consumerContext>
    </soapenv:Header>
   <soapenv:Body>
      <v1:someReceived xmlns:ns3="http://Service" xmlns:ns2="http://Schema">
         <v1:parameters>
            <!--Optional:-->
            <v1:someId>
               <!--Zero or more repetitions:-->
               <v1:long>111111111</v1:long>
            </v1:someId>
         </v1:parameters>
      </v1:someReceived>
   </soapenv:Body>
</soapenv:Envelope>

我添加了用户名和密码,但我必须添加as:consumerContext部分,我需要获得consumerCode以获得其他明智的响应我得到错误。我怎样才能获得带有spring ws的consumerCode

1 个答案:

答案 0 :(得分:1)

如果在Windows中使用firebase-app生成JAVA类,则为:

wsimport

您将生成wsimport -keep -verbose http://compA.com/ws/server?wsdl ,您可以填充有效数据,使用ConsumerContext.javaHeader.java中设置并触发请求。

您可以在此处找到类似的实现 - writing-and-consuming-soap-webservice-with-spring,其源代码位于GitHub