springboot调用webservice(asmx后缀)接口,返回值是xml格式的,使用dom4j进行解析

webservice调用

导入依赖

		<dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.13</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>log4j-over-slf4j</artifactId>
            <version>1.7.25</version>
        </dependency>
		<dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>
        

编写httpclient工具类

package com.ghrc.demo.user;

import java.nio.charset.Charset;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

public class HttpClientCallSoapUtil {
    static int socketTimeout = 30000;// 请求超时时间
    static int connectTimeout = 30000;// 传输超时时间
    static Logger logger = Logger.getLogger(HttpClientCallSoapUtil.class);

    /**
     * 使用SOAP1.1发送消息
     *
     * @param postUrl
     * @param soapXml
     * @param soapAction
     * @return
     */
    public  String doPostSoap1_1(String postUrl, String soapXml,
                                       String soapAction) {
        String retStr = "";
        // 创建HttpClientBuilder
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        // HttpClient
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
        HttpPost httpPost = new HttpPost(postUrl);
        //  设置请求和传输超时时间
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(socketTimeout)
                .setConnectTimeout(connectTimeout).build();
        httpPost.setConfig(requestConfig);
        try {
//设置请求头部信息
            httpPost.setHeader("Content-Type", "text/xml; charset=utf-8");
//            httpPost.setHeader("X-Powered-By", "ASP.NET");

            StringEntity data = new StringEntity(soapXml,
                    Charset.forName("UTF-8"));
            httpPost.setEntity(data);
            CloseableHttpResponse response = closeableHttpClient
                    .execute(httpPost);
            HttpEntity httpEntity = response.getEntity();
            if (httpEntity != null) {
                // 打印响应内容
                retStr = EntityUtils.toString(httpEntity, "UTF-8");
                logger.info("response:" + retStr);
            }
            // 释放资源
            closeableHttpClient.close();
        } catch (Exception e) {
            logger.error("exception in doPostSoap1_1", e);
        }
        return retStr;
    }
}

编写测试类

 @Test
    public void sad() throws Exception {
        String orderSoapXml = "<soap:Envelope xmlns:xsi=\"http://xxxxxxx\" xmlns:xsd=\"http://xxxx\" xmlns:soap=\"http://schemas.xmlsoap.xxxxxxx/\">\n" +
                "  <soap:Body>\n" +
                "    <GetAllUserList xmlns=\"http://xxxxx.org/\" />\n" +
                "  </soap:Body>\n" +
                "</soap:Envelope>";
        String postUrl = "http://xxxxxxxxx/xxxxxxx.asmx?op=GetAllUserList";
        HttpClientCallSoapUtil callSoapUtil = new HttpClientCallSoapUtil();
        String s = callSoapUtil.doPostSoap1_1(postUrl, orderSoapXml, "");

        System.out.println(s);

这些信息是从那个接口获得的,推荐用这个工具访问给你暴露的地址 工具soapui
在这里插入图片描述
把用工具得到的信息放在orderSoapXml里面
String s 就是得到的就是接口返回的xml格式的内容

使用dom4j解析这个xml格式的字符串

dom4j的依赖已经导入maven里面了
根据xml格式的解析就好了


        SAXReader saxReader = new SAXReader();
        Document read = saxReader.read(new ByteArrayInputStream(s.getBytes("UTF-8")));
       //获取根节点
        Element rootElement = read.getRootElement();
//        获取子节点
        Element body = rootElement.element("Body");
        Element getAllUserListResponse = body.element("GetAllUserListResponse");
        Element userList = getAllUserListResponse.element("GetAllUserListResult");
        Element userEntity = userList.element("UserEntity");


        List<Element> elements = userList.elements();
        for (int i = 0; i < elements.size(); i++) {
            Element element = elements.get(i);
            String userid = element.elementText("USERID");

            String loginname = element.elementText("LOGINNAME");

            String username = element.elementText("USERNAME");

            String functional = element.elementText("FUNCTIONAL");

            String company = element.elementText("COMPANY");

            String departmentid = element.elementText("DEPARTMENTID");

            String title = element.elementText("TITLE");

            String department = element.elementText("DEPARTMENT");
            }

之后就把xml里的信息根据属性值就把这个取出来了
完工!!!

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Spring Boot中调用Web Service接口,并且数据格式为JSON格式,可以按照以下步骤进行操作: 1. 添加相关依赖:在pom.xml文件中添加以下依赖,以支持Web Service和JSON的相关功能。 ```xml <dependencies> <!-- Spring Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Web Services --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> <!-- Jackson JSON Processor --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> </dependencies> ``` 2. 创建一个Web Service客户端:使用Spring Boot提供的`WebServiceTemplate`类来创建一个Web Service客户端。 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.ws.client.core.support.WebServiceGatewaySupport; @Component public class WebServiceClient extends WebServiceGatewaySupport { @Autowired public WebServiceClient() { // 设置WebService服务的URL setDefaultUri("http://example.com/your-webservice-url"); } public YourResponseObject callWebService(YourRequestObject request) { // 使用WebServiceTemplate发送请求并获取响应 YourResponseObject response = (YourResponseObject) getWebServiceTemplate() .marshalSendAndReceive(request); return response; } } ``` 3. 创建请求和响应对象:根据你的Web Service接口定义,创建对应的请求和响应对象。 ```java import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class YourRequestObject { // 请求参数 private String parameter1; private String parameter2; // getter和setter方法 // ... } @XmlRootElement public class YourResponseObject { // 响应数据 private String result1; private String result2; // getter和setter方法 // ... } ``` 4. 创建Controller:创建一个控制器类,用于处理客户端的请求。 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class YourController { @Autowired private WebServiceClient webServiceClient; @PostMapping("/your-endpoint") public YourResponseObject callWebService(@RequestBody YourRequestObject request) { YourResponseObject response = webServiceClient.callWebService(request); return response; } } ``` 以上就是在Spring Boot中调用Web Service接口使用JSON格式进行数据交互的基本步骤。根据你的具体情况,可能还需要根据实际需求进行一些额外的配置和处理。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值