maven 配置, 到官网找
4.0.0
com.chb.student
cxf01
0.0.1-SNAPSHOT
jar
cxf01
http://maven.apache.org
2.2.3
UTF-8
junit
junit
4.10
test
org.apache.cxf
cxf-rt-frontend-jaxws
${cxf.version}
org.apache.cxf
cxf-rt-transports-http
${cxf.version}
org.apache.cxf
cxf-rt-transports-http-jetty
${cxf.version}
二、使用代码优先
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 {
@WebMethod
public @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
{
@Test
public 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 implementation
MyServiceImpl helloWorldImpl = new MyServiceImpl();
// Create our Server
ServerFactoryBean 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();
//创建一个Client
Client 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.nokia.common.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;
}
}