OpenSource中关于CXF的简介:Apache CXF一个开源的Service框架,它实现了JCP与Web Service中一些重要标准。CXF简化了构造,集成,面向服务架构(SOA)业务组件与技术的灵活复用。在CXF中,Service使用WSDL标准定义并能够使用各种不同的消息格式(或binding)和网络协议(transports)包括SOAP、XML(通过HTTP或JMS)进行访问。CXF同样支持多种model 如:JAX-WS,JBI,SCA和CORBA service。CXF设计成可灵活部署到各种容器中包括Spring-based,JBI,SCA, Servlet和J2EE容器。
用CXF构建webservice程序的大致流程如下:
1 配置web.xml文件,加载CXF
2 编写java接口,和接口的实现
3 在spring的applicationContext.xml中配置java文件
4 创建beans.xml文件,里面配置接口的实现类
5 运行tomca服务器即可生成wsdl文件
6 编写测试代码:编写java测试文件和client-beans.xml文件
7编写运行客户端文件所需的服务器,可以单独编写,也可以在随tomcat启动
示例代码。首先下载cxf,然后解压,将lib下的所有jar包拷到工程下面
package com.easyway.cxf.service;
import javax.jws.WebParam;
import javax.jws.WebService;
import java.util.List;
import com.easyway.cxf.model.User;
/**
*
* 采用JaxWS发布服务
* 备注在接口中必须使用@WebService 注解否则出现错误
*
*
* @author longgangbai
*
*/
@WebService
public interface HelloService {
/**
* The @WebParam annotation is necessary as java interfaces do not store the Parameter name in the .class file. So if you leave out the annotation your parameter will be named arg0.
* @param name
* @return
*/
public String hello(@WebParam(name="text")String name);
/**
* Advanced usecase of passing an Interface in. JAX-WS/JAXB does not
* support interfaces directly. Special XmlAdapter classes need to
* be written to handle them
*/
public String sayHi(User user);
public String[] getAllUseNames(List<User> userList);
}
package com.easyway.cxf.service;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.jws.WebService;
import com.easyway.cxf.model.User;
/**
*
* 采用JaxWS发布服务
*
* JAX-WS includes many more annotations as well such as:
*
* @WebMethod - allows you to customize the operation name, exclude the operation from inclusion in the service, etc
* @WebParam - allows you to customize a parameter's name, namespace, direction (IN or OUT), etc
* @WebResult - allows you to customize the return value of the web service call
*
* @author longgangbai
*
*/
@WebService(endpointInterface = "com.easyway.cxf.service.HelloService",
serviceName = "HelloService")
public class HelloServiceImpl implements HelloService {
Map<Integer, User> users = new LinkedHashMap<Integer, User>();
public String hello(String username) {
return "Hello " + username;
}
public String sayHi(User user) {
users.put(users.size() + 1, user);
return "Hello " + user.getUsername();
}
public String[] getAllUseNames(List<User> userList) {
String[] userListArr=new String[userList.size()];
for (int i=0;i<userList.size();i++) {
userListArr[i]=userList.get(i).getUsername();
}
return userListArr;
}
}
package com.easyway.cxf.test.server;
import javax.xml.ws.Endpoint;
import com.easyway.cxf.service.HelloServiceImpl;
/**
* run webServiceApp.java 类来启动服务。 访问 http://localhost:8080/HelloService?wsdl 查看是否显示
* wsdl。
* @author longgangbai
*
*/
public class CFX {
public final static String SERVICE_ADDRESS="http://localhost:8080/HelloService";
public static void main(String[] args) {
System.out.println("web service start");
HelloServiceImpl implementor= new HelloServiceImpl();
//采用JWS发布服务
Endpoint.publish(SERVICE_ADDRESS, implementor);
System.out.println("web service started");
}
}
package com.easyway.cxf.test.client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.easyway.cxf.model.User;
import com.easyway.cxf.service.HelloService;
import com.easyway.cxf.test.server.CFX;
/**
* 调用服务端代码
* @author longgangbai
*
*/
public class CFXClient {
public static void main(String[] args) {
//采用JWS获取服务
JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();
svr.setServiceClass(HelloService.class);
//和服务端发送路径一样的
svr.setAddress(CFX.SERVICE_ADDRESS);
HelloService hw = (HelloService) svr.create();
User user = new User();
user.setUsername("Tony");
user.setPassword("test");
System.out.println(hw.sayHi(user));
}
}