CXF整合Spring之JaxWsProxyFactoryBean调用
1.见解
1.1 客户端的接口代码还一定要和服务端的接口代码一样,连注解都要一样,不够灵活
1.2 当客户端访问服务器的请求地址时,如果服务端没有对应的地址,就会报错,但是又没有cxf的异常捕获处理
1.3 address中的url是模糊匹配的
//服务端代码
2.UserBean.java
- package cn.hk.bean;
- public class UserBean {
- private int id;
- private String userName;
- private String password;
- public UserBean(){
- }
- public UserBean(int id, String userName, String password) {
- super();
- this.id = id;
- this.userName = userName;
- this.password = password;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- @Override
- public String toString() {
- return "UserBean [id=" + id + ", password=" + password + ", userName="
- + userName + "]";
- }
- }
3.IUserDao.java
- package cn.hk.dao;
- import java.util.List;
- import javax.jws.WebParam;
- import javax.jws.WebService;
- import cn.hk.bean.UserBean;
- @WebService
- public interface IUserDao {
- public UserBean getUser(@WebParam(name="id") int id);
- public String getUserName(int id);
- public List<UserBean> getUserList();
- }
4.IUserDaoImpl.java
- package cn.hk.dao.impl;
- import java.util.ArrayList;
- import java.util.List;
- import javax.jws.WebParam;
- import javax.jws.WebService;
- import cn.hk.bean.UserBean;
- import cn.hk.dao.IUserDao;
- @WebService
- public class IUserDaoImpl implements IUserDao {
- @Override
- public UserBean getUser(@WebParam(name="id") int id) {
- return new UserBean(id,"zhangsan","zhangsan");
- }
- @Override
- public List<UserBean> getUserList() {
- List<UserBean> list=new ArrayList<UserBean>();
- UserBean u1=new UserBean(1,"lisi","lisi");
- list.add(u1);
- u1=new UserBean(2,"wanwu","wanwu");
- list.add(u1);
- u1=new UserBean(3,"tiantian","tiantian");
- list.add(u1);
- return list;
- }
- @Override
- public String getUserName(int id) {
- return "id:"+id+"name:zhangsan";
- }
- }
5.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:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- 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://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop.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-servlet.xml"/>
- <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
- <import resource="classpath:META-INF/cxf/cxf-extension-javascript-client.xml"/>
- <bean id="userDaoService" class="cn.hk.dao.impl.IUserDaoImpl"></bean>
- <bean id="customInFaultInterceptor" class="cn.hk.interceptor.CustomInFaultInterceptor">
- <constructor-arg value="send"/>
- </bean>
- <jaxws:server address="/users" serviceClass="cn.hk.dao.impl.IUserDaoImpl" id="userService" >
- <jaxws:serviceBean >
- <ref bean="userDaoService"/>
- </jaxws:serviceBean>
- <jaxws:inInterceptors>
- <ref bean="customInFaultInterceptor"/>
- </jaxws:inInterceptors>
- <jaxws:inFaultInterceptors>
- <ref bean="customInFaultInterceptor"/>
- </jaxws:inFaultInterceptors>
- <jaxws:outInterceptors>
- <ref bean="customInFaultInterceptor"/>
- </jaxws:outInterceptors>
- <jaxws:outFaultInterceptors>
- <ref bean="customInFaultInterceptor"/>
- </jaxws:outFaultInterceptors>
- </jaxws:server>
- </beans>
6.web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <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">
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath: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>/services/*</url-pattern>
- </servlet-mapping>
- <display-name>cxfISpring</display-name>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
7.CustomInFaultInterceptor.java
- package cn.hk.interceptor;
- import org.apache.cxf.message.Message;
- import org.apache.cxf.phase.AbstractPhaseInterceptor;
- public class CustomInFaultInterceptor extends AbstractPhaseInterceptor<Message> {
- public CustomInFaultInterceptor(String phase){
- super(phase);
- }
- @Override
- public void handleMessage(Message message) {
- System.out.println("getInMessage------------------"+message.getExchange().getInMessage());
- System.out.println("getInFaultMessage------------------"+message.getExchange().getInFaultMessage());
- System.out.println("getOutMessage------------------"+message.getExchange().getOutMessage());
- System.out.println("getOutFaultMessage------------------"+message.getExchange().getOutFaultMessage());
- }
- }
//客户端代码
1.UserBean.java
- package cn.hk.bean;
- public class UserBean {
- private int id;
- private String userName;
- private String password;
- public UserBean(){
- }
- public UserBean(int id, String userName, String password) {
- super();
- this.id = id;
- this.userName = userName;
- this.password = password;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- @Override
- public String toString() {
- return "UserBean [id=" + id + ", password=" + password + ", userName="
- + userName + "]";
- }
- }
2.IUserDao.java
- package cn.hk.dao;
- import java.util.List;
- import javax.jws.WebParam;
- import javax.jws.WebService;
- import cn.hk.bean.UserBean;
- @WebService
- public interface IUserDao {
- public UserBean getUser(@WebParam(name="id") String id);
- public String getUserName(String id);
- public List<UserBean> getUserList();
- }
3.InvokeUsersServices.java 调用
- package cn.webservice.client;
- import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
- import cn.hk.dao.IUserDao;
- public class InvokeUsersServices {
- public static void main(String[] args) {
- JaxWsProxyFactoryBean factory=new JaxWsProxyFactoryBean();
- factory.setServiceClass(IUserDao.class);
- factory.setAddress("http://localhost:8085/cxfISprint/services/userss");
- IUserDao userDao=(IUserDao)factory.create();
- System.out.println(userDao.getUser("1"));
- System.out.println(userDao.getUserName("12"));
- System.out.println(userDao.getUserList().size());
- }
- }
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一定要引用接口的实现类
//需要的依赖jar