使用 cxf-spring-boot-starter-jaxws 的 xmlDatabinding 配置

xmlDatabinding configuration with cxf-spring-boot-starter-jaxws

我正在尝试使用 Spring boot cxf starter 发布服务,使用数据绑定 xmlbean,如下所示:

@Bean
public Endpoint nameServiceEndpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus, new NameWsServiceImpl());
        endpoint.publish("/NamesWsService");
        endpoint.setDataBinding(new XmlBeansDataBinding());
     return endpoint;
}

当我尝试 运行 应用时出现以下错误:

java.lang.NoSuchMethodError: org.apache.cxf.common.jaxb.JAXBUtils.createMininumEscapeHandler

在我的 pom 中,我有依赖项:

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
    <version>3.2.2</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-databinding-xmlbeans</artifactId>
    <version>3.1.14</version>
</dependency>

我该如何解决这个问题?

您可以使用 spring 引导 Web 服务,但您应该包括 wsdl4j

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
   <groupId>wsdl4j</groupId>
   <artifactId>wsdl4j</artifactId>
</dependency>

引用是https://spring.io/guides/gs/producing-web-service/

或者您只需使用企业和生产就绪 cxf-spring-boot-starter,这名副其实 - 因为您不必关心 wsdl4jxmlbeans。这个东西为你做了一切,只需在一个普通的 Spring 启动项目中使用它。以下 pom.xml 具有 一切 你需要 ever 使用 Spring Boot & CXF 执行 SOAP:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>de.codecentric.soap</groupId>
    <artifactId>cxf-boot-simple</artifactId>
    <version>2.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>cxf-boot-simple</name>
    <description>Demo project for using Spring Boot Starter CXF</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>cxf-spring-boot-starter</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>de.codecentric</groupId>
                <artifactId>cxf-spring-boot-starter-maven-plugin</artifactId>
                <version>2.0.0.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

配套的 Maven 插件 cxf-spring-boot-starter-maven-plugin does the heavy lifting for you: just place your wsdl file somewhere inside src/main/resources and it will generate all needed Java class files from your WSDL, incl. the Service Endpoint Interface (SEI) implementation - which enables the cxf-spring-boot-starter to automatically fire up your SOAP services - 100% contract first! Just as you know all the other frameworks in the Spring Boot universe. Here's also a fully working example project: https://github.com/codecentric/spring-samples/tree/master/cxf-boot-simple

留给您的唯一事情就是将业务转换到您的内部域模型中或从中进行转换:)玩得开心!

似乎 cxf-spring-boot-starter-jaxws 依赖于缺少 createMininumEscapeHandler 的旧 jar。
尝试通过导入 cxf-bom 修复它,它对我有用!

<!-- import cxf-bom -->
<dependencyManagement>
  <dependencies>
     <dependency>
       <groupId>org.apache.cxf</groupId>
       <artifactId>cxf-bom</artifactId>
       <version>3.4.0</version>
       <type>pom</type>
       <scope>import</scope>
     </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <!-- just cxf-spring-boot-starter-jaxws -->
  <dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
  </dependency>
</dependencies>