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<User> getUserList(@WebParam(name="userList")List<User> 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<User> getUserList(List<User> 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

复制代码
<?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:jaxws="http://cxf.apache.org/jaxws"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

    <bean id="userServiceImpl" class="com.gavin.service.UserServiceImpl"></bean>
    
    <jaxws:endpoint id="userService" address="/getUserService" implementor="#userServiceImpl"></jaxws:endpoint>

</beans>
复制代码

4.在web.xml配置cxf

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>server</display-name>
  
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring.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>
  </servlet>
  <servlet-mapping>
      <servlet-name>CXFServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
复制代码

做完上面的部分,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<User> list = new ArrayList<User>();
        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<User> 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<String,User> map = new HashMap<String,User>();
        //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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值