【问题标题】:CXF with Spring-Boot带弹簧启动的 CXF
【发布时间】:2015-01-22 16:11:35
【问题描述】:

我正在尝试让 CXF 和 Sprint Boot 发挥出色。我有一个名为 SubscriberApi 的 JAX-WS 服务端点。查看 spring-boot 日志,我看到成功映射:

Mapping servlet: 'CXFServlet' to [/api/*]
Setting the server's publish address to be /SubscriberApi

但是,我在点击时似乎无法获取 WSDL:

http://localhost:8080/api/SubscriberApi?wsdl
@Configuration
@ImportResource({"classpath:META-INF/cxf/cxf.xml"})
public class CxfConfiguration  {
  @Bean
  public SubscriberApi subscriberApi() {
    return new SubscriberApi();
  }
  @Bean
  public ServletRegistrationBean servletRegistrationBean() {
    CXFServlet cxfServlet = new CXFServlet();

    ServletRegistrationBean servletRegistrationBean =
        new ServletRegistrationBean(cxfServlet, "/api/*");
    servletRegistrationBean.setLoadOnStartup(1);
    return servletRegistrationBean;
  }
  @DependsOn("servletRegistrationBean")
  @Bean
  public Endpoint jaxwsEndpoint(SubscriberApi subscriberApi){
    javax.xml.ws.Endpoint jaxwsEndpoint =
        javax.xml.ws.Endpoint.publish("/SubscriberApi", subscriberApi);
      return jaxwsEndpoint;
  }
 }

【问题讨论】:

    标签: cxf jax-ws spring-boot


    【解决方案1】:

    让你的jaxwsEndpoint bean 返回一个org.apache.cxf.jaxws.EndpointImpl 的实例,它扩展了javax.xml.ws.Endpoint

    @Autowired
    private ApplicationContext applicationContext;
    
    @DependsOn("servletRegistrationBean")
    @Bean
    public Endpoint jaxwsEndpoint(){
       Bus bus = (Bus) applicationContext.getBean(Bus.DEFAULT_BUS_ID);
       EndpointImpl endpoint = new EndpointImpl(bus, subscriberApi());
       endpoint.publish("/SubscriberApi");
       // also showing how to add interceptors
       endpoint.getServer().getEndpoint().getInInterceptors().add(new LoggingInInterceptor());
       endpoint.getServer().getEndpoint().getOutInterceptors().add(new LoggingOutInterceptor());
    
       return endpoint;
    }
    

    原始帖子不包含可运行的示例,但这应该可以解决问题。

    可以在此处找到一个运行示例,其中所有配置链接在一起: Application.java

    【讨论】:

      【解决方案2】:

      您现在可以通过添加以下内容将自动配置与 Spring Boot CXF 启动器一起使用:

      <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
        <version>3.1.7</version>
      </dependency>
      

      另请参阅:http://cxf.apache.org/docs/springboot.html

      【讨论】:

      • 这个不错,但不支持在 cxf 端点内引导 spring
      【解决方案3】:

      有一种更简单的方法可以让 Spring Boot 和 Apache CXF 运行并根据您的 WSDL 文件提供 SOAP Web 服务:只需使用 cxf-spring-boot-starter,它会为您完成所有工作。您只需要使用启动器,它是您的pom.xml 中的companion Maven plugin,就像这样(完整示例!):

      <?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.1.2-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>
      

      将您的 wsdl 放在 src/main/resources 内的某个位置,实现您的端点类并完成!。这就是一切,没有手动样板编码(没有ServletRegistrationBean 等)。这只是根据 WSDL 为您生成的 - 首先是 100% 合同。

      这里还有一个完全可以理解的示例项目:https://github.com/codecentric/spring-samples/tree/master/cxf-boot-simple。还有一个博文系列,为你介绍所有相关知识:https://blog.codecentric.de/en/2016/10/spring-boot-apache-cxf-spring-boot-starter/玩得开心!

      【讨论】:

      • apache的cxf-spring-boot-starter-jaxws和codecentric的cxf-spring-boot-starter有什么区别?
      • 答案在blog.codecentric.de/en/2016/10/…But wait! Isn´t there an official Apache CXF spring-boot-starter?的第一段。简而言之:Apache starter 只初始化 CXF 的一部分,其中以代码为中心的 starter 负责每个需要的组件,初始化每个所需的样板,根据您的 WSDL(完全合同优先支持)自动生成 JAX-B 类文件,简化日志记录SOAP 消息等等。
      猜你喜欢
      • 2010-10-15
      • 2020-01-25
      • 1970-01-01
      • 2018-09-08
      • 2021-04-13
      • 1970-01-01
      • 1970-01-01
      • 2021-11-30
      • 1970-01-01
      相关资源
      最近更新 更多