springboot调用webservice简便方式

此方法不建议在需要高并发或者是批量调用webservice接口时使用,比较吃内存。仅在管理系统后台中,或者是用户量少时可以采用此取巧方案。

直接上核心代码:

package com.dhc.minboot.api;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * xx的webservice调用专用类
 */
@Component
public class HRClient {

    private static final Logger log = LoggerFactory.getLogger(HRClient.class);

    public Boolean doPost(String info) throws Exception {
        Boolean isSuccess = false;
        String wsUrl = "http://xxx.xx.xx.xxx/public/WEBHR_Service.asmx";
        URL url = new URL(wsUrl);
        URLConnection conn1 = url.openConnection();
        HttpURLConnection con = (HttpURLConnection) conn1;
        con.setDoInput(true);
        con.setDoOutput(true);
        con.setRequestMethod("POST");
        con.setRequestProperty("content-type","text/xml");
        con.setRequestProperty("SOAPAction","http://tempuri.org/BestSignPushUrl");
        String requestBody = "<soap:Envelope " +
                "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"> " +
                "<soap:Body>" +
                "<BestSignPushUrl xmlns=\"http://tempuri.org/\">" +
                "<httpBody>" + info +"</httpBody>" +
                "</BestSignPushUrl>" +
                " </soap:Body>" +
                "</soap:Envelope>";
        OutputStream out = con.getOutputStream();
        out.write(requestBody.getBytes());
        out.close();
        try {
            int code1 = con.getResponseCode();
            if(code1==200){
                InputStream is = con.getInputStream();
                byte[] b = new byte[1024];
                StringBuffer sb = new StringBuffer();
                int len = 0;
                while((len=is.read(b))!=-1){
                    String str = new String(b,0,len,"UTF-8");
                    sb.append(str);
                }
                log.info("hr系统请求返回{}",sb.toString());
                isSuccess = true;
                is.close();
                con.disconnect();
            } else {
                log.info("hr系统请求出现问题,code为{},参数为{}",code1,info);
            }
        } catch (Exception e) {
            log.error("hr系统请求出现问题,参数{}",info);
        }
        return isSuccess;
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮您解答关于 springboot 调用 webservice 接口的问题。首先,您需要在 pom.xml 文件中添加以下依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> ``` 接下来,您需要编写一个 WebServiceTemplate bean,并配置它的默认 URI。这个 bean 可以用来发送 SOAP 请求和接收 SOAP 响应。以下是一个示例: ```java @Configuration public class WebServiceConfig { @Bean public WebServiceTemplate webServiceTemplate() { WebServiceTemplate template = new WebServiceTemplate(); template.setDefaultUri("http://localhost:8080/your-webservice-url"); return template; } } ``` 然后,您需要创建一个 Java 类来调用您的 webservice 接口。以下是一个示例: ```java @Component public class MyWebServiceClient { @Autowired private WebServiceTemplate webServiceTemplate; public YourWebServiceResponse callYourWebServiceMethod(YourWebServiceRequest request) { YourWebServiceResponse response = (YourWebServiceResponse) webServiceTemplate.marshalSendAndReceive(request); return response; } } ``` 在最后一个示例中,我们创建了一个 MyWebServiceClient 类,并在其中注入了一个名为 webServiceTemplate 的 WebServiceTemplate bean。然后,我们创建了一个名为 callYourWebServiceMethod 的方法,其中我们将请求对象传递给 marshalSendAndReceive 方法,并接收响应对象。 希望这可以帮助回答您的问题!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值