java中使用axis发布和调用webService及dom4j解析xml字符串

工作中需要调用webService服务,这里记录一下如何在java中发布和调用webService。

需要的jar包:

webService服务端:

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService
public class MyWebService {

    //@WebMethod(exclude = true)注解用于在webService中排除某方法
    @WebMethod(exclude = true)
    public String hello(String name) {
        System.out.println("hello:" + name);
        return "hello:" + name;
    }

    public String transcoding(String fileName, String fileId, String userId, String filePath, String newPath) {
        System.out.println("transcoding...");
        System.out.println("fileName:" + fileName);
        System.out.println("fileId:" + fileId);
        System.out.println("userId:" + userId);
        System.out.println("filePath:" + filePath);
        System.out.println("newPath:" + newPath);

        String result = "";
        result += "<?xml version="1.0" encoding="UTF-8"?>";
        result += "<root>";
        result += "<hbzm>";
        result += "<FileID>" + fileId + "</FileID>";
        result += "<FileName>" + fileName + "</FileName>";
        result += "<userid>" + userId + "</userid>";
        result += "<filepath>" + filePath + "</filepath>";
        result += "<newpath>" + newPath + "</newpath>";
        result += "<result>ACK</result>";
        result += "</hbzm>";
        result += "</root>";

        return result;
    }

    public static void main(String[] args) {
        /**
         * 参数1:服务的发布地址 参数2:服务的实现者
         */
        Endpoint.publish("http://localhost:8081/upload/transcoding", new MyWebService());
        System.out.println("webservice start success");
    }
}

webService客户端:

import java.rmi.RemoteException;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class MyWebClient {
    public static void main(String[] args) {
        callTrans();
    }

    public static void callHello() {
        try {
            // new 一个服务
            Service sv = new Service();
            // 创建一个call对象
            Call call = (Call) sv.createCall();
            // 设置要调用的接口地址
            call.setTargetEndpointAddress("http://localhost:8081/upload/transcoding");
            // 设置要调用的接口方法
            call.setOperationName(new QName("http://webservice.xzh.com/", "hello"));
            // 设置参数,在设定参数时,不使用服务端定义的参数名,而是arg0~argN来定义
            call.addParameter("arg0", XMLType.XSD_STRING, ParameterMode.IN);
            // 返回参数类型
            call.setReturnType(XMLType.XSD_STRING);

            Object result = call.invoke(new Object[] { "jason" });
            System.out.println(result);
        }
        catch (ServiceException e) {
            e.printStackTrace();
        }
        catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public static void callTrans() {
        try {
            // new 一个服务
            Service sv = new Service();
            // 创建一个call对象
            Call call = (Call) sv.createCall();
            // 设置要调用的接口地址
            call.setTargetEndpointAddress("http://localhost:8081/upload/transcoding");
            // 设置要调用的接口方法
            call.setOperationName(new QName("http://webservice.xzh.com/", "transcoding"));
            // 设置参数,在设定参数时,不使用服务端定义的参数名,而是arg0~argN来定义
            call.addParameter("arg0", XMLType.XSD_STRING, ParameterMode.IN);
            call.addParameter("arg1", XMLType.XSD_STRING, ParameterMode.IN);
            call.addParameter("arg2", XMLType.XSD_STRING, ParameterMode.IN);
            call.addParameter("arg3", XMLType.XSD_STRING, ParameterMode.IN);
            call.addParameter("arg4", XMLType.XSD_STRING, ParameterMode.IN);
            call.setReturnType(XMLType.XSD_STRING);
            //发送转码请求
            String transResult = (String) call
                .invoke(new Object[] { "fileName", "fileIdTrans", "userId", "annexPathWithName", "basePath" });
            //解析转码接口返回的xml报文
            Document doc = DocumentHelper.parseText(transResult);
            Element root = doc.getRootElement();
            Element e = root.element("hbzm");
            System.out.println(e.elementText("result"));
            System.out.println(e.elementText("FileID"));
            System.out.println(e.elementText("FileName"));
            System.out.println(e.elementText("userid"));
            System.out.println(e.elementText("filepath"));
            System.out.println(e.elementText("newpath"));
        }
        catch (ServiceException e) {
            e.printStackTrace();
        }
        catch (RemoteException e) {
            e.printStackTrace();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

日志截图:

服务端:

客户端:

原文地址:https://www.cnblogs.com/Jason-Xiang/p/6832051.html