文章目录
- 一、CXF服务端
- 1. 导入依赖
- 2. 创建service接口
- 3. 接口实现类
- 4. cxf配置类
- 5. 查看wsdl结果
- 二、CXF客户端
- 2.1. 客户端
- 2.2. 断点调试
- 2.3. 发起调用服务
- 开源源码.
一、CXF服务端
1. 导入依赖
<properties><cxf.version>3.3.1</cxf.version></properties><!-- CXF webservice --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>${cxf.version}</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId><version>${cxf.version}</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-spring-boot-starter-jaxws</artifactId><version>${cxf.version}</version></dependency><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-validator</artifactId><version>5.2.4.Final</version></dependency>
2. 创建service接口
注解 | 简介 |
---|---|
@WebService | 放在接口上用于标记为webService服务接口 |
targetNamespace | 命名空间 |
name | 服务接口的名字,可不写 |
@WebMethod | 标记为webService服务的方法 |
@WebParam | 标记为webService服务的方法入参 |
package com.gblfy.ws.service;import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;/*** cxf接口** @author gblfy* @date 2021-09-17*/
@WebService(targetNamespace = "http://service.ws.gblfy.com/", name = "ICxfService")
public interface ICxfService {@WebMethodpublic String sayhello(@WebParam(name = "request") String request);
}
3. 接口实现类
package com.gblfy.ws.service.impl;import com.gblfy.ws.service.ICxfService;
import org.springframework.stereotype.Component;import javax.jws.WebService;/*** cxf接口实现类** @author gblfy* @date 2021-09-17*/
@WebService(serviceName = "cxfServiceShell",//对外发布的服务名targetNamespace = "http://service.ws.gblfy.com/",//指定你想要的名称空间,通常使用使用包名反转endpointInterface = "com.gblfy.ws.service.ICxfService")
@Component
public class CxfServiceImpl implements ICxfService {@Overridepublic String sayhello(String request) {return "gblfy.com " + request;}
}
4. cxf配置类
package com.gblfy.ws.config;import com.gblfy.ws.service.ICxfService;
import com.gblfy.ws.service.impl.CxfServiceImpl;
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 {@Beanpublic ServletRegistrationBean cxfServlet() {return new ServletRegistrationBean(new CXFServlet(), "/cxf/*");}@Bean(name = Bus.DEFAULT_BUS_ID)public SpringBus springBus() {return new SpringBus();}@Beanpublic ICxfService cxfService() {return new CxfServiceImpl();}/*** 发布服务并指定访问URL** @return*/@Beanpublic EndpointImpl userEnpoint() {EndpointImpl endpoint = new EndpointImpl(springBus(), cxfService());endpoint.publish("/cxfServiceShell");return endpoint;}
}
5. 查看wsdl结果
(1)配置启动端口 server.port: 8080
(2)启动springBoot启动类 输入 localhost:8080/cxf 可以看到自己发布的服务
http://localhost:8080/cxf/cxfServiceShell?wsdl
二、CXF客户端
2.1. 客户端
package com.gblfy.ws.client;import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.stereotype.Component;/*** cxf客户端调用(企业内部已封装)** @author gblfy* @date 2021-09-17*/
@Component
public class CxfClient {public static void main(String[] args) throws Exception {String cxfUrl = "http://127.0.0.1:8080/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;}
}
2.2. 断点调试
2.3. 发起调用服务
开源源码.
https://gitee.com/gb_90/unified-access-center