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一定要引用接口的实现类
CXF整合Spring之JaxWsProxyFactoryBean调用的更多相关文章
- WebService—CXF整合Spring实现接口发布和调用过程
一.CXF整合Spring实现接口发布 发布过程如下: 1.引入jar包(基于maven管理) <!-- cxf --> <dependency> <groupId> ...
- 【WebService】——CXF整合Spring
相关博客: [WebService]--入门实例 [WebService]--SOAP.WSDL和UDDI 前言: 之前的几篇博客基本上都是使用jdk来实现WebService的调用,没有使用任何框架 ...
- webservice 服务端例子+客户端例子+CXF整合spring服务端测试+生成wsdl文件 +cxf客户端代码自动生成
首先到CXF官网及spring官网下载相关jar架包,这个不多说.webservice是干嘛用的也不多说. 入门例子 模拟新增一个用户,并返回新增结果,成功还是失败. 大概的目录如上,很简单. Res ...
- 【Java EE 学习 81】【CXF框架】【CXF整合Spring】
一.CXF简介 CXF是Apache公司下的项目,CXF=Celtix+Xfire:它支持soap1.1.soap1.2,而且能够和spring进行快速无缝整合. 另外jax-ws是Sun公司发布的一 ...
- CXF整合Spring开发WebService
刚开始学webservice时就听说了cxf,一直没有尝试过,这两天试了一下,还不错,总结如下: 要使用cxf当然是要先去apache下载cxf,下载完成之后,先要配置环境变量,有以下三步: 1.打开 ...
- cxf整合spring错误为:cvc-complex-type.2.4.c
cxf整合spring,报错信息如下: Multiple annotations found at this line:- cvc-complex-type.2.4.c: The matching w ...
- cxf整合spring中出现的错误
Caused by: java.lang.ClassNotFoundException: javax.wsdl.extensions.ElementExtensible at org.apache.c ...
- CXF整合Spring发布WebService实例
一.说明: 上一篇简单介绍了CXF以及如何使用CXF来发布一个简单的WebService服务,并且介绍了客户端的调用. 这一篇介绍如何使用CXF与spring在Web项目中来发布WebService服 ...
- CXF整合spring
近公司需要弄webservics,还说不用框架整合(提倡使用hessian,他们既然说与操作系统有兼容问题,由于人员单薄,不得不屈服,哎),我想了老半天没弄明白他说的不用框架整合spring,尝试过直 ...
随机推荐
- UIView总结
转自:http://langhua9527.iteye.com/blog/1377741 如果想调用某个类的某个方法可以写成这样,这个方法来自NSObject类 performSelector: pe ...
- swift 函数.和匿名函数
函数 注意: 没有定义返回类型的函数会返回特殊的值,叫 Void.它其实是一个空的元组(tuple),没有任何元素,可以写成(). 使用元组作为返回参数,返回多个参数 func count(strin ...
- JqERY
//下拉菜单样式 /*查找全部select的下拉菜单*/ function getElemsById(cot_val){ return document.getElementById(cot_val) ...
- 0069 如何在Intellij IDEA中查看Spring MVC项目的所有请求与处理方法的映射列表
@RequestMapping注解分散于Controller中,如何在一个列表中看到所有的请求呢,比如下面这样: File-->Project Structure-->Modules--& ...
- try与catch
首先要清楚,如果没有try的话,出现异常会导致程序崩溃.而try则可以保证程序的正常运行下去,比如说:try{int i = 1/0;}catch(Exception e){........}一个计算 ...
- Android App性能測试
一.内存 1.查看单个应用App最大内存限制 Command:adb shell "getprop|grep heapgrowthlimit" C:\Users\hujiachun ...
- linux - camera capture
//cut a picture#include <stdio.h>#include <stdlib.h>#include <string.h>#include &l ...
- rsync详解之exclude排除文件
rsync详解之exclude排除文件 问题:如何避开同步指定的文件夹? --excludersync --exclude files and folders http://articles.sl ...
- elasticsearch安装与使用(1)-- centos7 elasticsearch的两种简单安装方法
转自:http://www.cnblogs.com/miao-zp/p/6003160.html 简单修改 前言 elasticsearch(下面称为ES)是一个基于Lucene的搜索服务器(By 百 ...
- 标签响应javascript的href处理[转载]
为了给一个<a />标签绑定javascript,但又不让它跳转链接,大家习惯上用的都是 <a href="javascript:;" onclick=" ...