极简SpringBoot+CXF,实现WebService

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

pom.xml

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

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

cxf-spring-boot-starter-jaxws 对应 springboot版本
cxf-spring-boot-starter-jaxwsspringboot
3.1.X
1.4.X
3.2.X1.5.X
3.3.X2.X

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

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

程序是能够正常运行。

②将 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的这个版本没有没有定义这个类。

解决方法:

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

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

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

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值