Minimalizing SpringBoot+CXF to Implement WebService

1. There are many articles about how spring boot can access cxf. This article focuses on how spring boot 1.X and spirngboot 2.X can integrate cxf. Here is the code implementation.

maven dependence

<?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>com.mrb</groupId>
    <artifactId>miniPJ</artifactId>
    <version>1.0-SNAPSHOT</version>

     <dependencies>


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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
            <type>jar</type>
        </dependency>
     </dependencies>

</project>

application.properties

server.port=8700

Application.java

package com.mrb.minipj;

import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.jws.WebService;
import javax.xml.ws.Endpoint;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author MRB
 */
@SpringBootApplication
public class Application {
    public static void main(String args[]){
        SpringApplication.run(Application.class, args);
    }

    @Configuration
    public static  class CxfConfig {

        @Autowired
        private Bus bus;

        @Autowired
        public DemoService demoService;



        @Bean
        public Endpoint endpoint() {
            EndpointImpl endpoint = new EndpointImpl(bus, demoService);
            WebService serviceAnn = demoService.getClass().getDeclaredAnnotation(WebService.class);
            endpoint.publish("/"+serviceAnn.name());
            return endpoint;
        }
    }

    @Component
    @WebService(serviceName = "demo",
            targetNamespace = "http://service.minipj.mrb.com"

    )
    public static class DemoService  {

        public String helloWorld(String param) {
            return "hello"+param;
        }

    }

}

2. Run Application.main, access: http://localhost:8700/services/centerwms?wsdl The results are as follows

3. Above, you can quickly start a Web Service demo, but this one was started by Springboot version 1.4.4, the following table is

cxf-spring-boot-starter-jaxws corresponds to spring boot version
cxf-spring-boot-starter-jaxws springboot
3.1.X
1.4.X
3.2.X 1.5.X
3.3.X 2.X

(1) Switch the version of cxf-spring-boot-starter-jaxws to 3.2.9:

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

The program can run normally.

(2) Switch the version of cxf-spring-boot-starter-jaxws to 3.3.0:

Errors not defined by the return class: Caused by: java.lang.NoClassDefFoundError: javax/validation/ClockProvider

The reason is that cxf-spring-boot-starter-jaxws defaults on java.validation:validation-api:1.1.0, which does not have this class defined.

Solution:

Add < parent > dependencies directly and recompile them

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

You can see that this version has been switched:

Click Run to visit: http://localhost:8700/services/centerwms?wsdl The results are as follows

Tags: Spring Apache Maven Java

Posted on Tue, 08 Oct 2019 07:16:49 -0700 by dmayo2