如何在 Java 中进行 SOAP 调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2149038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:42:22  来源:igfitidea点击:

How to make a SOAP call in Java

javasoap

提问by Jenni

This seems like it should be simple, but maybe I'm missing something. I just want to make a SOAP call in Java, preferrably using only built-in APIs. I'm a little overwhelmed looking at javax.xml.soappackage in the Java documentation. I have tried searching Google, but it seems like all the results are from 2000-2002, and they are all talking about libraries that can be used for SOAP calls (before SOAP libraries were built in, I guess).

这看起来应该很简单,但也许我遗漏了一些东西。我只想用 Java 进行 SOAP 调用,最好只使用内置 API。看着Java 文档中的javax.xml.soap包,我有点不知所措。我试过在 Google 上搜索,但似乎所有结果都是 2000-2002 年的,而且它们都在谈论可用于 SOAP 调用的库(我猜是在内置 SOAP 库之前)。

I don't need to handle the SOAP request; only make one. This sitehas an example that is pretty simple, but it doesn't use the built-in Java SOAP libraries. How would I do basically the same thing using core Java?

我不需要处理 SOAP 请求;只做一个。该站点有一个非常简单的示例,但它不使用内置的 Java SOAP 库。我将如何使用核心 Java 做基本相同的事情?

// Create the parameters
Vector params = new Vector(  );
params.addElement(
    new Parameter("flightNumber", Integer.class, flightNumber, null));
params.addElement(
    new Parameter("numSeats", Integer.class, numSeats, null));
params.addElement(
    new Parameter("creditCardType", String.class, creditCardType, null));
params.addElement(
    new Parameter("creditCardNumber", Long.class, creditCardNum, null));

// Create the Call object
Call call = new Call(  );
call.setTargetObjectURI("urn:xmltoday-airline-tickets");
call.setMethodName("buyTickets");
call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
call.setParams(params);

// Invoke
Response res = call.invoke(new URL("http://rpc.middleearth.com"), "");

// Deal with the response

采纳答案by bmargulies

Soap has changed a lot since the early days. You can do things like what you describe, but it is not common.

自早期以来,肥皂发生了很大变化。你可以做你描述的事情,但这并不常见。

A more common practice now is to use a wsdl2java tool to generate a client API from a WSDL description of the service. That will give you a nice, clean, API to call.

现在更常见的做法是使用 wsdl2java 工具从服务的 WSDL 描述生成客户端 API。这将为您提供一个漂亮、干净的 API 来调用。

Apache CXFis one place to go for this sort of thing.

Apache CXF是处理此类事情的一个地方。

One proviso is rpc/encoded. If you are dealing with an old service, it might be rpc/encoded, and in that case your best bet is Apache Axis1.x. Everything else has run away from rpc/encoded.

一个附带条件是 rpc/encoded。如果您正在处理旧服务,它可能是 rpc/encoded,在这种情况下,您最好的选择是Apache Axis1.x。其他一切都远离了 rpc/encoded。

回答by MariuszS

The simplest way is soap-ws library: https://github.com/reficio/soap-ws

最简单的方法是soap-ws库:https: //github.com/reficio/soap-ws

   SoapClient client = SoapClient.builder()
        .endpointUrl("http://rpc.middleearth.com")
        .build();

   client.post(envelope);