700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > SpringBoot2 整合 CXF 服务端和客户端

SpringBoot2 整合 CXF 服务端和客户端

时间:2019-02-20 17:36:58

相关推荐

SpringBoot2 整合 CXF 服务端和客户端

文章目录

一、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接口

package com.gblfy.ws.service;import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebService;/*** cxf接口** @author gblfy* @date -09-17*/@WebService(targetNamespace = "http://service./", 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.ponent;import javax.jws.WebService;/*** cxf接口实现类** @author gblfy* @date -09-17*/@WebService(serviceName = "cxfServiceShell",//对外发布的服务名targetNamespace = "http://service./",//指定你想要的名称空间,通常使用使用包名反转endpointInterface = "com.gblfy.ws.service.ICxfService")@Componentpublic class CxfServiceImpl implements ICxfService {@Overridepublic String sayhello(String request) {return " " + 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;@Configurationpublic 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.ponent;/*** cxf客户端调用(企业内部已封装)** @author gblfy* @date -09-17*/@Componentpublic 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. 发起调用服务

开源源码.

/gb_90/unified-access-center

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。