CXF整合Spring之JaxWsProxyFactoryBean调用

1.见解

1.1 客户端的接口代码还一定要和服务端的接口代码一样,连注解都要一样,不够灵活

1.2 当客户端访问服务器的请求地址时,如果服务端没有对应的地址,就会报错,但是又没有cxf的异常捕获处理

1.3 address中的url是模糊匹配的

//服务端代码

2.UserBean.Java

 

Java代码  收藏代码
  1. package cn.hk.bean;  
  2.   
  3. public class UserBean {  
  4.     private int id;  
  5.     private String userName;  
  6.     private String password;  
  7.     public UserBean(){  
  8.           
  9.           
  10.     }  
  11.     public UserBean(int id, String userName, String password) {  
  12.         super();  
  13.         this.id = id;  
  14.         this.userName = userName;  
  15.         this.password = password;  
  16.     }  
  17.     public int getId() {  
  18.         return id;  
  19.     }  
  20.     public void setId(int id) {  
  21.         this.id = id;  
  22.     }  
  23.     public String getUserName() {  
  24.         return userName;  
  25.     }  
  26.     public void setUserName(String userName) {  
  27.         this.userName = userName;  
  28.     }  
  29.     public String getPassword() {  
  30.         return password;  
  31.     }  
  32.     public void setPassword(String password) {  
  33.         this.password = password;  
  34.     }  
  35.     @Override  
  36.     public String toString() {  
  37.         return "UserBean [id=" + id + ", password=" + password + ", userName="  
  38.                 + userName + "]";  
  39.     }  
  40.       
  41.       
  42. }  

 

 3.IUserDao.java

 

Java代码  收藏代码
  1. package cn.hk.dao;  
  2.   
  3. import java.util.List;  
  4.   
  5. import javax.jws.WebParam;  
  6. import javax.jws.WebService;  
  7.   
  8. import cn.hk.bean.UserBean;  
  9.   
  10. @WebService  
  11. public interface IUserDao {  
  12.   
  13.     public UserBean getUser(@WebParam(name="id") int id);  
  14.     public String getUserName(int id);  
  15.     public List<UserBean> getUserList();  
  16. }  

 

4.IUserDaoImpl.java

Java代码  收藏代码
  1. package cn.hk.dao.impl;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import javax.jws.WebParam;  
  7. import javax.jws.WebService;  
  8.   
  9. import cn.hk.bean.UserBean;  
  10. import cn.hk.dao.IUserDao;  
  11. @WebService  
  12. public class IUserDaoImpl implements IUserDao {  
  13.   
  14.     @Override  
  15.     public UserBean getUser(@WebParam(name="id") int id) {  
  16.           
  17.            
  18.         return new UserBean(id,"zhangsan","zhangsan");  
  19.     }  
  20.   
  21.     @Override  
  22.     public List<UserBean> getUserList() {  
  23.         List<UserBean> list=new ArrayList<UserBean>();  
  24.         UserBean u1=new UserBean(1,"lisi","lisi");  
  25.         list.add(u1);  
  26.         u1=new UserBean(2,"wanwu","wanwu");  
  27.         list.add(u1);  
  28.         u1=new UserBean(3,"tiantian","tiantian");  
  29.         list.add(u1);  
  30.         return list;  
  31.     }  
  32.   
  33.     @Override  
  34.     public String getUserName(int id) {  
  35.           
  36.         return "id:"+id+"name:zhangsan";  
  37.     }  
  38.   
  39. }   

  5.applicationContext.xml

 

Xml代码  收藏代码
  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:aop="http://www.springframework.org/schema/aop"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xmlns:jaxws="http://cxf.apache.org/jaxws"   
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  8.     http://www.springframework.org/schema/beans/spring-beans.xsd   
  9.     http://www.springframework.org/schema/tx   
  10.     http://www.springframework.org/schema/tx/spring-tx.xsd   
  11.     http://www.springframework.org/schema/aop   
  12.     http://www.springframework.org/schema/aop/spring-aop.xsd  
  13.     http://cxf.apache.org/jaxws   
  14.     http://cxf.apache.org/schemas/jaxws.xsd">  
  15.     <import resource="classpath:META-INF/cxf/cxf.xml"/>  
  16.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>  
  17.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>  
  18.     <import resource="classpath:META-INF/cxf/cxf-extension-javascript-client.xml"/>                 
  19.       
  20.        
  21.     <bean id="userDaoService" class="cn.hk.dao.impl.IUserDaoImpl"></bean>  
  22.   
  23.     <bean id="customInFaultInterceptor" class="cn.hk.interceptor.CustomInFaultInterceptor">  
  24.         <constructor-arg value="send"/>  
  25.     </bean>  
  26.     <jaxws:server address="/users" serviceClass="cn.hk.dao.impl.IUserDaoImpl" id="userService"   >  
  27.             <jaxws:serviceBean >  
  28.                 <ref bean="userDaoService"/>  
  29.             </jaxws:serviceBean>  
  30.             <jaxws:inInterceptors>  
  31.                 <ref bean="customInFaultInterceptor"/>  
  32.             </jaxws:inInterceptors>  
  33.             <jaxws:inFaultInterceptors>  
  34.                 <ref bean="customInFaultInterceptor"/>  
  35.             </jaxws:inFaultInterceptors>  
  36.             <jaxws:outInterceptors>  
  37.                 <ref bean="customInFaultInterceptor"/>  
  38.             </jaxws:outInterceptors>  
  39.             <jaxws:outFaultInterceptors>  
  40.                 <ref bean="customInFaultInterceptor"/>  
  41.             </jaxws:outFaultInterceptors>  
  42.               
  43.     </jaxws:server>  
  44.       
  45. </beans>  

 6.web.xml

 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  3.     <context-param>  
  4.         <param-name>contextConfigLocation</param-name>  
  5.         <param-value>classpath:applicationContext.xml</param-value>  
  6.     </context-param>  
  7.     <listener>  
  8.         <listener-class>  
  9.             org.springframework.web.context.ContextLoaderListener  
  10.         </listener-class>  
  11.     </listener>     
  12.       
  13.     <servlet>  
  14.         <servlet-name>CXFServlet</servlet-name>  
  15.         <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
  16.         <load-on-startup>1</load-on-startup>  
  17.     </servlet>  
  18.     <servlet-mapping>  
  19.         <servlet-name>CXFServlet</servlet-name>  
  20.         <url-pattern>/services/*</url-pattern>  
  21.     </servlet-mapping>  
  22.     <display-name>cxfISpring</display-name>  
  23.     <welcome-file-list>  
  24.         <welcome-file>index.jsp</welcome-file>        
  25.     </welcome-file-list>  
  26. </web-app>  

 

 7.CustomInFaultInterceptor.java

 

Java代码  收藏代码
  1. package cn.hk.interceptor;  
  2.   
  3.   
  4. import org.apache.cxf.message.Message;  
  5. import org.apache.cxf.phase.AbstractPhaseInterceptor;  
  6.   
  7. public class CustomInFaultInterceptor extends AbstractPhaseInterceptor<Message> {  
  8.   
  9.     public CustomInFaultInterceptor(String phase){  
  10.           
  11.         super(phase);  
  12.     }  
  13.     @Override  
  14.     public void handleMessage(Message message)  {  
  15.           
  16.         System.out.println("getInMessage------------------"+message.getExchange().getInMessage());  
  17.         System.out.println("getInFaultMessage------------------"+message.getExchange().getInFaultMessage());  
  18.         System.out.println("getOutMessage------------------"+message.getExchange().getOutMessage());  
  19.         System.out.println("getOutFaultMessage------------------"+message.getExchange().getOutFaultMessage());  
  20.     }  
  21.   
  22. }  

 //客户端代码

1.UserBean.java

 

Java代码  收藏代码
  1. package cn.hk.bean;  
  2.   
  3. public class UserBean {  
  4.     private int id;  
  5.     private String userName;  
  6.     private String password;  
  7.     public UserBean(){  
  8.           
  9.           
  10.     }  
  11.     public UserBean(int id, String userName, String password) {  
  12.         super();  
  13.         this.id = id;  
  14.         this.userName = userName;  
  15.         this.password = password;  
  16.     }  
  17.     public int getId() {  
  18.         return id;  
  19.     }  
  20.     public void setId(int id) {  
  21.         this.id = id;  
  22.     }  
  23.     public String getUserName() {  
  24.         return userName;  
  25.     }  
  26.     public void setUserName(String userName) {  
  27.         this.userName = userName;  
  28.     }  
  29.     public String getPassword() {  
  30.         return password;  
  31.     }  
  32.     public void setPassword(String password) {  
  33.         this.password = password;  
  34.     }  
  35.     @Override  
  36.     public String toString() {  
  37.         return "UserBean [id=" + id + ", password=" + password + ", userName="  
  38.                 + userName + "]";  
  39.     }  
  40.       
  41.       
  42. }  

 2.IUserDao.java

 

Java代码  收藏代码
  1. package cn.hk.dao;  
  2.   
  3. import java.util.List;  
  4.   
  5. import javax.jws.WebParam;  
  6. import javax.jws.WebService;  
  7.   
  8.   
  9. import cn.hk.bean.UserBean;  
  10. @WebService  
  11. public interface IUserDao {  
  12.   
  13.     public UserBean getUser(@WebParam(name="id") String  id);  
  14.     public String getUserName(String id);  
  15.     public List<UserBean> getUserList();  
  16. }  

 3.InvokeUsersServices.java 调用

 

Java代码  收藏代码
  1. package cn.webservice.client;  
  2.   
  3. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
  4.   
  5. import cn.hk.dao.IUserDao;  
  6.   
  7. public class InvokeUsersServices {  
  8.       
  9.     public static void main(String[] args) {  
  10.           
  11.         JaxWsProxyFactoryBean factory=new JaxWsProxyFactoryBean();  
  12.         factory.setServiceClass(IUserDao.class);  
  13.           
  14.         factory.setAddress("http://localhost:8085/cxfISprint/services/userss");  
  15.         IUserDao userDao=(IUserDao)factory.create();  
  16.         System.out.println(userDao.getUser("1"));  
  17.         System.out.println(userDao.getUserName("12"));  
  18.         System.out.println(userDao.getUserList().size());  
  19.     }  
  20. }  

 

 

CXF整合spring之JaxWsProxyFactoryBean调用异常

1.

Exception in thread "main" java.lang.ClassCastException: org.apache.cxf.endpoint.ClientImpl cannot be cast to cn.hk.dao.IUserDao

at cn.webservice.client.InvokeUsersServices.main(InvokeUsersServices.java:13)

 

cause 是JaxWsProxyFactoryBean对象,不是JaxWsClientFactoryBean对象

2.

Exception in thread "main" javax.xml.ws.WebServiceException: Could not find wsdl:binding operation info for web method getUserName.

at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:113)

at $Proxy17.getUserName(Unknown Source)

at cn.webservice.client.InvokeUsersServices.main(InvokeUsersServices.java:14)

 

cause 客户端的接口也庶加@WebService注解

3.

object is not an instance of declaring class while invoking public java.lang.String cn.hk.dao.impl.IUserDaoImpl.getUserName(int) with params [10].

... 2 more

cause jaxws:serviceBean一定要引用接口的实现类

posted @ 2017-08-05 09:22  chenxiangxiang  阅读(4851)  评论(0编辑  收藏  举报