前工作中也用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
[java] view plain copy
- 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<User> userList);
- }
3、 服务接口实现
见文件HelloWorldImpl.java
[java] view plain copy
- 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<Integer, User> users = new LinkedHashMap<Integer, User>();
- 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<User> 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
[java] view plain copy
- 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
[java] view plain copy
- 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
[java] view plain copy
- 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
[html] view plain copy
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
- <display-name>cxf</display-name>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- <welcome-file>index.htm</welcome-file>
- <welcome-file>index.jsp</welcome-file>
- <welcome-file>default.html</welcome-file>
- <welcome-file>default.htm</welcome-file>
- <welcome-file>default.jsp</welcome-file>
- </welcome-file-list>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>WEB-INF/classes/applicationContext.xml</param-value>
- </context-param>
- <listener>
- <listener-class>
- org.springframework.web.context.ContextLoaderListener
- </listener-class>
- </listener>
- <servlet>
- <servlet-name>CXFServlet</servlet-name>
- <servlet-class>
- org.apache.cxf.transport.servlet.CXFServlet
- </servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>CXFServlet</servlet-name>
- <url-pattern>/webservice/*</url-pattern>
- </servlet-mapping>
- <!-- 字符过滤器 -->
- <filter>
- <filter-name>encoding</filter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- <init-param>
- <param-name>forceEncoding</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>encoding</filter-name>
- <url-pattern>*.jsp</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>encoding</filter-name>
- <url-pattern>*.html</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>encoding</filter-name>
- <url-pattern>*.do</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>encoding</filter-name>
- <url-pattern>*.action</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>encoding</filter-name>
- <url-pattern>*.jsp</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>encoding</filter-name>
- <url-pattern>*.html</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>encoding</filter-name>
- <url-pattern>*.do</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>encoding</filter-name>
- <url-pattern>*.3g</url-pattern>
- </filter-mapping>
- </web-app>
2、 配置applicationContext.xml
见文件applicationContext.xml
[html] view plain copy
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:jaxws="http://cxf.apache.org/jaxws"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://cxf.apache.org/jaxws
- http://cxf.apache.org/schemas/jaxws.xsd">
- <import resource="classpath:META-INF/cxf/cxf.xml"/>
- <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
- <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
- <jaxws:endpoint
- id="helloWorld"
- implementor="com.hsy.server.HelloWorldImpl"
- address="/helloWorld" />
- <bean id="client"
- class="com.hsy.server.HelloWorld"
- factory-bean="clientFactory"
- factory-method="create"/>
- <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
- <property name="serviceClass" value="com.hsy.server.HelloWorld"/>
- <property name="address" value="http://localhost:8080/cxf/webservice/helloWorld"/>
- </bean>
- </beans>
3、 修改客户端代码
见文件HelloWorldClient.java
[java] view plain copy
- 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<User> userList= new ArrayList<User>();
- 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
本文参照了:使用 CXF做 webservice简单例子
http://www.cnblogs.com/frankliiu-java/articles/1641949.html