极简SpringBoot+CXF,实现WebService

1.关于springboot 如何接入cxf的文章并不少,本篇文章主侧重于springboot1.X与spirngboot2.X如何整合cxf,下面是代码实现

pom.xml



    4.0.0
    com.mrb
    miniPJ
    1.0-SNAPSHOT

     


         
             org.apache.cxf
             cxf-spring-boot-starter-jaxws
             3.1.11
         

        
            org.projectlombok
            lombok
            1.16.18
            jar
        
     


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.运行Application.main,访问:http://localhost:8700/services/centerwms?wsdl 结果如下

极简SpringBoot+CXF,实现WebService_第1张图片

3.以上就能快速启动一个WebService demo,但这个是springboot1.4.4版本启动的,下表是

cxf-spring-boot-starter-jaxws 对应 springboot版本
cxf-spring-boot-starter-jaxws springboot
3.1.X
1.4.X
3.2.X 1.5.X
3.3.X 2.X

①将 cxf-spring-boot-starter-jaxws 版本 切换为 3.2.9:

        
             org.apache.cxf
             cxf-spring-boot-starter-jaxws
             3.2.9
         

程序是能够正常运行。

②将 cxf-spring-boot-starter-jaxws 版本 切换为 3.3.0:

回报类没有定义的错误:Caused by: java.lang.NoClassDefFoundError: javax/validation/ClockProvider

原因在于 cxf-spring-boot-starter-jaxws 默认依赖java.validation:validation-api:1.1.0的这个版本没有没有定义这个类。极简SpringBoot+CXF,实现WebService_第2张图片

解决方法:

直接添加 依赖,然后重新编译

    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.2.RELEASE
    

可以看到这个版本切换了:

极简SpringBoot+CXF,实现WebService_第3张图片

点击运行,访问:http://localhost:8700/services/centerwms?wsdl 结果如下

极简SpringBoot+CXF,实现WebService_第4张图片

你可能感兴趣的:(Java,WebService)