Springboot+CXF实现WebService的客户端和服务端示例

服务端:

package com.xxx.abc.core.application.cxf;

import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CxfConfig {

    @Bean
    public ServletRegistrationBean cxfServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/cxf/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    /**
     * 发布服务并指定访问URL
     *
     * @return
     */
    @Bean
    public EndpointImpl userEnpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), new CxfServer());
        endpoint.publish("/cxfServiceShell");
        return endpoint;
    }
}

package com.xxx.abc.core.application.cxf;

import org.springframework.stereotype.Component;

import javax.jws.WebService;

@WebService(serviceName = "cxfServiceShell",//对外发布的服务名
        targetNamespace = "http://cxf.application.core.abc.xxx.com/"//,指定你想要的名称空间,通常使用使用包名反转
)
        //endpointInterface = "com.xxx.abc.core.application.cxf")
@Component
public class CxfServer {

    public String sayhello(String request) {
        return "gblfy.com " + request;
    }
}
server:
    port: 9081
    servlet:
        context-path: /cxfServer
buildscript {
    apply plugin: "java"
    apply plugin: "io.spring.dependency-management"
}

dependencies {

    // 工程依赖
   // implementation(project(':xxx-xx-xx'))
    implementation 'org.apache.cxf:cxf-spring-boot-starter-jaxws:3.5.5'
    implementation 'javax.jws:javax.jws-api:1.1'
   // implementation 'com.sun.xml.ws:jaxws-ri:2.3.0'
    implementation 'com.sun.xml.ws:jaxws-rt:2.1.3'
    implementation 'javax.activation:activation:1.1.1'
    // 仅编译依赖

   // compileOnly('org.apache.httpcomponents:httpclient')
   // compileOnly('javax.servlet:javax.servlet-api')
   // compileOnly('com.google.code.findbugs:annotations')

    // 仅引入注解依赖
   // implementation('io.swagger.core.v3:swagger-annotations')
  //  implementation('jakarta.validation:jakarta.validation-api')


    annotationProcessor('org.springframework.boot:spring-boot-configuration-processor')

    // 测试环境依赖项
    testImplementation('junit:junit')
    testImplementation('org.springframework.boot:spring-boot-starter-test')
    testImplementation('org.springframework.boot:spring-boot-starter-web')
}

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.boot:spring-boot-dependencies:2.7.6"
    }
}

tasks.named('test') {
    useJUnitPlatform()
}

启动springBoot启动类 输入 localhost:8080/cxf 可以看到自己发布的服务

http://localhost:8080/cxfServer/cxf/cxfServiceShell?wsdl

客户端:

server:
    port: 9080
    servlet:
        context-path: /cxf
buildscript {
    apply plugin: "java"
    apply plugin: "io.spring.dependency-management"
}

dependencies {
    implementation 'org.apache.cxf:cxf-spring-boot-starter-jaxws:3.5.5'
    implementation 'javax.jws:javax.jws-api:1.1'
    implementation 'com.sun.activation:jakarta.activation:1.2.2'
    implementation 'org.apache.cxf:cxf-rt-transports-http:3.5.5'
    implementation 'org.apache.cxf:cxf-rt-frontend-jaxws:3.5.5'
    implementation 'javax.jws:javax.jws-api:1.1'
    implementation ('com.sun.xml.ws:jaxws-rt:2.1.3') {
        exclude group: "com.sun.xml.bind", module: "jaxb-impl"
    }
    implementation 'javax.activation:activation:1.1.1'
    implementation 'javax.xml.bind:jaxb-api:2.2.1'
    implementation 'javax.xml:jaxb-impl:2.1'
    implementation 'com.sun.xml.bind:jaxb-xjc:2.2.11'

    // 测试环境依赖项
    testImplementation('junit:junit')
    testImplementation('org.springframework.boot:spring-boot-starter-test')
    testImplementation('org.springframework.boot:spring-boot-starter-web')
}

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.boot:spring-boot-dependencies:2.7.6"
    }
}

tasks.named('test') {
    useJUnitPlatform()
}
package com.xxx.abc.core.application.cxf;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.stereotype.Component;

@Component
public class CxfClient {
    public static void main(String[] args) throws Exception {
        String cxfUrl = "http://127.0.0.1:9081/cxfServer/cxf/cxfServiceShell?wsdl";
        String method = "sayhello";
        String reqXml = "1";

        //调用服务
        CxfClient.cxfClientSingleParam(cxfUrl, method, reqXml);
    }

    /**
     * 单/多参调用工具类(Object类型)
     *
     * @param cxfUrl url地址
     * @param method 调用方法名
     * @param reqXml 发送报文体
     * @return res 返回结果
     * @throws Exception 若有异常,在控制台输出异常,并将异常抛出
     */
    public static String cxfClientSingleParam(String cxfUrl, String method, Object... reqXml) throws Exception {
        String res = null;
        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(cxfUrl);

        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
        Object[] objects = new Object[0];
        try {
            // 基本格式:invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke(method, reqXml);
            res = objects[0].toString();
            System.out.println("返回数据:" + res);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
            throw e;
        }
        return res;
    }
}

需要注意的问题:依赖需要解决冲突,gradle文件里exclude了依赖com.sun.xml.bind,否则报错

java.lang.NoSuchFieldError: REFLECTION

返回结果获取:在 System.out.println("返回数据:" + res);处打断点能获取到从server端返回的数据返回数据:gblfy.com 1

参考:https://blog.csdn.net/weixin_40816738/article/details/120348754

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我了解了您的问题。使用Spring Boot和CXF框架搭建一个Webservice服务端的步骤如下: 1. 首先,在您的项目添加CXFSpring Boot的依赖,例如: ``` <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.3.6</version> </dependency> ``` 注意,这里使用的是CXF的JAX-WS模块,您也可以根据需要选择其他模块。 2. 接着,在您的配置文件添加CXF的配置,例如: ``` @Configuration public class CxfConfig { @Bean public ServletRegistrationBean<CXFServlet> cxfServlet() { return new ServletRegistrationBean<>(new CXFServlet(), "/yourWebserviceUrl/*"); } @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); } @Bean public YourWebService yourWebService() { return new YourWebService(); } @Bean public Endpoint yourWebServiceEndpoint() { EndpointImpl endpoint = new EndpointImpl(springBus(), yourWebService()); endpoint.publish("/YourWebServiceUrl"); return endpoint; } } ``` 这里配置了CXF的Servlet,以及您自己实现Webservice服务类和Endpoint。注意,您需要将 "/yourWebserviceUrl/" 和 "/YourWebServiceUrl" 替换为您自己的URL。 3. 最后,您可以实现您的Webservice服务类,例如: ``` @Service @WebService(serviceName = "YourWebService") public class YourWebService { @WebMethod public String hello(@WebParam(name = "name") String name) { return "Hello, " + name + "!"; } } ``` 这里实现了一个简单的hello方法,接收一个name参数并返回一个包含该参数的问候语。 完成以上步骤后,您就成功地搭建了一个基于CXFSpring Boot的Webservice服务端。当您在浏览器输入"http://localhost:8080/YourWebServiceUrl?wsdl"时,您将看到您的Webservice服务的WSDL文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值