Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CXF with Spring-Boot

I am attempting to get CXF and Sprint Boot to play nicely. I have a JAX-WS service endpoint called SubscriberApi. Looking at the spring-boot logs I see successful mapping:

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

However, I cant seem to get the WSDL when hitting:

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;
  }
 }
like image 260
Vladimir Avatar asked Nov 24 '14 20:11

Vladimir


1 Answers

There's a much easier way to get Spring Boot & Apache CXF running and providing a SOAP webservice based on your WSDL file: Just use the cxf-spring-boot-starter, which does everything for you. You only need to use the starter and it's companion Maven plugin in your pom.xml like this (full example!):

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

Place your wsdl somewhere inside src/main/resources, implement your endpoint class and your done!. That's really everything, no manual boilerplate coding (no ServletRegistrationBean etc.). This is just generated for you based on the WSDL - 100% contract first.

Here's also a fully comprehensible example project: https://github.com/codecentric/spring-samples/tree/master/cxf-boot-simple. And there's also a blog post series, which will introduce you to everything related to know: https://blog.codecentric.de/en/2016/10/spring-boot-apache-cxf-spring-boot-starter/ Have fun!

like image 199
jonashackt Avatar answered Sep 27 '22 19:09

jonashackt