服务端:
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