Apache CXF是一个开源的WebService框架,CXF大大简化了Webservice的创建,同时它继承了XFire的传统,一样可以和spring天然的进行无缝的集成。CXF框架是一种基于servlet技术的SOA应用开发框架,要正常运用基于CXF应用框架开发的企业应用,除了CXF应用本身之外,还需要JDK和servlet容器的支持。
service文档结构:
下载地址:http://download.csdn.net/detail/lovebosom/9649110 可以挑选所需的jar
CXFServlet
org.apache.cxf.transport.servlet.CXFServlet
1
CXFServlet
/webservice/*
@WebService
public interface GetInfo {
/*发现有时不用@WebParam也可以接收到参数*/
public void sayHello(@WebParam(name="userName") String userName,@WebParam(name="age") int age);
}
@WebService(targetNamespace="http://inter.webservice.zhan.com/",
endpointInterface="com.zhan.webservice.inter.GetInfo")
public class GetInfoImpl implements GetInfo {
public GetInfoImpl() {
System.out.println("service GetInfoImpl()..");
}
@Override
public String sayHello(String name,int age) {
System.out.println("service sayHello()..."+ name +",年龄:"+age);
return "hello:"+name +"年龄:"+age;
}
}
地址栏输入:http://localhost:8080/webservice_cxf_server/webservice/getInfo?wsdl 即可看到文档
import java.net.URL;
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
public class ClientTest {
public static void main(String[] args) {
JaxWsDynamicClientFactory cf = JaxWsDynamicClientFactory.newInstance();
String wsurl = "http://127.0.0.1:8080//webservice_cxf_server/webservice/getInfo?wsdl";
Client client = cf.createClient(wsurl);
String namespace = "http://inter.webservice.zhan.com/";
String method="sayHello";
try {
/**
* cxf方式 : 2种方式
*/
//第1种方式:不使用命名空间,直接使用方法名
Object[] objects=client.invoke(method,"cxf1",11);//方法名,参数
System.out.println("=="+objects[0].toString());
//第2种方式:使用命名空间,使用QName对象(QName+方法名)+参数,
QName qname = new QName("http://inter.webservice.zhan.com/", method);//(namespace,method)
Object[] objects2=client.invoke(qname,"cxf2",22);//QName,参数
System.out.println(objects2[0]);
/**
* axis 调用webservice
*/
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(wsurl);//wsurl换为new URL(wsurl)也可以
call.setOperation(method);// 要调用的方法名
call.setUseSOAPAction(true);
call.setOperationName(new QName(namespace,method));
call.addParameter(
"userName",//如果服务端收不到参数,可以试试 new QName(namespace,"userName"),
org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
call.addParameter("age",//如果服务端收不到参数,可以试试 new QName(namespace,"age"),
org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
String str = (String) call.invoke(new Object[]{"axis",33});
System.out.println(str);
} catch (Exception e) {
e.printStackTrace();
}
}
}
更多webservice创建和调用方式:http://download.csdn.net/download/lovebosom/9649112
二、xfire开发webservice
1.web.xml 配置:xfire
contextConfigLocation
classpath:applicationContext.xml,
classpath:xfire-servlet.xml
XFireServlet
org.codehaus.xfire.spring.XFireSpringServlet
XFireServlet
/services/*
2.配置xfire-servlet.xml
3.编写接口和实现类:
/**
* 删除学员信息
* @author gyz
* 2017年11月3日 上午11:28:12
*
*/
public interface LearnerService {
public String deleteLearner(String icCard,String area);
}
public class LearnerServiceImpl implements LearnerService {
private static final Logger log = Logger.getLogger(LearnerServiceImpl.class);
@SuppressWarnings("unchecked")
public String deleteLearner(String icCard, String area) {
//TODO 。。。
}
4.http://127.0.0.1/gx/services/LearnerService?wsdl.....打开。
5.客户端采用cxf调用:
public static void main(String[] args) throws Exception {
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://127.0.0.1/gx/services/LearnerService?wsdl");
Object[] objects = client.invoke("deleteLearner", new Object[]{"24354","450800"});
System.out.println("Echo response: "+ objects[0]);
}