cxf開發webservice服務器+客戶端(各類類型的參數傳遞返回)

開發環境:eclipse3.7+jdk1.6.0_29+tomcat6.0.37html

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

源碼下載地址: http://pan.baidu.com/s/1qWEtePm web

Serverspring

1.新建一個接口類:UserServiceapache

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.接口實現類:UserServiceImpltomcat

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配置文件中配置cxfapp

<?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配置cxfeclipse

<?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?wsdljsp

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/666666list result:lisi/888888========================map==========================map resilt:zs========================array=========================array result:kangkang/123456array result:jane/123456

相關文章
相關標籤/搜索