Java三种方式实现发送xml参数的WebService接口调用

项目开发中与第三方系统数据对接遇到的问题,仅用作记录。

1.使用cxf调用(联调时没有收到响应信息)

JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
logger.info(JSON.toJSON(todoInfo));
Client client = clientFactory.createClient("http://******************?wsdl");

String[] result = (String[]) client.invoke("ummWaitMessageAdd", todoInfo.get("sysno"), todoInfo.get("iccode"), todoInfo.get("msgno"), todoInfo.get("pkno")
        , todoInfo.get("gno"), todoInfo.get("title"), todoInfo.get("type"), todoInfo.get("url"), todoInfo.get("info"), todoInfo.get("prior")
        , todoInfo.get("flow"), todoInfo.get("create"), todoInfo.get("create"));
logger.info(result.toString());
String resultStr = result[0];
return resultStr;

相关jar包,也可点击下载

pom.xml

2.7.12

    org.apache.cxf
    cxf-rt-core
    ${cxf.version}


    org.apache.cxf
    cxf-rt-frontend-simple
    ${cxf.version}


    org.apache.cxf
    cxf-rt-frontend-jaxws
    ${cxf.version}


    org.apache.cxf
    cxf-rt-databinding-aegis
    ${cxf.version}


    org.apache.cxf
    cxf-rt-transports-local
    ${cxf.version}


    org.apache.cxf
    cxf-rt-transports-http
    ${cxf.version}


    org.apache.cxf
    cxf-rt-transports-http-jetty
    ${cxf.version}


    org.apache.cxf
    cxf-rt-transports-jms
    ${cxf.version}


    org.apache.cxf
    cxf-rt-management
    ${cxf.version}


    org.apache.cxf
    cxf-common-utilities
    2.5.10

2.使用axis方式调用(这种方式可以收到服务端返回信息,不过一直报错)

            org.apache.axis.client.Service service = new org.apache.axis.client.Service();
            Call call = (Call) service.createCall();
            String endpoint = "http://*****************************?wsdl";
            String operationName = "ummWaitMessageAdd";
            call.setTargetEndpointAddress(new URL(endpoint));
            call.setOperationName(new QName(targetNameSpace, operationName));
            call.addParameter("sysno", Constants.XSD_STRING, ParameterMode.IN);
            call.addParameter("iccode", Constants.XSD_STRING, ParameterMode.IN);
            call.addParameter("msgno", Constants.XSD_STRING, ParameterMode.IN);
            call.addParameter("pkno", Constants.XSD_STRING, ParameterMode.IN);
            call.addParameter("gno", Constants.XSD_STRING, ParameterMode.IN);
            call.addParameter("title", Constants.XSD_STRING, ParameterMode.IN);
            call.addParameter("type", Constants.XSD_STRING, ParameterMode.IN);
            call.addParameter("url", Constants.XSD_STRING, ParameterMode.IN);
            call.addParameter("info", Constants.XSD_STRING, ParameterMode.IN);
            call.addParameter("prior", Constants.XSD_STRING, ParameterMode.IN);
            call.addParameter("flow", Constants.XSD_STRING, ParameterMode.IN);
            call.addParameter("create", Constants.XSD_STRING, ParameterMode.IN);
            call.addParameter("update", Constants.XSD_STRING, ParameterMode.IN);
            //设置返回的类型
            call.setReturnType(Constants.XSD_STRING);
            // 这里的obj{}是放入几个入参,完全由service提供的接口方法的入参决定,且顺序和你存放的顺序一致!一般入参为String类型的xml报文,回参也是xml报文。
            Object[] obj = new Object[] { todoInfo.get("sysno")+"", todoInfo.get("iccode"), todoInfo.get("msgno"), todoInfo.get("pkno")
                    , todoInfo.get("gno"), todoInfo.get("title"), todoInfo.get("type"), todoInfo.get("url"), todoInfo.get("info"), todoInfo.get("prior")
                    , todoInfo.get("flow"), todoInfo.get("create")+"", todoInfo.get("create")+"" };
            String result = (String) call.invoke(obj);
            return result;

相关jar包,也可点击下载

pom.xml


    org.apache.axis
    axis
    1.4


    org.apache.axis
    axis-jaxrpc
    1.4


    axis
    axis-wsdl4j
    1.5.1


    commons-discovery
    commons-discovery
    0.2


    javax.mail
    mail
    1.4.7

3.通过http post方式(最后就是通过这种方式实现接口...)

先将请求参数封装在xml中,在发送http请求

/**
 * 生成请求xml数据
 * @param methodName 方法名 本例为"ummWaitMessageAdd"
 * @param todoInfo 数据 (key为wsdl文件中参数的name值注意大小写和顺序都要保持一致,value为实际值)
 * @return
 */
private String makeXml(String methodName ,Map todoInfo) {
    logger.info("=======生成xml======");
    StringBuffer sb = new StringBuffer();
    sb.append("\n" +
            "\n" +
            "  \n" +
            "    <" + methodName + " xmlns=\"WSCenter\">\n" );
    for (String key : todoInfo.keySet()) {
        if (DataUtil.checkMapcontainsKey(todoInfo, key)) {
            sb.append("<" + key + ">");
            sb.append(todoInfo.get(key).toString().replaceAll("&", "&"));//特殊字符需要转换
            sb.append("");
        } else {
            sb.append("<" + key + "/>");
        }
        sb.append("\r\n");
    }
    sb.append( "    \n" +
            "  \n" +
            "");
    logger.info("=======生成xml结束======");
    return sb.toString();
}
发送请求代码
URL url = new URL("http://*****************************?wsdl");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setRequestMethod("POST");
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.write(params.getBytes("utf-8"));//params就是上面生成的xml内容
dos.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
String line = null;
StringBuffer strBuf = new StringBuffer();
while ((line = reader.readLine()) != null) {
    strBuf.append(line);
}
dos.close();
reader.close();
String rs = strBuf.toString();

webservice服务端wsdl文件(服务端是用.net实现)

Java三种方式实现发送xml参数的WebService接口调用_第1张图片

你可能感兴趣的:(java)