【问题标题】:integrating generated CXF client jar with Spring Boot fails when cxf-rt-transports-http is missing in pom.xml当 pom.xml 中缺少 cxf-rt-transports-http 时,将生成的 CXF 客户端 jar 与 Spring Boot 集成失败
【发布时间】:2015-10-26 17:39:44
【问题描述】:

我想将 Spring 应用程序迁移到 Spring Boot。原始应用程序由几个 maven 模块组成,我想重用现有模块之一。我要重用的模块是生成的 cxf Web 服务客户端。它在应用程序的pom.xml 中链接,如下所示:

<dependency>
   <groupId>generated.package</groupId>
   <artifactId>cxf-service-adapter</artifactId>
   <version>1.0</version>           
</dependency>

在该模块中生成的GenCxfService 接口如下所示

@WebService(targetNamespace = "https://the-domain/the-service/", name = "genCxfService")
@XmlSeeAlso({ObjectFactory.class})
public interface GenCxfService {
    // the anotated web service methods
    // ....
}

我需要 Web 服务客户端接口 GenCxfService 由 Spring Boot 管理,以便我可以将其传递给 Spring Security AuthenticationProvider


好吧,我认为这没什么大不了的。我从模块中取出已编译的cxf-service-adapter-1.0.jar,将其放入自己的项目内存储库中,并尝试将java配置设置为@autowire bean。首先我是这样尝试的:

import generated.package.GenCxfService;
...
@ComponentScan({"generated.package.*","my.package.*"})
...
@Bean
public MyAuthenticationProvider myAuthenticationProvider() {
    JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
    factory.setAddress("https://the-domain/the-service/");
    factory.setServiceClass(GenCxfService.class);
    GenCxfService genCxfService = (GenCxfService) factory.create();
    return new my.package.authentication.MyAuthenticationProvider(userDAO, genCxfService);
}

这在运行时给了我以下异常:

javax.xml.ws.soap.SOAPFaultException: Could not find conduit initiator for address: https://the-domain/the-service/the-wsdl.asmx and transport: http://schemas.xmlsoap.org/soap/http
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:161)
    at com.sun.proxy.$Proxy126.validateMyUser(Unknown Source)
    at my.package.authentication.MyAuthenticationProvider.authenticate(MyAuthenticationProvider.java:47)

我以为我只是有一个错误的 java 配置,并尝试通过重用原始项目模块中的现有 xml 配置来解决问题,使用

@ImportResource({"classpath:web-service.xml"})

带有xml文件:

    <jaxws:client id="genCxfService"
        serviceClass="generated.package.GenCxfService"
        address="https://the-domain/the-service/the-wsdl.asmx">
    </jaxws:client>

    <bean id="logInbound" class="org.apache.cxf.interceptor.LoggingInInterceptor" />

    <cxf:bus>
        <cxf:inInterceptors>
            <ref bean="logInbound" />
        </cxf:inInterceptors>
        <cxf:inFaultInterceptors>
            <ref bean="logInbound" />
        </cxf:inFaultInterceptors>
    </cxf:bus>

这是在cxf documentation (Configuring a Spring Client Option 1) 中配置客户端的方式。尽管如此,我仍然遇到同样的错误。

我做错了什么?

更新

cxf-rt-transports-http 添加到 pom 中就可以了:

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
</dependency>

谁能解释为什么缺少库不会在启动期间而是在运行时引发异常?

提前致谢。

【问题讨论】:

    标签: web-services maven dependency-injection spring-boot cxf


    【解决方案1】:

    Spring boot 将在运行时查找类,因此您不必依赖特定的库,它会使用反射来查找满足其所需接口的类,以便轻松交换实现。

    这些类也是延迟启动或查找的,因此它仅在发送请求时发生,而不是在启动时发生。

    【讨论】:

    • 好的,太好了!下次我遇到这样的问题时……您知道我如何快速找出缺少哪个课程吗?在我今天的问题中,异常有点模糊。
    • 如果我处于相同的位置,我会在互联网上搜索一些例外情况,或者查看正在运行的项目的源代码/示例以确定需要什么依赖项(最好的方法之一这样做是检查单元测试中使用了哪些依赖项)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多