我是靠谱客的博主 殷勤钢笔,最近开发中收集的这篇文章主要介绍java访问WebService接口Soap,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

参考地址:https://www.cnblogs.com/garfieldcgf/p/5966317.html

相关资料地址推荐:
https://blog.csdn.net/myblogzz/article/details/80311038

本文介绍两种方式:
1.适合发送单个参数
2.适合发送很多的list,复杂的数据,但是会有点夸张,各位往下看就知道了
3.文章最末尾有完整的import和Maven

方式一:

在这里插入图片描述

@CrossOrigin(origins = "*", maxAge = 3600)
    @PostMapping(value = "/select")
    public Object[] 方法名(String ropeModel) {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://你的地址?wsdl");
        QName name = new QName("这里好像是什么固定的地址,问对方要", "方法名");
        Object[] objects = new Object[0];
        try {
            objects = client.invoke(name, ropeModel);
            return objects;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return objects;
    }

方式二:原理就是把完整的xml变成字符串发过去

好处:直接发送,容易看懂
坏处:字符串太多了,显得很多
注意看完:推荐一个软件,很牛逼,根据webService的地址就能显示所有的接口,并生成相应的xml模板,这样你就不用懂xml了
1.先分享软件。2.贴上java代码。3.截图使用软件获取xml格式

软件名:SoapUI5.6.0
链接:https://pan.baidu.com/s/1Aibfpd1bVoUF_rbsQ8hXig
提取码:ks4b

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by garfield on 2016/10/12.
 */
public class TestWebService {
    public static void main(String[] args) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        //拼接xml请求,带有请求头
        String params = "<id>5</id>";//随手举个例子,类似...
        String soapRequestData = "<soapenv:Envelope n" +
                "txmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" n" +
                "txmlns:ser="http://service.resource.ws.bd.newland.com/">n" +
                "   <soapenv:Header>n" +
                "t<serviceCode>serviceCode</serviceCode>n" +
                "t<userName>userName</userName>n" +
                "t<authCode>authCode</authCode>n" +
                "   </soapenv:Header>n" +
                "   <soapenv:Body>n" +
                "      <ser:function>n" +
                params +
                "      </ser:function>n" +
                "   </soapenv:Body>n" +
                "</soapenv:Envelope>n";

        try {
            String method = "请求地址";//比如http://192.177.222.222:8888/services/Service_Name/Function_Name
            PostMethod postMethod = new PostMethod(method);
            byte[] b = soapRequestData.getBytes("utf-8");
            InputStream is = new ByteArrayInputStream(b, 0, b.length);
            RequestEntity re = new InputStreamRequestEntity(is, b.length, "application/soap+xml; charset=utf-8");
            postMethod.setRequestEntity(re);

            HttpClient httpClient = new HttpClient();
            int statusCode = httpClient.executeMethod(postMethod);
            //200说明正常返回数据
            if (statusCode != 200) {
                //internet error
                System.out.println(statusCode);
            }
            soapRequestData = postMethod.getResponseBodyAsString();
            System.out.println(soapRequestData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

软件的使用
请先下载安装SoapUI
然后打开按照下面截图步骤
在这里插入图片描述

在这里插入图片描述

完整import


import cn.hutool.core.util.XmlUtil;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.lang.StringUtils;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.json.JSONException;
import org.junit.Test;
import org.lastools.LASHeader;
import org.lastools.LASPoint;
import org.lastools.LASReader;
import org.lastools.LASlibJNI;
import org.springframework.web.bind.annotation.*;

import javax.xml.namespace.QName;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.lang.Math.*;
<dependencies>
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>4.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.6.3</version>
        </dependency>
        <dependency>
            <groupId>javax.xml.rpc</groupId>
            <artifactId>javax.xml.rpc-api</artifactId>
            <version>1.1.2</version>
        </dependency>


        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>com.fasterxml.jackson.core</groupId>-->
<!--            <artifactId>jackson-core</artifactId>-->
<!--            <version>2.8.5</version>-->
<!--        </dependency>-->

<!--        <dependency>-->
<!--            <groupId>com.fasterxml.jackson.core</groupId>-->
<!--            <artifactId>jackson-databind</artifactId>-->
<!--            <version>2.8.5</version>-->
<!--        </dependency>-->

        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
            <version>3.1.1</version>
        </dependency>
        <!-- Jetty is needed if you're are not using the CXFServlet -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.vaadin.external.google</groupId>
            <artifactId>android-json</artifactId>
            <version>0.0.20131108.vaadin1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>net.minidev</groupId>
            <artifactId>json-smart</artifactId>
        </dependency>
        <dependency>
            <groupId>cn.torhang.util</groupId>
            <artifactId>axis</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>cn.torhang.util</groupId>
            <artifactId>jaxrpc</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.ws.commons.axiom</groupId>
            <artifactId>axiom-api</artifactId>
            <version>1.2.20</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>fakepath</groupId>
            <artifactId>laslibjni</artifactId>
            <version>0.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20180130</version>
        </dependency>
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.jdom</groupId>
            <artifactId>jdom</artifactId>
            <version>1.1</version>
        </dependency>
    </dependencies>

最后

以上就是殷勤钢笔为你收集整理的java访问WebService接口Soap的全部内容,希望文章能够帮你解决java访问WebService接口Soap所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部