码迷,mamicode.com
首页 > Web开发 > 详细

使用cxf实现webService服务

时间:2016-03-31 14:52:44      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:

本人第一次写文章,各种工具使用还不熟悉,在编写过程中也出现不少问题。这里,不会深入到为什么,只是将用代码加文字描述把一个webService的创建到使用以及与spring的整合叙述一下,也希望大神们多多指教。

1.首先看下webService的目录结构。

1)HelloWorldClient这个是用于前期测试使用。后面会从新创建一个项目作为客户端调用service。

2)pojo,实体类,这个相信有一点基础的人都知道是干嘛的,不再叙述。

3)webServiceApp用于前期未引入spring进行测试。

4)service层的接口与实现。

5)applicationContext.xml 未引入spring 前不使用。

技术分享

技术分享

2.下面开始具体描述。

1)新建java web工程 之后,下载cxf工具包。解压CXF之后,把cxf工具包lib下的jar包全部放到工程的lib下。可以去官方下载,下载地址:http://cxf.apache.org/download.html。

2)创建User.java

------------------------------------------------------------------------------------------

package com.jian.pojo;
import java.io.Serializable;
@SuppressWarnings("serial")
public class User implements Serializable {
private String id;
private String name;
private String age;
private String description;
      //此处省略get,set ,toString方法
}

-------------------------------------------------------------------------------------------

3)创建接口HelloWorld.java

-------------------------------------------------------------------------------------------

package com.jian.service;
import java.util.List;
import javax.jws.WebParam;
import javax.jws.WebService;
import com.jian.pojo.User;
@WebService  
public interface HelloWorld {
   String sayHi(@WebParam(name="text")String text);  
   String sayHiToUser(User user);  
   String[] SayHiToUserList(List<User> userList);  
}

-----------------------------------------------------------------------------------------

4)service接口实现 HelloWorldImpl.java

-----------------------------------------------------------------------------------------

package com.jian.service.impl;
import java.util.List;
import javax.jws.WebService;
import com.jian.pojo.User;
import com.jian.service.HelloWorld;
@WebService(endpointInterface = "com.jian.service.HelloWorld", serviceName = "helloWorld")   //这里注意com.jian.service.HelloWorld不少情况下容易弄错
public class HelloWorldImpl implements HelloWorld {
public String sayHi(String text) {
return "hello" + text;
}
public String sayHiToUser(User user) {
return "hello " + user.getName() + user.getDescription();
}
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;
}
}

---------------------------------------------------------------------------------------------------------------------------

5)接下来可以编写服务端启动代码了webServiceApp.java

---------------------------------------------------------------------------------------------------------------------------

package com.jian.server;
import javax.xml.ws.Endpoint;
import com.jian.service.impl.HelloWorldImpl;

public class webServiceApp {  
  
    public static void main(String[] args) {  
         System.out.println("web service start");  
         HelloWorldImpl implementor = new HelloWorldImpl();  
         String address = "http://localhost:8080/cxfWebService";  
         Endpoint.publish(address, implementor);  
         System.out.println("web service started");  
    }  
  
}  

-----------------------------------------------------------------------------------------------------

6)在编写客户端代码  HelloWorldClient .java

-----------------------------------------------------------------------------------------------------

package com.jian.client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.jian.pojo.User;
import com.jian.service.HelloWorld;

public class HelloWorldClient {
public static void main(String[] args) {
//首先右键run as 运行com.jian.server.webServiceApp类,然后再运行这段客户端代码  
JaxWsProxyFactoryBean jwpfb = new JaxWsProxyFactoryBean();  
jwpfb.setServiceClass(HelloWorld.class);
jwpfb.setAddress("http://localhost:8080/cxfWebService");  
HelloWorld hw = (HelloWorld) jwpfb.create();  
User user = new User();  
user.setName("马克思");   
user.setDescription("怀念马克思");  
System.out.println(hw.sayHiToUser(user)); 
}

   }

--------------------------------------------------------------------------------------------------------

7)到这里,一个未整合spring的webService已经完成了。先启动webServiceApp,启动后可以看到如下图,说明启动正常

技术分享

技术分享

然后再启动 HelloWorldClient ,如果未报错,可以看到结果:

技术分享

技术分享

8)接下来,整合spring。

n1:配置web.xml

  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>cxf</display-name>  
  4.   <welcome-file-list>  
  5.     <welcome-file>index.html</welcome-file>  
  6.     <welcome-file>index.htm</welcome-file>  
  7.     <welcome-file>index.jsp</welcome-file>  
  8.     <welcome-file>default.html</welcome-file>  
  9.     <welcome-file>default.htm</welcome-file>  
  10.     <welcome-file>default.jsp</welcome-file>  
  11.   </welcome-file-list>  
  12.     
  13.     <context-param>  
  14.         <param-name>contextConfigLocation</param-name>  
  15.         <param-value>WEB-INF/classes/applicationContext.xml</param-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>CXFServlet</servlet-name>  
  26.         <servlet-class>  
  27.                org.apache.cxf.transport.servlet.CXFServlet  
  28.         </servlet-class>  
  29.         <load-on-startup>1</load-on-startup>  
  30.     </servlet>  
  31.   
  32.     <servlet-mapping>  
  33.          <servlet-name>CXFServlet</servlet-name>  
  34.          <url-pattern>/*</url-pattern>  
  35.     </servlet-mapping>  
  36.     
  37.     
  38.     
  39.     
  40.   <!-- 字符过滤器 -->    
  41.     <filter>    
  42.         <filter-name>encoding</filter-name>    
  43.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>    
  44.         <init-param>    
  45.             <param-name>encoding</param-name>    
  46.             <param-value>UTF-8</param-value>    
  47.         </init-param>    
  48.         <init-param>    
  49.             <param-name>forceEncoding</param-name>    
  50.             <param-value>true</param-value>    
  51.         </init-param>    
  52.     </filter>    
  53.         
  54.         
  55.     <filter-mapping>    
  56.         <filter-name>encoding</filter-name>    
  57.         <url-pattern>*.jsp</url-pattern>    
  58.     </filter-mapping>    
  59.     <filter-mapping>    
  60.         <filter-name>encoding</filter-name>    
  61.         <url-pattern>*.html</url-pattern>    
  62.     </filter-mapping>    
  63.     <filter-mapping>    
  64.         <filter-name>encoding</filter-name>    
  65.         <url-pattern>*.do</url-pattern>    
  66.     </filter-mapping>    
  67.     <filter-mapping>    
  68.         <filter-name>encoding</filter-name>    
  69.         <url-pattern>*.action</url-pattern>    
  70.     </filter-mapping>   
  71.     <filter-mapping>    
  72.         <filter-name>encoding</filter-name>    
  73.         <url-pattern>*.jsp</url-pattern>    
  74.     </filter-mapping>    
  75.     <filter-mapping>    
  76.         <filter-name>encoding</filter-name>    
  77.         <url-pattern>*.html</url-pattern>    
  78.     </filter-mapping>    
  79.     <filter-mapping>    
  80.         <filter-name>encoding</filter-name>    
  81.         <url-pattern>*.do</url-pattern>    
  82.     </filter-mapping>    
  83.     <filter-mapping>    
  84.         <filter-name>encoding</filter-name>    
  85.         <url-pattern>*.3g</url-pattern>    
  86.     </filter-mapping>     
  87.     
  88. </web-app>  

n2: 配置applicationContext.xml

---------------------------------------------------------------------------------------------------------

<?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.jian.service.impl.HelloWorldImpl" address="/helloWorld" />
<bean id="client" class="com.jian.service.HelloWorld" factory-bean="clientFactory" factory-method="create" />
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.jian.service.HelloWorld" />
<property name="address" value="http://localhost:8080/cxfWebService" />
</bean>
</beans>  

-------------------------------------------------------------------------------------------------------------------

n3:修改测试  HelloWorldClient 

-------------------------------------------------------------------------------------------------------------------

package com.jian.client;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.jian.pojo.User;
import com.jian.service.HelloWorld;

public class HelloWorldClient {
public static void main(String[] args) {
    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); 
       System.out.println( client.sayHiToUser(user1));

-------------------------------------------------------------------------------------------------------------------------------------------

运行后发现报错。但是今天运行了下又没有报错,不知道问题出在哪里了。

如果报错的话可以执行一下操作。

技术分享

个人寻找到的原因是该项目不是webservice的客户端要无法接下xml格式文件,这时,可以重新创建一个新的项目作为客户端。如下:

new-->other --webServiceClient

技术分享

然后点击create a new web service project 

技术分享

创建完成后会出先如下图:

技术分享

填写wsdl url

技术分享

然后next。。。就OK了,客户端会获取到service端的相关内容。

在这里可以做测试了

测试小例子:test.java

-------------------------------------------------------------------------------------------------------------------------

import com.jian.service.SayHiToUser;
import com.jian.service.User;

public class test {
   public static void main(String[] args) {
  User u = new User();
  u.setDescription("helloworld");
  u.setName("马克思");
  SayHiToUser he = new SayHiToUser();
  he.setArg0(u);
 System.out.println(he.getArg0().getName() + he.getArg0().getDescription())  ;
   }
}

------------------------------------------------------------------------------------------------------------------------------------

输出结果

技术分享

这样,一个cxf 与spring的服务器端与客户端的程序就结束了。


参考的文档:http://blog.csdn.net/hu_shengyang/article/details/38384597


使用cxf实现webService服务

标签:

原文地址:http://blog.csdn.net/u010166195/article/details/51023220

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!