CXF implementation WebService service (1)

I also used CXF in my previous work, but I used the environment built by others. This time I built the environment again. There were also problems encountered in the process, and I also did a simple sorting out.

As for what CXF is used for, I don’t want to say too much. Everyone knows that this is an implementation tool of webService technology in our java programming. Let’s talk about why CXF is used to implement webService:

1. Java’s webService implementation itself is a very performance-consuming implementation (the conversion between xml and java objects on the server and client side consumes performance)

2. At present, the mainstream webService applications of java are mainly CXF and AXIS2;

3. Through the understanding of online channels, the current efficiency of CXF is at least 50% higher than that of AXIS2;

4. In addition, there is a webService tool metro which is more efficient CXF is 10% higher;

5. CXF implementation materials can be found on the Internet, a lot of materials, metro materials are relatively small;

6. CXF is very mature in java application implementation. Enterprises are more inclined to use such a mature solution;

For the above reasons, I choose CXF to implement webService.

Reference:

Java Web Services: CXF performance comparison-performance comparison between CXF and the latest version of Axis2 and Metro

http://www.ibm.com/developerworks/cn/java/j -jws14/

Achieve the publishing of webService application by way of annotation

1. Basic Environment

After creating a new java web project cxf, download the cxf toolkit. After decompressing CXF, put all the jar packages under the cxf toolkit lib under the lib of the project.

The cxf toolkit version used here is: apache-cxf-2.7.12

Download link:

http://www.apache.org/dyn/closer.cgi?path=/cxf/2.7. 12/apache-cxf-2.7.12.zip

2. Write service interface

See file HelloWorld.java

< div class="dp-highlighter bg_java" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:936.531px; overflow-x:auto; overflow-y:hidden; padding-top: 1px; position:relative; line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

[java] view plain copy

  1. packagecom.hsy.server;
  2. importjava.util.List; /span>
  3. ​< /span>
  4. importjavax.jws.WebParam;
  5. < li style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin :0px!important; padding:0px 3px 0 px 10px!important; background-color:rgb(248,248,248)"> importjavax.jws.WebService;

  6. importcom.hsy.pojo.User;
  7. @WebService span>
  8. publicinterfaceHelloWorld{< /li>
  9. String sayHi(@WebParam(name=“text”)String text) ;
  10. String sayHiToUser( User user);
  11. String[] SayHiToUserList(ListuserList);
  12. }
  13. < /ol>



3. Service interface implementation

See file HelloWorldImpl.java

[java] view plain copy

    < li class="alt" style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color: inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> package< span style="margin:0px; padding:0px; border:none; background-color:inherit">com.hsy.server;
  1. importjava.util.LinkedHashMap;
  2. importjava.util .List;
  3. importjava.util.Map;< /span>
  4. importjavax.jws.WebParam;
  5. < span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">importjavax. jws.WebService;
  6. importcom.hsy.pojo.User; span>
  7. @WebService< /span>(endpointInterface=“com.hsy.server.HelloWorld”,serviceName=“HelloWorld”)
  8. public< span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">classHelloWorldImplimplementsHelloWorld{ span>
  9. Map users = newLinkedHashMap();
  10. public String sayHi(@WebParam(name= “text”) String text){
  11. < span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">return“Hello,”+text;
  12. public String sayHiToUser(User user){
  13. users.put(users.size()+1, user);
  14. span class=”keyword” style=”margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit”>return“Hello,”+user.getName();
  15. public String[] SayHiToUserList(List userList ) {  
  16.         String[] result = new String[userList.size()];  
  17.         int i = 0;  
  18.         for(User u:userList){  
  19.             result[i] = “Hello “ + u.getName();  
  20.             i++;  
  21.         }  
  22.         return result;  
  23.     }  
  24.   
  25.     /** 
  26.      * @param args 
  27.      */  
  28.     public static void main(String[] args) {  
  29.         // TODO Auto-generated method stub  
  30.   
  31.     }  
  32.   
  33. }  


4、  发布服务app

见文件webServiceApp.java

[java]  view plain  copy

  1. package com.hsy.server;  
  2.   
  3. import javax.xml.ws.Endpoint;  
  4.   
  5. public class webServiceApp {  
  6.   
  7.     /** 
  8.      * @param args 
  9.      */  
  10.     public static void main(String[] args) {  
  11.          System.out.println(“web service start”);  
  12.          HelloWorldImpl implemento r = new HelloWorldImpl();  
  13.          String address = “http://localhost:8080/helloWorld”;  
  14.          Endpoint.publish(address, implementor);  
  15.          System.out.println(“web service started”);  
  16.     }  
  17.   
  18. }  



右键 run as 选择java application发布服务;然后在浏览器输入地址:http://localhost:8080/helloWorld?wsdl

如图:20140805132120.jpg

说明webService服务发布成功。

 

5、  客户端访问服务

见文件HelloWorldClient.java

[java]  view plain  copy

  1. package com.hsy.client;  
  2.   
  3. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
  4.   
  5. import com.hsy.pojo.User;  
  6. import com.hsy.server.HelloWorld;  
  7.   
  8. public class  HelloWorldClient {  
  9.   
  10.     /** 
  11.      * @param args 
  12.      */  
  13.     public static void main(String[] args) {  
  14.           
  15.         //首先右键run as 运行com.hsy.server.webServiceApp类,然后再运行这段客户端代码  
  16.         JaxWsProxyFactoryBean jwpfb = new JaxWsProxyFactoryBean();  
  17.         jwpfb.setServiceClass(HelloWorld.class);  
  18.         jwpfb.setAddress(“http://localhost:8080/helloWorld”);  
  19.         HelloWorld hw = (HelloWorld) jwpfb.create();  
  20.         User user = new User();  
  21.         user.setName(“马克思”);  
  22.         user.setDescription( “怀念马克思”);  
  23.         System.out.println(hw.sayHiToUser(user));  
  24.           
  25.     }  
  26.   
  27. }  



右键 run as 选择java application,控制台打印如图:

20140805132610.jpg

Ok,客户端访问也成功了。

6、  附:

User.java

[java]  view plain  copy

  1. package com.hsy.pojo;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. @SuppressWarnings(“serial”)  
  6. public class User implements Serializable {  
  7.   
  8.     private String id;  
  9.     private String name;  
  10.     private String age;  
  11.     private String description;  
  12.       
  13.     public User() {  
  14.         super();  
  15.     }  
  16.   
  17.     public String getId() {  
  18.         return id;  
  19.     }  
  20.   
  21.     public void setId(String id) {  
  22.         this.id = id;  
  23.     }  
  24.   
  25.     public String getName() {  
  26.         return name;  
  27.     }  
  28.   
  29.     public void setName(String name) {  
  30.         this.name = name;  
  31.     }  
  32.   
  33.     public String getAge() {  
  34.         return age;  
  35.     }  
  36.   
  37.     public void setAge(String age) {  
  38.         this< /span>.age = age;  
  39.     }  
  40.   
  41.     public String getDescription() {  
  42.         return description;  
  43.     }  
  44.   
  45.     public void setDescription(String description) {  
  46.         this.description = description;  
  47.     }  
  48.       
  49.       
  50. }  



 

二与spring集成实现webService

1、  配置web.xml

见文件web.xml

[html]  view plain  copy

  1. xml version=“1.0” encoding=“UTF-8”?>  
  2. <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”>  
  3.   <display-name>cxfdisplay-name>  
  4.   <welcome-file-list>  
  5.     <welcome-file>index.htmlwelcome-file>  
  6.     <welcome-file>index.htmwelcome-file>  
  7.     <welcome-file>index.jspwelcome-file>  
  8.     <welcome-file>default.htmlwelcome-file>  
  9.     <welcome-file>default.htmwelcome-file>  
  10.     <w elcome-file>default.jspwelcome-file>  
  11.   welcome-file-list>  
  12.     
  13.     <context-param>  
  14.         <param-name>contextConfigLocationparam-name>  
  15.         <param-value>WEB-INF/classes/applicationContext.xmlparam-value>  
  16.     context-param>  
  17.   
  18.     <listener>  
  19.         <listener-class>  
  20.               org.springframework.web.context.ContextLoaderListener  
  21.         listener-class>  
  22.     listener>  
  23.   
  24.     <servlet>  
  25.         <servlet-name>CXFServletservlet-name>  
  26.         <servlet-class>  
  27.                org.apache.cxf.transport.servlet.CXFServlet  
  28.         servlet-class>  
  29.         <load-on-startup>1load-on-startup>  
  30.     servlet>  
  31.   
  32.     <servlet-mapping>  
  33.          <servlet-name>CXFServletservlet-name>  
  34.          <url-pattern>/webservice/*url-pattern>  
  35.     servlet-mapping>  
  36.     
  37.     
  38.     
  39.     
  40.       
  41.     <filter>    
  42.         <filter-name>encodingfilter-name>    
  43.         <filter-class>org. springframework.web.filter.CharacterEncodingFilterfilter-class>    
  44.         <init-param>    
  45.             <param-name>encodingparam-name>    
  46.             <param-value>UTF-8 param-value>    
  47.         init-param>    
  48.         <init-param>    
  49.             <param-name>forceEncodingparam-name>    
  50.             <param-value>trueparam-value>    
  51.         init-param>    
  52.     filter>    
  53.         
  54.         
  55.     <filter-mapping>    
  56.         <filter-name>encodingfilter-name>    
  57.         <url-pattern>*.jspurl-pattern>    
  58.     filter-mapping>    
  59.     <filter-mapping>    
  60.         <filter-name>encodingfilter-name>    
  61.         <url-pattern>*.htmlurl-pattern>    
  62.     filter-mapping>    
  63.     <filter-mapping>    
  64.         <filter-name>encodingfilter-name>    
  65.         <url-pattern>*.dourl-pattern>    
  66.     filter-mapping>    
  67.     <filter-mapping>    
  68.         <filter-name>encodingfilter-name>    
  69.         <url-pattern>*.actionurl-pattern>    
  70.     filter-mapping>   
  71.     <filter-mapping>    
  72.         <filter-name>encodingfilter-name>    
  73.         <url-pattern>*.jspurl-pattern>< /span>    
  74.     filter-mapping>    
  75.     <filter-mapping>    
  76.         <filter-name>encodingfilter-name>    
  77.         <url-pattern>*.htmlurl-pattern>    
  78.     filter-mapping>    
  79.     <filter-mapping>    
  80.         <filter-name>encodingfilter-name>    
  81.         <url-pattern>*.dourl-pattern>    
  82.     filter-mapping>    
  83.     <filter-mapping>    
  84.         <filter-name>encodingfilter-name>    
  85.         <url-pattern>*.3gurl-pattern>    
  86.     filter-mapping>     
  87.     
  88. web-app>  



 

2、  配置applicationContext.xml

见文件applicationContext.xml

[html]  view plain  copy

  1. xml version=“1.0” encoding=“UTF-8”?>  
  2. <beans xmlns=“http://www.springframework.org/schema/beans”  
  3.        xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”  
  4.        xmlns:jaxws=“http://cxf.apache.org/jaxws”  
  5.        xsi:schemaLocation=”  
  6.              http://www.springframework.org/schema/beans  
  7.              http://www.springframework.org/schema/beans/spring-beans.xsd  
  8.              http://cxf.apache.org/jaxws   
  9.              http://cxf.apache.org/schemas/jaxws.xsd”>  
  10.   
  11.       <import resource=“classpath:META-INF/cxf/cxf.xml”/>  
  12.       <import resource=“classpath:META-INF/cxf/cxf-extension-soap.xml”/>  
  13.       <import resource=“classpath:META-INF/cxf/cxf-servlet.xml”/>  
  14.   
  15.       < jaxws:endpoint   
  16.              id=“helloWorld”  
  17.              implementor=“com.hsy.server.HelloWorldImpl”  
  18.              address=“/helloWorld” />  
  19.   
  20.      <bean id=“client”   
  21.             class=“com.hsy.server.HelloWorld”   
  22.             factory-bean=“clientFactory”   
  23.             factory-method=“create”/>  
  24.   
  25.      <bean id=“clientFactory” class=“org.apache.cxf.jaxws.JaxWsProxyFactoryBean”>  
  26.             <property name=“serviceClass” value=“com.hsy.server.HelloWorld”/>  
  27.             <property name=“address” value=“http://localhost:8080/cxf/webservice/helloWorld”/>  
  28.      bean>  
  29.        
  30. beans>  



 

3、  修改客户端代码

见文件HelloWorldClient.java

[java]  view plain  copy

  1. package com.hsy.client;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import org.springframework.beans.factory.BeanFactory;  
  7. import org.springframework.beans.factory.xml.XmlBeanFactory;  
  8. import org.springframework.context.ApplicationContext;  
  9. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  10. import org.springframework.core.io.FileSystemResource;  
  11. import org.springframework.core.io.Resource;  < /span>
  12.   
  13. import com.hsy.pojo.User;  
  14. import com.hsy.server.HelloWorld;  
  15.   
  16. public class HelloWorldClient {  
  17.   
  18.     /** 
  19.      * @param args 
  20.      */  
  21.     public static void main(String[] args) {  
  22.           
  23.         //Resource resource= new FileSystemResource(“F:/workspaces4me2013/.metadata/.me_tcat/WEB-INF/classes/applicationContext.xml”);     
  24.         //BeanFactory factory= new XmlBeanFactory(resource );   
  25.         ApplicationContext factory = new ClassPathXmlApplicationContext(“/applicationContext.xml”);  
  26.         HelloWorld client = (HelloWorld)factory.getBean(“client”);  
  27.         User user1 = new User();  
  28.         user1.setName(“马克思”);  
  29.         user1.setDescription(“怀念马克思”);  
  30.         User user2 = new User();  
  31.         user2.setName(“恩格斯”);  
  32.         user2.setDescription(“怀念恩格斯”);  
  33.         List userList= new ArrayList();  
  34.         userList.add(user1);  
  35.         userList.add(user2);  
  36.         String[] res = client.SayHiToUserList(userList);  
  37.         System.out.println(res[0 ]);  
  38.         System.out.println(res[1]);    
  39.           
  40.     }  
  41.   
  42. }  



 

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/franklii u-java/articles/1641949.html

[java]  view plain  copy

  1. package com.hsy.server;  
  2.   
  3. import java.util.List;  
  4.   
  5. import javax.jws.WebParam;  
  6. import javax.jws. WebService;  
  7.   
  8. import com.hsy.pojo.User;  
  9.   
  10. @WebService  
  11. public interface HelloWorld {  
  12.     String sayHi(@WebParam(name=“text”)String text);  
  13.     String sayHiToUser(User user);  
  14.     String[] SayHiToUserList(List userList);  
  15. }  

[java]  view plain  copy

[java]  view plain  copy

[java]  view plain  copy

  1. package com.hsy.server;  
  2.   
  3. import java.util.LinkedHashMap;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6.   
  7. import javax.jws.WebParam;  
  8. import javax.jws.WebService;  
  9.   
  10. import com.hsy.pojo.User;  
  11.   
  12. @WebService(endpointInterface=“com.hsy.server.HelloWorld”,serviceName=“HelloWorld”)  
  13. public class HelloWorldImpl implements HelloWorld {  
  14.     Map users = new LinkedHashMap();  
  15.   
  16.     public String sayHi(@WebParam(name = < /span>“text”) String text) {  
  17.         return “Hello,”+text;  
  18.     }  
  19.   
  20.     public String sayHiToUser(User user) {  
  21.         users.put(users.size()+1, user);  
  22.         return “Hello,”+user.getName();  
  23.     }  
  24.   
  25.     public String[] SayHiToUserList(List userList) {  
  26.         String[] result = new String[userList.size()];  
  27.         int i = 0;  
  28.         for(User u:userList){  
  29.             result[i] = “Hello “ + u.getName();  
  30.             i++;  
  31.         }  
  32.         return result;  
  33.     }  
  34.   
  35.     /** 
  36.      * @param args 
  37.      */  
  38.     public static void main(String[] args) {  
  39.         // TODO Auto-generated method stub  
  40.   
  41.     }  
  42.   
  43. }  

[java]  view plain  copy

[java]  view plain  copy

[java]  view plain  copy

  1. package com.hsy.server;  
  2.   
  3. import javax.xml.ws.Endpoint;  
  4.   
  5. public class webServiceApp {  
  6.   
  7.     /** 
  8.      * @param args 
  9.      */  
  10.     public static void main(String[] args) {  
  11.          System.out.println(“web service start”);  
  12.          HelloWorldImpl implementor = new HelloWorldImpl();  
  13.          String address = “http://localhost:8080/helloWorld”;  
  14.          Endpoint.publish(address, implementor);  
  15.          System.out.println(“web service started”);  
  16.     }  
  17.   
  18. }  

[java]  view plain  copy

[java]  view plain  copy

[java]  view plain  copy

  1. package com.hsy.client;  
  2.   
  3. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
  4.   
  5. import com.hsy.pojo.User;  
  6. import com.hsy.server.HelloWorld;  
  7.   
  8. public class HelloWorldClient {  
  9.   
  10.     /** 
  11.      * @param args 
  12.      */  
  13.     public static void main(String[] args) {  
  14.           
  15.         //首先右键run as 运行com.hsy.server.webServiceApp类,然后再运行这段客户端代码  
  16.         JaxWsProxyFactoryBean jwpfb = new JaxWsProxyFactoryBean();  
  17.         jwpfb.setServiceClass(HelloWorld.class);  
  18.         jwpfb.setAddress(“http://localhost:8080/helloWorld”);  
  19.         HelloWorld hw = (HelloWorld) jwpfb.create();  
  20.         User user = new User();  
  21.         user.setName(“马克思”);  
  22.         user.setDescription(“怀念马克思”);  
  23.         System.out.println(hw.sayHiToUser(user));  
  24.           
  25. < span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">    }  
  26.   
  27. }  

[java]  view plain  copy

[java]  view plain  copy

[java ]  view plain  copy

  1. package com.hsy.pojo;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. @SuppressWarnings(“serial”)  
  6. public class User implements Serializable {  
  7.   
  8.     private String id;  
  9.     private String name;  
  10.     private String age;  
  11.     private String description;  
  12.       
  13.     public User() {  
  14.         super();  
  15.     }  
  16.   
  17.     public String getId() {  
  18.         return id;  
  19.     }  
  20.   
  21.     public void setId(String id) {  
  22.         this.id = id;  
  23.     }  
  24.   
  25.     public String getNa me() {  
  26.         return name;  
  27.     }  
  28.   

  29.     public void setName(String name) {  
  30.         this.name = name;  
  31.     }  
  32.   
  33.     public String getAge() {  
  34.         return age;  
  35.     }  
  36.   
  37.     public void setAge(String age) {  
  38.         this.age = age;  
  39.     }  
  40.   
  41.     public String getDescription() {  
  42.         return description;  
  43.     }  
  44.   
  45.     public void setDescription(String description) {  
  46.         this.description = description;  
  47.     }  
  48.       
  49.       
  50. }  

[java]  view plain  copy

[java]  view plain  copy

[html]  view plain  copy

  1. xml version=“1.0” encoding=“UTF-8”?>  
  2. <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”>  
  3.   <display-name>cxfdisplay-name>  
  4.   <welcome-file-list>  
  5.     <welcome-file>index.htmlwelcome-file>  
  6.     <welcome-file>index.htmwelcome-file>  
  7.     <welcome-file>index.jspwelcome-file>  
  8.     <welcome-file>default.htmlwelcome-file>  
  9.     <welcome-file>default.htmwelcome-file>  
  10.     <welcome-file>default.jspwelcome-file>  
  11. < li class="alt" style="border-style:none none none solid; border-left-width:3px; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">   welcome-file-list>  

  12.     
  13.     <context-param>< /span>  
  14.         <param-name>contextConfigLocationparam-name>  
  15.         <param-value>WEB-INF/classes/applicationContext.xmlparam-value>  
  16.     context-param>  
  17.   
  18.     <listener>  
  19.         <listener-class>  
  20.               org.springframework.web.context.ContextLoaderListener  
  21.         listener-class>  
  22.     listener>  
  23.   
  24.     <servlet>  
  25.         <servlet-name>CXFServletservlet-name>  
  26.         <servlet-class>  
  27.                org.apache.cxf.transport.servlet.CXFServlet  
  28.         servlet-class>  
  29.         <load-on-startup>1load-on-startup>  
  30.     servlet>  
  31.   
  32.     <servlet-mapping>  
  33.          <serv let-name>CXFServletservlet-name>  
  34.          <url-pattern>/webservice/*url-pattern>  
  35.     servlet-mapping>  
  36.     
  37.     
  38.     
  39.     
  40.       
  41.     <filter>    
  42.         <filter-name>encodingfilter-name>    
  43.         <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>    
  44.         <init-param>    
  45.             <param-name>encodingparam-name>    
  46.             <param-value>UTF-8param-value>    
  47.         init-param>    
  48.         <init-param>    
  49.             <param-name>forceEncodingparam-name>    
  50.             <param-value>trueparam-value>    
  51.         init-param>    
  52.     filter>    
  53.         
  54.         
  55.     <filter-mapping>    
  56.         <filter-name>encodingfilter-name>    
  57.         <url-pattern>*.jspurl-pattern>    
  58.     filter-mapping>    
  59.     <filter-mapping>    
  60.         <filter-name>encodingfilter-name>    
  61.         <url-pattern>*.htmlurl-pattern>    
  62.     filter-mapping>    
  63.     <filter-mapping>    
  64.         <filter-name>encodingfilter-name>    
  65.         <url-pattern>*.dourl-pattern>    
  66.     filter-mapping>    
  67.     <filter-mapping>    
  68.         <filter-name>encodingfilter-name>    
  69.         <url-pattern>*.actionurl-pattern>    
  70.     filter-mapping>   
  71.     <filter-mapping>    
  72.         <filter-name>encodingfilter-name>    
  73.         <url-pattern>*.jspurl-pattern>    
  74.     filter-mapping>    
  75.     <filter-mapping>    
  76.         <filter-name>encodingfilter-name>    
  77.         <url-pattern>*.htmlurl-pattern>    
  78.     filter-mapping>    
  79.     <filter-mapping>    
  80.         <filter-name>encodingfilter-name>    
  81.         <url-pattern>*.dourl-pattern>    
  82.     filter-mapping>    
  83.     <filter-mapping>    
  84.         <filter-name>encodingfilter-name>    
  85.         <url-pattern>*.3gurl-pattern>    
  86.     filter-mapping>     
  87.     
  88. web-app>  

[html]  view plain  copy

[html]  view plain  copy

[html]  view plain  copy

  1. xml version=“1.0” encoding=“UTF-8”?>  
  2. <beans xmlns=“http://www.springframework.org/schema/beans”  
  3.        xmlns:xsi=< span class="attribute-value" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">“http://www.w3.org/2001/XMLSchema-instance”  
  4.        xmlns:jaxws=“http://cxf.apache.org/jaxws”  
  5.        xsi:schemaLocation=”  
  6.              http://www.springframework.org/schema/beans  
  7.              http://www.springframework.org/schema/beans/spring-beans.xsd  
  8.              http://cxf.apache.org/jaxws   
  9.              http://cxf.apache.org/schemas/jaxws.xsd”>  
  10.   
  11.       <import resource=“classpath:META-INF/cxf/cxf.xml”/>  
  12.       <import resource=“classpath:META-INF/cxf/cxf-extension-soap.xml”/>  
  13.       <import resource=“classpath:META-INF/cxf/cxf-servlet.xml”/>  
  14.   
  15.       <jaxws:endpoint   
  16.              id=“helloWorld”  
  17.              implementor=“com.hsy.server.HelloWorldImpl”  
  18.              address=“/helloWorld” />  
  19.   
  20.      <bean id=“client”   
  21.             class=“com.hsy.server.HelloWorld”   
  22.             factory-bean=“clientFactory”   
  23.             factory-method=“create”/>  
  24.   
  25.      <bean id=“clientFactory” class=“org.apache.cxf.jaxws.JaxWsProxyFactoryBean”>  
  26.             <property name=“serviceClass” value=“com.hsy.server.HelloWorld”/>  
  27.             <property name=“address” value=“http://localhost:8080/cxf/webservice/helloWorld”/>  
  28.      bean>  
  29.        
  30. beans>  

[html]  view plain  copy

[html]  view plain  copy

[java]  view plain  copy

  1. package com.hsy.client;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import org.springframework.beans.factory.BeanFactory;  
  7. import org.springframework.beans.factory.xml.XmlBeanFactory;  
  8. import org.springframework.context.ApplicationContext;  
  9. import org.springframework.context.support.ClassPathXmlApplicationContext;  < /li>
  10. import org.springframework.core.io.FileSystemResource;  
  11. import org.springframework.core.io.Resource;  
  12.   
  13. import com.hsy.pojo.User;  
  14. import com.hsy.server.HelloWorld;  
  15.   
  16. public class HelloWorldClient {  
  17.   
  18.     /** 
  19.      * @param args 
  20.      */  
  21.     public static void main(String[] args) {  
  22.           
  23.         //Resource resource= new FileSystemResource(“F:/workspaces4me2013/.metadata/.me_tcat/WEB-INF/classes/applicationContext. xml”);     
  24.         //BeanFactory factory= new XmlBeanFactory(resource );   
  25.         ApplicationContext factory = new ClassPathXmlApplicationContext(“/applicationContext.xml”);  
  26.         HelloWorld client = (Hel loWorld)factory.getBean(“client”);  
  27.         User user1 = new User();  
  28.         user1.setName(“马克思”);  
  29.         user1.setDescription(“怀念马克思”);  
  30.         User user2 = new User();  
  31.         user2.setName(“恩格斯”);  
  32.         user2.setDescription(“怀念恩格斯”);  
  33.         List userList= new ArrayList();  
  34.         userList.add(user1);  
  35.         userList.add(user2);  
  36.         String[] res = client.SayHiToUserList(userList);  
  37.         System.out.println(res[0]);  
  38.         System.out.println(res[1]);    
  39.           
  40.     }  
  41.   
  42. }  

[java]  view plain  copy

[java]  view plain  copy

Leave a Comment

Your email address will not be published.