让自己的电脑做网站的服务器,如何防范恶意网站,网站开发的岗位与分工,淘宝网站建设合同转自#xff1a;http://blog.csdn.net/hu_shengyang/article/details/38384597 以前工作中也用CXF,但都是用别人现成搭好的环境#xff0c;这次自己重头搭建一遍环境。过程中也有遇到的问题#xff0c;也做了简单的整理。 对于CXF是干什么用的#xff0c;我不想多说#x… 转自http://blog.csdn.net/hu_shengyang/article/details/38384597 以前工作中也用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(nametext)String text); String sayHiToUser(User user); String[] SayHiToUserList(ListUser 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(endpointInterfacecom.hsy.server.HelloWorld,serviceNameHelloWorld) public class HelloWorldImpl implements HelloWorld { MapInteger, User users new LinkedHashMapInteger, 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(ListUser 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 version1.0 encodingUTF-8? web-app xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlnshttp://java.sun.com/xml/ns/javaee xmlns:webhttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd xsi:schemaLocationhttp://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd idWebApp_ID version2.5 display-namecxf/display-name welcome-file-list welcome-fileindex.html/welcome-file welcome-fileindex.htm/welcome-file welcome-fileindex.jsp/welcome-file welcome-filedefault.html/welcome-file welcome-filedefault.htm/welcome-file welcome-filedefault.jsp/welcome-file /welcome-file-list context-param param-namecontextConfigLocation/param-name param-valueWEB-INF/classes/applicationContext.xml/param-value /context-param listener listener-class org.springframework.web.context.ContextLoaderListener /listener-class /listener servlet servlet-nameCXFServlet/servlet-name servlet-class org.apache.cxf.transport.servlet.CXFServlet /servlet-class load-on-startup1/load-on-startup /servlet servlet-mapping servlet-nameCXFServlet/servlet-name url-pattern/webservice/*/url-pattern /servlet-mapping !-- 字符过滤器 -- filter filter-nameencoding/filter-name filter-classorg.springframework.web.filter.CharacterEncodingFilter/filter-class init-param param-nameencoding/param-name param-valueUTF-8/param-value /init-param init-param param-nameforceEncoding/param-name param-valuetrue/param-value /init-param /filter filter-mapping filter-nameencoding/filter-name url-pattern*.jsp/url-pattern /filter-mapping filter-mapping filter-nameencoding/filter-name url-pattern*.html/url-pattern /filter-mapping filter-mapping filter-nameencoding/filter-name url-pattern*.do/url-pattern /filter-mapping filter-mapping filter-nameencoding/filter-name url-pattern*.action/url-pattern /filter-mapping filter-mapping filter-nameencoding/filter-name url-pattern*.jsp/url-pattern /filter-mapping filter-mapping filter-nameencoding/filter-name url-pattern*.html/url-pattern /filter-mapping filter-mapping filter-nameencoding/filter-name url-pattern*.do/url-pattern /filter-mapping filter-mapping filter-nameencoding/filter-name url-pattern*.3g/url-pattern /filter-mapping /web-app 2、 配置applicationContext.xml 见文件applicationContext.xml [html] view plain copy ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beans xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:jaxwshttp://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 resourceclasspath:META-INF/cxf/cxf.xml/ import resourceclasspath:META-INF/cxf/cxf-extension-soap.xml/ import resourceclasspath:META-INF/cxf/cxf-servlet.xml/ jaxws:endpoint idhelloWorld implementorcom.hsy.server.HelloWorldImpl address/helloWorld / bean idclient classcom.hsy.server.HelloWorld factory-beanclientFactory factory-methodcreate/ bean idclientFactory classorg.apache.cxf.jaxws.JaxWsProxyFactoryBean property nameserviceClass valuecom.hsy.server.HelloWorld/ property nameaddress valuehttp://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(怀念恩格斯); ListUser userList new ArrayListUser(); 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客户端访问也成功了。