cxf开发webservice服务器+客户端(各种类型的参数传递返回)

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

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

XFire搭建webservice: http://www.cnblogs.com/gavinYang/p/3525339.html
JAX-WS搭建webservice:http://www.cnblogs.com/gavinYang/p/3525287.html

源码下载地址: http://pan.baidu.com/s/1qWEtePm 

Server

1.新建一个接口类:UserService

复制代码
package com.gavin.service;

import java.util.List;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public 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端就已经完成了,访问:http://localhost:8080/server/services/getUserService?wsdl

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

你可能感兴趣的:(cxf开发webservice服务器+客户端(各种类型的参数传递返回))