以前工作中也用CXF,但都是用别人现成搭好的环境,这次自己重头搭建一遍环境。过程中也有遇到的问题,也做了简单的整理。
对于CXF是干什么用的,我不想多说,大家都知道这是我们在java编程中webService技术的一种实现工具。我们说说为什么用CXF来实现webService:
1. Java的webService实现本身就是一个很耗性能的实现方案(xml与java对象之间在服务端以及客户端的互转比较消耗性能)
2. 目前java主流的webService应用以CXF、AXIS2为主;
3. 通过网络渠道的了解,目前CXF的效率要比AXIS2高出至少50%;
4. 另外有一个webService的工具metro的效率比CXF高出10%;
5. CXF的实现资料网上可以随便找出一大堆,metro的资料相对少一些;
6. CXF在java应用实现中已经很成熟,企业更倾向于用这样一个成熟的解决方案;
基于以上原因,我选择CXF来实现webService。
参考资料:
Java Web 服务: CXF 性能比较----CXF 与最新版本的 Axis2 和 Metro 之间的性能对比
http://www.ibm.com/developerworks/cn/java/j-jws14/
一 以annotation注解方式实现发布webService应用
1、 基础环境
新建java web工程cxf之后,下载cxf工具包。解压CXF之后,把cxf工具包lib下的jar包全部放到工程的lib下。
此处用到的cxf工具包版本为:apache-cxf-2.7.12
下载地址:
http://www.apache.org/dyn/closer.cgi?path=/cxf/2.7.12/apache-cxf-2.7.12.zip
2、 编写服务接口
见文件HelloWorld.java
package com.hsy.server;
import java.util.List;
import javax.jws.WebParam;
import javax.jws.WebService;
import com.hsy.pojo.User;
@WebService
public interface HelloWorld {
String sayHi(@WebParam(name="text")String text);
String sayHiToUser(User user);
String[] SayHiToUserList(List userList);
}
3、 服务接口实现
见文件HelloWorldImpl.java
package com.hsy.server;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.jws.WebParam;
import javax.jws.WebService;
import com.hsy.pojo.User;
@WebService(endpointInterface="com.hsy.server.HelloWorld",serviceName="HelloWorld")
public class HelloWorldImpl implements HelloWorld {
Map users = new LinkedHashMap();
public String sayHi(@WebParam(name = "text") String text) {
return "Hello,"+text;
}
public String sayHiToUser(User user) {
users.put(users.size()+1, user);
return "Hello,"+user.getName();
}
public String[] SayHiToUserList(List userList) {
String[] result = new String[userList.size()];
int i = 0;
for(User u:userList){
result[i] = "Hello " + u.getName();
i++;
}
return result;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
4、 发布服务app
见文件webServiceApp.java
package com.hsy.server;
import javax.xml.ws.Endpoint;
public class webServiceApp {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("web service start");
HelloWorldImpl implementor = new HelloWorldImpl();
String address = "http://localhost:8080/helloWorld";
Endpoint.publish(address, implementor);
System.out.println("web service started");
}
}
右键 run as 选择java application发布服务;然后在浏览器输入地址:http://localhost:8080/helloWorld?wsdl
如图:20140805132120.jpg
说明webService服务发布成功。
5、 客户端访问服务
见文件HelloWorldClient.java
package com.hsy.client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.hsy.pojo.User;
import com.hsy.server.HelloWorld;
public class HelloWorldClient {
/**
* @param args
*/
public static void main(String[] args) {
//首先右键run as 运行com.hsy.server.webServiceApp类,然后再运行这段客户端代码
JaxWsProxyFactoryBean jwpfb = new JaxWsProxyFactoryBean();
jwpfb.setServiceClass(HelloWorld.class);
jwpfb.setAddress("http://localhost:8080/helloWorld");
HelloWorld hw = (HelloWorld) jwpfb.create();
User user = new User();
user.setName("马克思");
user.setDescription("怀念马克思");
System.out.println(hw.sayHiToUser(user));
}
}
右键 run as 选择java application,控制台打印如图:
20140805132610.jpg
Ok,客户端访问也成功了。
6、 附:
User.java
package com.hsy.pojo;
import java.io.Serializable;
@SuppressWarnings("serial")
public class User implements Serializable {
private String id;
private String name;
private String age;
private String description;
public User() {
super();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
二与spring集成实现webService
1、 配置web.xml
见文件web.xml
cxf
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
contextConfigLocation
WEB-INF/classes/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
CXFServlet
org.apache.cxf.transport.servlet.CXFServlet
1
CXFServlet
/webservice/*
encoding
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
encoding
*.jsp
encoding
*.html
encoding
*.do
encoding
*.action
encoding
*.jsp
encoding
*.html
encoding
*.do
encoding
*.3g
2、 配置applicationContext.xml
见文件applicationContext.xml
3、 修改客户端代码
见文件HelloWorldClient.java
package com.hsy.client;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import com.hsy.pojo.User;
import com.hsy.server.HelloWorld;
public class HelloWorldClient {
/**
* @param args
*/
public static void main(String[] args) {
//Resource resource= new FileSystemResource("F:/workspaces4me2013/.metadata/.me_tcat/WEB-INF/classes/applicationContext.xml");
//BeanFactory factory= new XmlBeanFactory(resource );
ApplicationContext factory = new ClassPathXmlApplicationContext("/applicationContext.xml");
HelloWorld client = (HelloWorld)factory.getBean("client");
User user1 = new User();
user1.setName("马克思");
user1.setDescription("怀念马克思");
User user2 = new User();
user2.setName("恩格斯");
user2.setDescription("怀念恩格斯");
List userList= new ArrayList();
userList.add(user1);
userList.add(user2);
String[] res = client.SayHiToUserList(userList);
System.out.println(res[0]);
System.out.println(res[1]);
}
}
4、 启动tamcat发布webService
然后在浏览器输入地址:http://localhost:8080/cxf/webservice/helloWorld?wsdl
如图:20140805133642.jpg
说明webService服务发布成功。
5、 运行客户端代码访问webService
右键 run as 选择java application,控制台打印如图:
20140805134838.jpg
Ok,客户端访问也成功了。
此篇实现了webService服务的发布以及在本工程下的客户端调用服务的示例,或许不是很直观。
请看下一篇:CXF客户端代码生成与服务调用(二)
http://blog.csdn.net/hu_shengyang/article/details/38384839
http://www.cnblogs.com/frankliiu-java/articles/1641949.html