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();
        }
    }
}

 

 

日志截圖:

服務端:

客戶端:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM