【问题标题】:SpringBoot multiple @Configuration and SOAP clientsSpring Boot 多个 @Configuration 和 SOAP 客户端
【发布时间】:2015-06-21 14:49:26
【问题描述】:

我已经尝试按照这个简单的教程https://spring.io/guides/gs/consuming-web-service/ 进行操作,并且效果很好。

然后我尝试使用附加的@Configuration 和扩展WebServiceGatewaySupport 的客户端类连接到另一个SOAP 服务。似乎两个客户端类然后使用相同的@Configuration-class,使我添加的第一个失败(未知的 jaxb-context 等)。如何确保客户端类使用正确的@Configuration-class?

【问题讨论】:

    标签: spring-boot spring-ws


    【解决方案1】:

    TL;DR:您必须将“marshaller()”方法的名称与客户端配置中的参数变量名称相匹配。

    覆盖 bean 定义

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

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

    “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;
    }
    

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

    INFO 7 --- [main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'marshaller'
    

    解决方案

    因此,要创建更多拥有自己的编组器而不发生冲突的客户端,您需要有这样的 @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;
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      我最终在返回WebServiceTemplate 的@Configuration-classes 中创建了@Bean。也就是说,我不使用 Spring 的自动配置机制。我删除了extend WebserviceGatewaySupport,并使用@Autowired 自动装配在@Configuration 类中创建的WebserviceTemplateBean bean。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-26
        相关资源
        最近更新 更多