使用Java请求WebService

1、使用cmd命令窗口执行命令

wsimport -encoding utf-8 -p 【磁盘位置】 -keep 【wsdl地址】

此命令可编译代码,得到一些需要用到的Java类

2、找到磁盘位置,删掉.class文件,导入.java文件到idea

 导入之后,可以写个测试类测试

public static void main(String[] args) throws MalformedURLException {
        URL _wsdlLocation = new URL("web-service接口对应的wsdl地址");
        //XXXService是举的例子,真正叫什么Service是看生成的Java文件中的那个Service叫什么
        XxxService xxxService = new XxxService(_wsdlLocation,new QName("wsdl接口地址里面的NameSpace", "XxxService"));
        XxxServiceSoap xxxServiceSoap = xxxService.getXxxServiceSoap();
        String respXml = xxxServiceSoap.对应web-service接口的方法名(方法对应的入参);
        //这样就拿到响应结果了,按照XML格式解析即可
        System.out.println(respXml);
    }

还可以为了以后调用方便,简单的封装一下,需要调用web-service接口中的方法,就直接使用

ServiceSoap的对象.方法名就可以了。

import java.net.MalformedURLException;
import java.net.URL;
import javax.annotation.PostConstruct;
import javax.xml.namespace.QName;
import org.springframework.stereotype.Component;

@Component
public class XxxClient {

    private XxxServiceSoap xxxServiceSoap;

    @PostConstruct
    public void init(){
        try {
            URL _wsdlLocation = new URL("web-service对应的wsdl地址");
            XxxService xxxService = new XxxService(_wsdlLocation,new QName("wsdl地址中的name-space地址", 
                    "XxxService"));
            xxxServiceSoap = xxxService.getXxxServiceSoap();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    public XxxServiceSoap getSoap(){
        return xxxServiceSoap;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
使用Java发出请求WebService,你可以按照以下步骤进行操作: 1. 导入必要的库文件:在Java项目中,你需要导入相应的库文件,以便能够使用WebService相关的类和方法。具体的库文件取决于你使用WebService框架,例如JAX-WS或Apache Axis等。 2. 创建客户端代理:使用WebService提供的WSDL(Web服务描述语言)文件,通过客户端代理工具生成客户端代码。这个代理类将充当与WebService进行交互的中间层。 3. 初始化WebService客户端:在Java代码中,你需要实例化WebService客户端类,并进行相应的配置。这包括设置WebService的地址、命名空间和其他参数。 4. 调用WebService方法:通过客户端代理类的实例,你可以直接调用WebService提供的方法。根据WebService的定义,可能需要提供相应的参数,并接收返回值。 5. 处理返回结果:根据具体需要,你可以对返回结果进行处理和解析。这可能涉及到XML解析、数据转换等操作。 下面是一个简单示例,用于展示如何使用Java发送SOAP请求到一个Web服务: ```java import javax.xml.namespace.QName; import javax.xml.soap.*; import java.io.ByteArrayOutputStream; public class WebServiceClient { public static void main(String[] args) { try { // 创建SOAP消息 SOAPMessage soapMessage = MessageFactory.newInstance().createMessage(); // 创建SOAP请求 SOAPPart soapPart = soapMessage.getSOAPPart(); SOAPEnvelope envelope = soapPart.getEnvelope(); envelope.addNamespaceDeclaration("web", "http://example.com/webservice"); SOAPBody soapBody = envelope.getBody(); QName operationName = new QName("http://example.com/webservice", "getHelloWorld"); SOAPBodyElement soapBodyElement = soapBody.addBodyElement(operationName); // 设置请求参数(如果有) QName parameterName = new QName("http://example.com/webservice", "name"); SOAPElement parameterElement = soapBodyElement.addChildElement(parameterName); parameterElement.setValue("John"); // 发送请求 SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); SOAPConnection soapConnection = soapConnectionFactory.createConnection(); String url = "http://example.com/webservice"; SOAPMessage response = soapConnection.call(soapMessage, url); // 处理响应 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); response.writeTo(outputStream); String responseString = outputStream.toString(); // 打印响应结果 System.out.println("Response:\n" + responseString); // 关闭连接 soapConnection.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 这只是一个基本示例,实际上,你需要根据具体的WebService和框架进行相应的配置和参数设置。希望这能帮到你!
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值