先定义一个web service
[java] view plain copy print ?
- @WebService(targetNamespace="http://www.itfad.net/queryUser")
- public interface IQueryUser
- {
- String query(@WebParam(name = "user") UserInfo user);
- }
@WebService(targetNamespace="http://www.itfad.net/queryUser")
public interface IQueryUser
{
String query(@WebParam(name = "user") UserInfo user);
}
实现类:
[java] view plain copy print ?
- @WebService(endpointInterface="net.itfad.services.IQueryUser")
- public class QueryUserImpl implements IQueryUser
- {
- public String query(UserInfo user)
- {
- if (user == null)
- throw new IllegalArgumentException("参数user不能为null");
- return "your name is " + user.getUserName();
- }
- }
@WebService(endpointInterface="net.itfad.services.IQueryUser")
public class QueryUserImpl implements IQueryUser
{
public String query(UserInfo user)
{
if (user == null)
throw new IllegalArgumentException("参数user不能为null");
return "your name is " + user.getUserName();
}
}
web service发布方式
第一种,使用jax-ws方式,非常简单
String url="http://localhost:8080/queryUser;
Endpoint.publish(url, new QueryUserImpl);
第二种,CXF方式
[java] view plain copy print ?
- String url="http://localhost:8080/queryUser";
- JaxWsServerFactoryBean factoryBean=new JaxWsServerFactoryBean();
- factoryBean.setAddress(url);
- factoryBean.setServiceClass(IQueryUser.class);
- factoryBean.setServiceBean(new QueryUserImpl());
- factoryBean.create()
String url="http://localhost:8080/queryUser";
JaxWsServerFactoryBean factoryBean=new JaxWsServerFactoryBean();
factoryBean.setAddress(url);
factoryBean.setServiceClass(IQueryUser.class);
factoryBean.setServiceBean(new QueryUserImpl());
factoryBean.create()
第三种,CXF Simple FrontEnd方式
该方式无需在类中指定相关协议,也就是说不需要在类中指定相关webservice注解,一个普通类就行
[java] view plain copy print ?
- ServerFactoryBean svrFactory = new ServerFactoryBean();
-
- svrFactory.setServiceClass(IQueryUser.class);
- svrFactory.setAddress("http://localhost:8080/queryUser");
-
- svrFactory.setServiceBean(QueryUserImpl);
- svrFactory.create();
ServerFactoryBean svrFactory = new ServerFactoryBean();
//设置服务接口类
svrFactory.setServiceClass(IQueryUser.class);
svrFactory.setAddress("http://localhost:8080/queryUser");
//设置服务实现接口类
svrFactory.setServiceBean(QueryUserImpl);
svrFactory.create();
客户端调用web service方法
首先要使用cxf的wsdl2java命令工具生成服务代理类,如:wsdl2java -p com.charles.client http://localhost:8080/queryUser?wsdl
具体命令格式可使用wsdl2java /?查看。生成后的代理类名是:服务实现类名+Service
方法一:使用标准的JAX-WS的API完成客户端调用
//注意。此处http:http://www.itfad.net/queryUser是服务的命名空间,可查看wsdl文件中的targetNamespace
QName qName = new QName("http://www.itfad.net/queryUser","QueryUserImplService");
QueryUserImplService ImplService =new QueryUserImplService(new URL("http://localhost:8080/CXFDemo/queryUser?wsdl"),qName);
IQueryUser service=(IQueryUser)ImplService.getPort(IQueryUser.class);
方法二:javax.xml.ws.Service
String endPointAddress = "http://localhost:8080/queryUser";
//服务实际地址
//此处http://charles.com/为命名空间,默认是包名的倒序。IQueryUserService=服务接口名+service
javax.xml.ws.Service service = javax.xml.ws.Service.create(new QName("http://charles.com/","IQueryUserService"));
//IQueryUserPort=服务接口名+Port
service.addPort(new QName("http://charles.com/","IQueryUserPort"),javax.xml.ws.soap.SOAPBinding.SOAP11HTTP_BINDING, endPointAddress);
IQueryUser queryService1 =service.getPort(IQueryUser.class);
System.out.println(queryService1.query(user));
这里其实利用wsdl2java工具为我们生成类时已经包含了这种方法。比如我们的服务接口是IQueryUser,那么我们会发现生成的文件下有个IQueryUserService类
该类正是继承至javax.xml.ws.Service,并封装以上代码的功能,调用代码如下:
IQueryUserService queryUserService=new IQueryUserService();//默认构造函数里使用默认wsdl地址和服务的QName
IQueryUser queryService1 =queryUserService.getIQueryUserPort();//方法内部调用了super.getPort(IQueryUserPort, IQueryUser.class);
System.out.println(queryService1.query(user));
方法三:使用CXF中JaxWsProxyFactoryBean客户端代理工厂调用web服务
JaxWsProxyFactoryBean soapFactoryBean = newJaxWsProxyFactoryBean();
soapFactoryBean.setAddress("http://localhost:8080/queryUser");
soapFactoryBean.setServiceClass(IQueryUser.class);
Object o = soapFactoryBean.create();
IQueryUser service = (IQueryUser)o;
方法四:使用CXF的JaxWsDynamicClientFactory动态代理工厂
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("people.wsdl", classLoader);
Object person =Thread.currentThread().getContextClassLoader().loadClass("com.acme.Person").newInstance();
Method m = person.getClass().getMethod("setName", String.class);
m.invoke(person, "Joe Schmoe");
client.invoke("addPerson", person);