WebService服务端客户端开发(CXF和AXIS)

服务端:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>springcxfwss</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <!-- 设置Spring容器加载配置文件路径 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/config/*.xml
        </param-value>
    </context-param>

    <!-- 加载Spring容器配置 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <servlet>
        <servlet-name>CXFService</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>  
    </servlet>
    <servlet-mapping>
        <servlet-name>CXFService</servlet-name>
        <url-pattern>/webservice/*</url-pattern>
    </servlet-mapping>
    
</web-app>

 

applicationContext-server.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

    <jaxws:endpoint id="userService"
        implementor="com.ufgov.webservice.service.UserServiceImpl" address="/userWS">
    </jaxws:endpoint>
    
    <!-- cxf3以后,只需要引入这个配置文件即可,其他两个废弃掉了 -->
    <import resource="classpath:META-INF/cxf/cxf.xml" />
    
</beans>  

 

package com.ufgov.webservice.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.jws.WebMethod;
import javax.jws.WebService;

import com.ufgov.webservice.bean.Student;
import com.ufgov.webservice.bean.UserDemo;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@WebService
public class UserServiceImpl {

    @WebMethod(exclude = true) // 不会被发布出去
    public String testA(String teString) {
        return "testA:" + teString;
    }

    public List<String> getAllListString() {
        /*
         * for (Entry<String, Object> entry : p.entrySet()) {
         * System.out.println("Key: " + entry.getKey() + " Value: " +
         * entry.getValue()); }
         */
        List<String> list = new ArrayList<String>();
        list.add("0001");
        list.add("0002");
        list.add("0003");
        return list;
    }

//exclude=true该方法不会被发布出去

    @WebMethod(exclude=true)
    public HashMap<String, Object> getHashMap() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("name", "测试服务器返回Map");
        map.put("user", new UserDemo("000", "测试服务器返回Map", 3));
        return map;
    }

}

 

客户端(CXF)

public class CxfClientTest {

    final static String wsUrl = "http://localhost:8080/wss/webservice/userWS?wsdl";
    final static String endpoint = "http://localhost:8080/wss/webservice/userWS";
    final static String namespaceURI = "http://service.webservice.ufgov.com/";

    public Object[] callService(String wsUrl, String method, Object... arg) {
        Object[] result = null;
        try {
            JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
            Client client = clientFactory.createClient(wsUrl);
            result = client.invoke(method, arg);
            return result;
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return result;
    }

public static void main(String[] args) throws Exception {

Object[] result1 = test.callService(wsUrl, "testA", "返回字符串测试");
//        System.out.println("***************testA********************************");
//        System.out.println("testA: " + result1[0].toString());

}

 

    // 解析返回的Map数据,参数必须为Hashmap
    public static void mapMethod(Object obj)
            throws IllegalArgumentException, IllegalAccessException, IntrospectionException, InvocationTargetException {
        StringBuilder sBuilder = new StringBuilder();
        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            String key = property.getName();
            if (key.compareToIgnoreCase("class") == 0) {

            }
            Method getter = property.getReadMethod();
            Object value = getter != null ? getter.invoke(obj) : null;
            // map.put(key, value);
            sBuilder.append(key + " : " + value + " ");
        }
    }

 

    // 反射获取类
    public static void reflectMethod(Object obj) throws IllegalArgumentException, IllegalAccessException {
        // 根据反射获取对象的值
        StringBuilder sBuilder = new StringBuilder();
        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field f : fields) {
            f.setAccessible(true); // 设置些属性是可以访问的
            Object val = new Object();
            val = f.get(obj);
            sBuilder.append(f.getName() + " : " + val + " ");
        }

        System.out.println(sBuilder.toString());
    }

 

客户端(AXIS)

public void getHashMap() {
        try {
            // 调用过程
            Service service = new Service();
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(new java.net.URL(endpoint));
            QName qn=new QName(namespaceURI, "getHashMap");
            call.setOperationName(qn);// WSDL里面描述的操作名称
            
            // 注册序列化和反序列化类
            call.registerTypeMapping(UserDemo.class, qn,
            new BeanSerializerFactory(UserDemo.class, qn),
            new BeanDeserializerFactory(UserDemo.class, qn));
            // 设置返回类型
            call.setReturnType(qn, UserDemo[].class);
            
//            call.setReturnType(XMLType.XSD_ENTITY);// 设置返回类型 字符串、int等类型
            call.setUseSOAPAction(true);

            Object[] obj = new Object[] {};
            Object result = call.invoke(obj);
            @SuppressWarnings("unchecked")
            HashMap<String, Object> res =  (HashMap<String, Object>)result;
            System.out.println("******** getAllUsers ********");
            for (Object key : res.entrySet()) {
                System.out.println(key.toString() + res.get(key));
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值