博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cxf开发webservice服务器+客户端(各种类型的参数传递返回)
阅读量:6258 次
发布时间:2019-06-22

本文共 6657 字,大约阅读时间需要 22 分钟。

转自:https://www.cnblogs.com/gavinYang/p/3551345.html

开发环境:eclipse3.7+jdk1.6.0_29+tomcat6.0.37

XFire搭建webservice: 

JAX-WS搭建webservice:

源码下载地址:  

Server

1.新建一个接口类:UserService

package com.gavin.service;import java.util.List;import javax.jws.WebParam;import javax.jws.WebService;@WebServicepublic interface UserService {    String getUserName(@WebParam(name="username")String username);        User getUser(@WebParam(name="user")User user);        List
getUserList(@WebParam(name="userList")List
userList); User getUserMap(@WebParam(name="userMap")User userMap); User[] getUserArray(@WebParam(name="userArray")User userArray);}

 

2.接口实现类:UserServiceImpl

package com.gavin.service;import java.util.List;public class UserServiceImpl implements UserService{    @Override    public String getUserName(String username) {        // TODO 自动生成的方法存根        return username;    }    @Override    public User getUser(User user) {        // TODO 自动生成的方法存根        return user;    }    @Override    public List
getUserList(List
userList) { // TODO 自动生成的方法存根 return userList; } @Override public User getUserMap(User userMap) { // TODO 自动生成的方法存根 return userMap; } @Override public User[] getUserArray(User userArray) { // TODO 自动生成的方法存根 return userArray.getUserArray(); }}

User对象

package com.gavin.service;import java.util.HashMap;public class User {    private String username;    private String userpass;    private HashMap userMap;    private User[] userArray;    public User() {    }        public User(HashMap userMap) {        this.userMap = userMap;    }    public User(User[] userArray) {        this.userArray = userArray;    }    public User(String name,String pass) {        this.username = name;        this.userpass = pass;    }        public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public String getUserpass() {        return userpass;    }    public void setUserpass(String userpass) {        this.userpass = userpass;    }    public HashMap getUserMap() {        return userMap;    }    public void setUserMap(HashMap userMap) {        this.userMap = userMap;    }    public User[] getUserArray() {        return userArray;    }    public void setUserArray(User[] userArray) {        this.userArray = userArray;    }    }

 

3.spring配置文件中配置cxf

4.在web.xml配置cxf

server
contextConfigLocation
/WEB-INF/spring.xml
org.springframework.web.context.ContextLoaderListener
CXFServlet
org.apache.cxf.transport.servlet.CXFServlet
CXFServlet
/services/*
index.jsp

做完上面的部分,server端就已经完成了,访问:

Client

1.必须将服务器端的User对象复制过来,才能进行对象传递返回,否则会出现类型转换异常

2.测试server

package com.gavin.cxf.test;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import org.apache.cxf.endpoint.Client;import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;import com.gavin.service.User;public class TestCXF {    public static void main(String[] args) {        System.out.println("=======================plain========================");        Object[] plainResult = callService("http://localhost:8080/server/services/getUserService?wsdl", "getUserName", "gavin");        System.out.println("plain result:"+plainResult[0]);        System.out.println("=======================object========================");        Object[] objectResult = callService("http://localhost:8080/server/services/getUserService?wsdl", "getUser", new User("gavin", "123456"));        User user = (User)objectResult[0];        System.out.println("object result:"+user.getUsername()+"/"+user.getUserpass());        System.out.println("========================list=========================");        List
list = new ArrayList
(); list.add(new User("zhangsan", "666666")); list.add(new User("lisi", "888888")); Object[] listResult = callService("http://localhost:8080/server/services/getUserService?wsdl", "getUserList", list); List
userList = (List) listResult[0]; for(User userInfo : userList){ System.out.println("list result:"+userInfo.getUsername()+"/"+userInfo.getUserpass()); } //map,array对象必须包在一个对象下面 System.out.println("========================map=========================="); HashMap map = new HashMap(); map.put("name", "zs"); //如果想在map中传递对象则需写成HashMap
map = new HashMap
(); //map.put("user", new User("wangwu", "123456")); Object[] mapResult = callService("http://localhost:8080/server/services/getUserService?wsdl", "getUserMap", new User(map)); User userMap = (User) mapResult[0]; System.out.println("map resilt:"+userMap.getUserMap().get("name")); System.out.println("========================array========================="); Object[] arrayResult = callService("http://localhost:8080/server/services/getUserService?wsdl", "getUserMap", new User(new User[]{new User("kangkang","123456"),new User("jane","123456")})); User userArray = (User) arrayResult[0]; for(User userInfo : userArray.getUserArray()){ System.out.println("array result:"+userInfo.getUsername()+"/"+userInfo.getUserpass()); } } public static Object[] callService(String wsUrl, String method, Object...arg) { JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); Client client = dcf.createClient(wsUrl); Object[] res = null; try { res = client.invoke(method, arg); } catch (Exception e) { e.printStackTrace(); } return res; } }

打印结果显示:

=======================plain========================
plain result:gavin
=======================object========================
object result:gavin/123456
========================list=========================
list result:zhangsan/666666
list result:lisi/888888
========================map==========================
map resilt:zs
========================array=========================
array result:kangkang/123456
array result:jane/123456

你可能感兴趣的文章
datetime.timedelta类
查看>>
SQL Server,MySQL,Oracle三者的区别
查看>>
[K/3Cloud] 在设计时复制已有表单菜单或菜单项快速建立菜单
查看>>
矩阵快速幂总结
查看>>
[spring] Ioc 基础
查看>>
关于DataTables一些小结
查看>>
win7(windows 7)系统下安装SQL2005(SQL Server 2005)图文教程
查看>>
Hibernate的基本配置
查看>>
Python 3.5 安装geohash库后import geohash失败
查看>>
总结100个英文邮件常用例句让你写作无忧
查看>>
css3--之backface-visibility
查看>>
软件需求分析之猫咪记单词
查看>>
good vs evil
查看>>
算法28-----范围求和
查看>>
基于V4L2的视频驱动开发(1)
查看>>
zoj 1008
查看>>
VC++ CArchive及简单的文件操作方法
查看>>
使用canvas制作一个移动端画板
查看>>
android中ListView数据混乱问题
查看>>
QT学习-10/31/2012
查看>>