初学者:来自 Java 的 Soap 请求响应调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11675690/
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
Beginner: Soap Request Response call from java
提问by Dolly Chan
I try to do a SOAP request and get SOAP response by Java, and then call it from the JSP page. I researched many sites, and try many ways, it doesn't seem to able to get it working.
我尝试执行 SOAP 请求并通过 Java 获取 SOAP 响应,然后从 JSP 页面调用它。我研究了很多网站,并尝试了很多方法,但似乎无法使其正常工作。
URL u = new URL("http://www.mysitename.com/mysoaprequest.wsdl");
URLConnection uc = u.openConnection();
HttpURLConnection connection = (HttpURLConnection) uc;
....
....
Can I just create a wsdl file with the soap request xml inside?
我可以创建一个包含soap请求xml的wsdl文件吗?
I read something about Web Service, do I have to get some Web Service working to make it work?
我读了一些关于 Web Service 的内容,我是否必须让一些 Web Service 工作才能使其工作?
I use some wsdl to test http://www.webservicex.net/AustralianPostCode.asmx?WSDL
我使用一些 wsdl 来测试http://www.webservicex.net/AustralianPostCode.asmx?WSDL
I kept getting this error
我一直收到这个错误
java.io.IOException: Server returned HTTP response code: 500 for URL: http://www.webservicex.net/AustralianPostCode.asmx?WSDL
java.io.IOException:服务器返回 HTTP 响应代码:500 用于 URL:http: //www.webservicex.net/AustralianPostCode.asmx?WSDL
If anyone can point me to the right direction will be greatly appreciated.
如果有人能指出我正确的方向,将不胜感激。
This is the actual soap request I need to make
这是我需要提出的实际肥皂请求
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.application.tvcc.dmv.ca.gov/">
<soapenv:Header/>
<soapenv:Body>
<ser:getNewCourseCompletion>
<arg0>
<userDto>
<password>********</password>
<userId>[email protected]</userId>
</userDto>
</arg0>
</ser:getNewCourseCompletion>
</soapenv:Body>
</soapenv:Envelope>
回答by davidfmatheson
The WSDL is a kind of menu defining what SOAP requests it will accept, and what it will give you back in return. You don't actually send a WSDL document to that URL as a request. If you want to dive into exactly what XML you should send, I would recommend downloading soapUI:
WSDL 是一种菜单,用于定义它将接受什么 SOAP 请求,以及它将返回给您什么。您实际上并未将 WSDL 文档作为请求发送到该 URL。如果您想深入了解您应该发送的 XML,我建议您下载soapUI:
http://sourceforge.net/projects/soapui/files/soapui/4.5.1/
http://sourceforge.net/projects/soapui/files/soapui/4.5.1/
Creating a new project and adding the link you provided:
创建一个新项目并添加您提供的链接:
http://www.webservicex.net/AustralianPostCode.asmx?WSDL
http://www.webservicex.net/AustralianPostCode.asmx?WSDL
as the initial WSDL (you should see the box in SoapUI). It will then generate the kinds of requests that you can send that web service, and let you fill in the blanks and send it along.
作为初始 WSDL(您应该在 SoapUI 中看到该框)。然后它将生成您可以发送该 Web 服务的请求类型,并让您填写空白并将其发送。
If you want to do that from Java code, you have to pick a web service platform like JAX-WS and have it generate the Java code that forms the requests and sends them along. Take a look at some JAX-WS tutorials.
如果您想从 Java 代码中做到这一点,您必须选择一个像 JAX-WS 这样的 Web 服务平台,并让它生成形成请求并将它们一起发送的 Java 代码。看看一些 JAX-WS 教程。
回答by Cuga
I know it's a little late, but I'll throw an answer out there anyway.
我知道现在有点晚了,但无论如何我都会给出答案。
What you want to do is auto-generate Java code for this web service. I'll demonstrate using the WSDL you referenced: http://www.webservicex.net/AustralianPostCode.asmx?WSDL
您要做的是为该 Web 服务自动生成 Java 代码。我将演示使用您引用的 WSDL:http: //www.webservicex.net/AustralianPostCode.asmx?WSDL
In Java, the tool to auto-generate the code is wsimport
. You'll want to use something like this:
在 Java 中,自动生成代码的工具是wsimport
. 你会想要使用这样的东西:
wsimport http://www.webservicex.net/AustralianPostCode.asmx?WSDL -p com.company.whateveruwant -d . -keep
This will put the code you want in the specified package (here com.company.whateveruwant
).
这会将您想要的代码放在指定的包中(此处com.company.whateveruwant
)。
From there, all you have to do is simply invoke the SOAP method like a normal Java library:
从那里,您所要做的就是像普通 Java 库一样调用 SOAP 方法:
package com.company.whateveruwant;
import org.junit.Test;
public class AustralianPostCodeTest {
@Test
public void test() {
AustralianPostCodeSoap soap = new AustralianPostCode().getAustralianPostCodeSoap();
String response = soap.getAustralianPostCodeByLocation("Collinswood");
System.out.println(response);
}
}
This prints out:
这打印出来:
<NewDataSet>
<Table>
<Location>Collinswood</Location>
<PostCode> SA 5081</PostCode>
</Table>
</NewDataSet>
回答by Enrique San Martín
SOAP is a very old and very difficult protocol (there are many blogs that tell you why soap is a complete mess), well, if you can, try REST (like RESTeasy framework). But if you have to use soap, you can use AXIS (i use AXIS1 because there are many legacy apps that use that Library), i don't have any experience with AXIS2, so here is the hint's to make clients:
SOAP 是一个非常古老且非常困难的协议(有很多博客会告诉您为什么soap 是一团糟),好吧,如果可以,请尝试REST(例如RESTeasy 框架)。但是,如果您必须使用肥皂,则可以使用 AXIS(我使用 AXIS1,因为有许多使用该库的遗留应用程序),我对 AXIS2 没有任何经验,因此这里是提示客户:
1) You have to add to your classpath the axis library (in linux like the following example):
1) 您必须将轴库添加到类路径中(在 linux 中,如下例所示):
env AXISCLASSPATH=~/axis-1_4/lib/axis.jar:~/axis-1_4/lib/commons-discovery-0.2.jar:~/axis-1_4/lib/commons-logging-1.0.4.jar:~/axis-1_4/lib/jaxrpc.jar:~/axis-1_4/lib/saaj.jar:~/axis-1_4/lib/log4j-1.2.8.jar:~/axis-1_4/lib/xml-apis.jar:~/axis-1_4/lib/xercesImpl.jar:~/wsdl4j-1_6_2/lib/wsdl4j.jar
2) Then you can create the client/server like:
2)然后您可以创建客户端/服务器,如:
java -classpath $AXISCLASSPATH org.apache.axis.wsdl.WSDL2Java --server-side example.wsdl
The --server-side make that axis create the wsdl files for deploy and undeploy the server layer, if you want to create a client, remove it.
--server-side 使该轴创建 wsdl 文件以部署和取消部署服务器层,如果要创建客户端,请将其删除。
3) Now you have a folder like com/example/server/.java 4) In the Impl file (like ExampleImpl.java) you have the methods that you have to impl to do some with the WS (in the server side).
3) 现在你有一个像 com/example/server/.java 这样的文件夹 4) 在 Impl 文件(比如 ExampleImpl.java)中,你有一些方法,你必须用 WS 来执行一些操作(在服务器端)。
5) If you want to consume the WS, you have to do some like:
5)如果你想消耗WS,你必须这样做:
EngineConfiguration engine = EngineConfigurationFactoryFinder
.newFactory().getClientEngineConfig();
SimpleProvider provider = new SimpleProvider(engine);
provider.deployTransport("http", new CommonsHTTPSender());
ExampleLocator sendSmsLocator = new ExampleLocator(engine);
Example example = null;
try {
example = (ExampleBindingStub) exampleLocator.getExample();
} catch (Exception e) {
e.printStackTrace();
}
try {
result = example.exampleMethod(params...);
} catch (Exception e) {
e.printStackTrace();
}
Well there is many documentation in the axis site:
好吧,轴站点中有很多文档:
http://axis.apache.org/axis/java/index.html
http://axis.apache.org/axis/java/index.html
Regards,
问候,