SpringBoot 多个@Configuration 和 SOAP 客户端

use*_*371 3 spring-ws spring-boot

我尝试遵循这个简单的教程https://spring.io/guides/gs/consuming-web-service/,它有效。

然后,我尝试连接到另一个 SOAP 服务,其中包含附加的@Configuration客户端类扩展WebServiceGatewaySupport. 看来两个客户端类都使用相同的@Configuration类,使得我第一个添加的类失败(未知的 jaxb-context 等)。如何确保客户端类使用正确的@Configuration类?

小智 5

长话短说:您必须将“marshaller()”方法的名称与客户端配置中的 param 变量名称相匹配。

覆盖 bean 定义

发生的情况是,您的两个 @Configuration 类都对 Jaxb2Marshaller 使用相同的 bean 名称,即(使用 spring 示例):

@Bean
public Jaxb2Marshaller marshaller() { //<-- that name
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();     
    marshaller.setContextPath("hello.wsdl");
    return marshaller;
}
Run Code Online (Sandbox Code Playgroud)

其中“marshaller()”方法名称是将在下面注入到客户端的 bean 名称:

@Bean
public QuoteClient quoteClient(Jaxb2Marshaller marshaller) { //<-- used here
    QuoteClient client = new QuoteClient();
    client.setDefaultUri("http://www.webservicex.com/stockquote.asmx");
    client.setMarshaller(marshaller);
    client.setUnmarshaller(marshaller);
    return client;
}
Run Code Online (Sandbox Code Playgroud)

如果您对第二个客户端使用“marshaller()”,Spring 会覆盖该 bean 定义。您可以在日志文件中发现这一点,查找如下内容:

INFO 7 --- [main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'marshaller'
Run Code Online (Sandbox Code Playgroud)

解决方案

因此,要创建更多拥有自己的编组器而不发生冲突的客户端,您需要有一个像这样的@Configuration:

@Configuration
public class ClientBConfiguration {

    @Bean
    public Jaxb2Marshaller marshallerClientB() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setContextPath("hello.wsdl");
        return marshaller;
    }

    @Bean
    public ClientB clientB(Jaxb2Marshaller marshallerClientB) {
        ClientB client = new ClientB();
        client.setDefaultUri("http://www.webservicex.com/stockquote.asmx");
        client.setMarshaller(marshallerClientB);
        client.setUnmarshaller(marshallerClientB);
        return client;
    }

}
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

5060 次

最近记录:

4 年,6 月 前