欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-06-08 12:18:00
...

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

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

<cxf.version>2.7.12</cxf.version>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-core</artifactId>
    <version>${cxf.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-simple</artifactId>
    <version>${cxf.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>${cxf.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-databinding-aegis</artifactId>
    <version>${cxf.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-local</artifactId>
    <version>${cxf.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>${cxf.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http-jetty</artifactId>
    <version>${cxf.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-jms</artifactId>
    <version>${cxf.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-management</artifactId>
    <version>${cxf.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-common-utilities</artifactId>
    <version>2.5.10</version>
</dependency>

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

<dependency>
    <groupId>org.apache.axis</groupId>
    <artifactId>axis</artifactId>
    <version>1.4</version>
</dependency>
<dependency>
    <groupId>org.apache.axis</groupId>
    <artifactId>axis-jaxrpc</artifactId>
    <version>1.4</version>
</dependency>
<dependency>
    <groupId>axis</groupId>
    <artifactId>axis-wsdl4j</artifactId>
    <version>1.5.1</version>
</dependency>
<dependency>
    <groupId>commons-discovery</groupId>
    <artifactId>commons-discovery</artifactId>
    <version>0.2</version>
</dependency>
<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.7</version>
</dependency>

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

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

/**
 * 生成请求xml数据
 * @param methodName 方法名 本例为"ummWaitMessageAdd"
 * @param todoInfo 数据 (key为wsdl文件中参数的name值注意大小写和顺序都要保持一致,value为实际值)
 * @return
 */
private String makeXml(String methodName ,Map<String, Object> todoInfo) {
    logger.info("=======生成xml======");
    StringBuffer sb = new StringBuffer();
    sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
            "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
            "  <soap:Body>\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("&", "&amp;"));//特殊字符需要转换
            sb.append("</" + key + ">");
        } else {
            sb.append("<" + key + "/>");
        }
        sb.append("\r\n");
    }
    sb.append( "    </" + methodName + ">\n" +
            "  </soap:Body>\n" +
            "</soap:Envelope>");
    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接口调用

相关标签: webservice java