cxf01

编程入门 行业动态 更新时间:2024-10-26 22:19:52

cxf01

cxf01

maven 配置, 到官网找

<project xmlns=".0.0" xmlns:xsi=""xsi:schemaLocation=".0.0 .0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.chb.student</groupId><artifactId>cxf01</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>cxf01</name><url>;/url><properties><cxf.version>2.2.3</cxf.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.10</version><scope>test</scope></dependency><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><!-- Jetty is needed if you're are not using the CXFServlet --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http-jetty</artifactId><version>${cxf.version}</version></dependency></dependencies>
</project>

二、使用代码优先

2.1、创建一个接口

package com.chb.cxfws;import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;@WebService
public interface IMyService {@WebMethodpublic @WebResult(name="hello")String sayHello(@WebParam(name="name")String name);
}

2.2、实现类

package com.chb.cxfws;import javax.jws.WebService;@WebService(endpointInterface="com.chb.cxfws.IMyService")
public class MyServiceImpl implements IMyService {public String sayHello(String name) {return "hello " + name + "!";}}

2.3、发布服务

package com.chb.cxfws;import javax.xml.ws.Endpoint;public class MyServer {public static void main(String[] args) {Endpoint.publish("http://localhost:8888/cxf", new MyServiceImpl());}}

3、创建客户端

3.1、使用cxf的工具wsdl2java生成源码

3.2、使用jaxws调用


/*** Unit test for simple App.*/
public class AppTest 
{@Testpublic void test() throws MalformedURLException {//jaxws调用//URL不是必须的,除非服务的地址有改变URL wsdlUrl = new URL("http://localhost:8888/ms?wsdl");MyServiceImplService myss = new MyServiceImplService();IMyService myService =  myss.getCxfwsPort();System.out.println(myService.sayHello("chb"));}
}

3.3、使用cxf, simple

3.3.1、服务端:

        //CXF提供另一种发布方式// Create our service implementationMyServiceImpl helloWorldImpl = new MyServiceImpl();// Create our ServerServerFactoryBean svrFactory = new ServerFactoryBean();svrFactory.setServiceClass(IMyService.class);svrFactory.setAddress("http://localhost:8889/ms");svrFactory.setServiceBean(helloWorldImpl);svrFactory.create();

3.3.2、客户端

        ClientProxyFactoryBean factory = new ClientProxyFactoryBean();factory.setServiceClass(IMyService.class);factory.setAddress("http://localhost:8889/ms");IMyService client = (IMyService) factory.create();System.out.println(client.sayHello("chb2"));

四、Dynamic Client

4.1 、CXF提供动态类的两个工厂类。如果你的服务是在JAX-WS的概念来定义,你应该使用JaxWsDynamicClientFactory 。如果你不想要或需要JAX-WS语义,使用DynamicClientFactory。下面使用jaxws版。

@Test/***   测试 Dynamic client*/public void testDynamic() throws Exception {JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();//创建一个ClientClient client = dcf.createClient("http://localhost:8889/ms?wsdl");Object[] res = client.invoke("sayHello", "test echo");System.out.println("Echo response: " + res[0]);}

集成为一个工具类


import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.junit.Test;import com.nokiamon.context.CommonContext;public class CXFClientUtil {private static JaxWsDynamicClientFactory dcf = null;public static JaxWsDynamicClientFactory getJaxWsDynamicClientFactory() {if(null == CXFClientUtil.dcf) {CXFClientUtil.dcf = JaxWsDynamicClientFactory.newInstance();}return CXFClientUtil.dcf;}/*** cxf 使用JaxWsDynamicClienFactor 调用服务的方法。* @param wsdlURL  服务的url* @param methodName   调用的方法* @param params     参数* @return*/public static Object[] clientInvoke(String wsdlURL, String methodName, Object... params) {try {Thread.currentThread().setContextClassLoader(CommonContext.contextClassLoader);JaxWsDynamicClientFactory dcf = CXFClientUtil.getJaxWsDynamicClientFactory();Client client = dcf.createClient(wsdlURL);Object[] objects = client.invoke(methodName, params);return objects;} catch (Exception e) {//LogUtil.outputErrorInfo(CommonContext.logger, e, wsdlURL, methodName, params);}return null;}/*** * @param client* @param methodName* @param params* @return*/public static Object[] clientInvoke(Client client, String methodName, Object... params) {try {Thread.currentThread().setContextClassLoader(CommonContext.contextClassLoader);Object[] objects = client.invoke(methodName, params);return objects;} catch (Exception e) {//LogUtil.outputErrorInfo(CommonContext.logger, e, client.toString(), methodName, params);}return null;}
}

更多推荐

cxf01

本文发布于:2024-02-12 19:12:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1689029.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!