从 Java SE 程序调用 Web 服务方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14331727/
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
Call web service method from Java SE program
提问by Shantanu Banerjee
How can I call a web service method from java desktop client. Basically my java program is running on a desktop which is internet enabled. My web service url is eg. localhost:8090/Service.svc
, This web service is basically store data in database we just need to call a web service method and pass a xml string as parameter.
如何从 Java 桌面客户端调用 Web 服务方法。基本上,我的 Java 程序运行在支持 Internet 的桌面上。我的网络服务网址是例如。localhost:8090/Service.svc
, 这个web service 基本上是将数据存储在数据库中我们只需要调用一个web service方法并传递一个xml字符串作为参数。
My sample code is look like:
我的示例代码如下所示:
public static void main(String [] args)
{
String accessURL,method, parameters[];
try {
String xml = "<DocumentElement>"
+ "<Table1>"
+ "<ConsumptionID>0</ConsumptionID>"
+ "<ConsumptionDate>2012/01/01 00:00:00</ConsumptionDate>"
+ "<MeterNumber>99999901</MeterNumber>"
+ "<Voltage>200</Voltage>"
+ "<Ampere>50</Ampere>"
+ "<PowerFactor>0.91</PowerFactor>"
+ "<KiloWatt>8525</KiloWatt>"
+ "<HourValue>3</HourValue>"
+ "<EndKiloWattHour>5841</EndKiloWattHour>"
+ "<KVA>1000</KVA>"
+ "<ClientID>1011</ClientID>"
+ "<CreatedBy>1</CreatedBy>"
+ "</Table1>"
+ "</DocumentElement>";
accessURL = "http://localhost:8090/Service.svc";
method = "Save_Consumption";
parameters = new String[1];
parameters[0] = xml;
Service service = new Service();
Call call = (Call)service.createCall();
call.setTargetEndpointAddress(new java.net.URL(accessURL));
call.setOperationName(method);
Object[] params = new Object[parameters.length];
params[0]=(Object)(parameters[0]);
String retval = (String)call.invoke(params);
System.out.println("Operation Result: " + retval);
} catch(Exception e) {
System.err.println(e.toString());
e.printStackTrace();
}
}
But it shows exceptions, here is the stack trace.
但它显示异常,这是堆栈跟踪。
run:
- Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
The message with Action '' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver. Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None).
AxisFault
faultCode: {http://schemas.microsoft.com/ws/2005/05/addressing/none}ActionNotSupported
faultSubcode:
faultString: The message with Action '' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver. Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None).
faultActor:
faultNode:
faultDetail:
{http://xml.apache.org/axis/}stackTrace:The message with Action '' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver. Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None).
at org.apache.axis.message.SOAPFaultBuilder.createFault(SOAPFaultBuilder.java:222)
at org.apache.axis.message.SOAPFaultBuilder.endElement(SOAPFaultBuilder.java:129)
at org.apache.axis.encoding.DeserializationContext.endElement(DeserializationContext.java:1087)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:606)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1741)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2898)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:488)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(SAXParserImpl.java:302)
at org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227)
at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:696)
at org.apache.axis.Message.getSOAPEnvelope(Message.java:435)
at org.apache.axis.handlers.soap.MustUnderstandChecker.invoke(MustUnderstandChecker.java:62)
at org.apache.axis.client.AxisClient.invoke(AxisClient.java:206)
at org.apache.axis.client.Call.invokeEngine(Call.java:2784)
at org.apache.axis.client.Call.invoke(Call.java:2767)
at org.apache.axis.client.Call.invoke(Call.java:2443)
at org.apache.axis.client.Call.invoke(Call.java:2366)
at org.apache.axis.client.Call.invoke(Call.java:1812)
at com.lmeasure.utils.HelloClient.main(HelloClient.java:54)
{http://xml.apache.org/axis/}hostname:eeipldt0132
The message with Action '' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver. Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None).
at org.apache.axis.message.SOAPFaultBuilder.createFault(SOAPFaultBuilder.java:222)
at org.apache.axis.message.SOAPFaultBuilder.endElement(SOAPFaultBuilder.java:129)
at org.apache.axis.encoding.DeserializationContext.endElement(DeserializationContext.java:1087)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:606)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1741)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2898)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:488)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(SAXParserImpl.java:302)
at org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227)
at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:696)
at org.apache.axis.Message.getSOAPEnvelope(Message.java:435)
at org.apache.axis.handlers.soap.MustUnderstandChecker.invoke(MustUnderstandChecker.java:62)
at org.apache.axis.client.AxisClient.invoke(AxisClient.java:206)
at org.apache.axis.client.Call.invokeEngine(Call.java:2784)
at org.apache.axis.client.Call.invoke(Call.java:2767)
at org.apache.axis.client.Call.invoke(Call.java:2443)
at org.apache.axis.client.Call.invoke(Call.java:2366)
at org.apache.axis.client.Call.invoke(Call.java:1812)
at com.lmeasure.utils.HelloClient.main(HelloClient.java:54)
Can anyone know whats wrong with that or provide me any good tutorial/working example to resolve my issue?
谁能知道这有什么问题,或者为我提供任何好的教程/工作示例来解决我的问题?
回答by EJK
The problem seems to be that you are not posting the appropriate XML. For SOAP services it is very difficult to hand-craft SOAP messages. You should let Axis do this for you.
问题似乎是您没有发布适当的 XML。对于 SOAP 服务,手工制作 SOAP 消息非常困难。您应该让 Axis 为您执行此操作。
For starters you will the the service WSDL. This is the service definition in XML schema.
对于初学者,您将使用服务 WSDL。这是 XML 模式中的服务定义。
Once you have the WSDL, you can generate client stubs. The stubs will allow you to build the client request using Java objects instead of XML.
拥有 WSDL 后,您就可以生成客户端存根。存根将允许您使用 Java 对象而不是 XML 来构建客户端请求。
You do this in Axis using the wsdltojavatool. Here is the documentation: http://axis.apache.org/axis/java/user-guide.html#WSDL2JavaBuildingStubsSkeletonsAndDataTypesFromWSDL.
您可以使用wsdltojava工具在 Axis 中执行此操作。这是文档:http: //axis.apache.org/axis/java/user-guide.html#WSDL2JavaBuildingStubsSkeletonsAndDataTypesFromWSDL。
And here is an example: http://www.digizol.com/2008/07/web-service-axis-tutorial-client-server.html
这是一个例子:http: //www.digizol.com/2008/07/web-service-axis-tutorial-client-server.html
回答by Paul Vargas
You can try using the tool wsimport
from JDK 6 for getting the client of your WebService. This tool generates the Java Code that you can use in your proyect. For this, you need the WSDLfile.
您可以尝试使用wsimport
JDK 6 中的工具来获取 WebService 的客户端。此工具生成您可以在您的项目中使用的 Java 代码。为此,您需要WSDL文件。
You can try the next command:
您可以尝试下一个命令:
wsimport http://localhost:8090/Service.svc?wsdl -s C:\sources -Xnocompile
And you can use the generate classes for something like that for call to the WebService:
您可以使用生成类来调用 WebService:
ExampleService exampleService = new ExampleService();
Example example = exampleService.getExamplePort();
ReturnValue rv = example.doSomething(param);
With this way, you not need additional libraries.
通过这种方式,您不需要额外的库。
You can find a good example in Consuming a Web Service with Java 6 and JAX-WS
您可以在使用 Java 6 和 JAX-WS 使用 Web 服务中找到一个很好的示例
回答by Johannes Brodwall
It looks like the method name "Save_Consumption" should be set in an HTTP Header with the name "SOAPAction".
看起来方法名称“Save_Consumption”应该在名为“SOAPAction”的 HTTP 标头中设置。
I am not familiar with the Service and Call methods, but with the plain old HttpURLConnection API, this would be:
我不熟悉 Service 和 Call 方法,但是使用普通的旧 HttpURLConnection API,这将是:
URL httpUrl = new URL(accessUrl);
HttpURLConnection connection = (HttpURLConnection) httpUrl.openConnection();
connection.addRequestProperty("SOAPAction", soapAction);
Like the other posters are saying, you can use the wsimport command, but in my experience, this is a band-aid on a heart attack. Good on you to try and do without!
就像其他海报所说的那样,您可以使用 wsimport 命令,但根据我的经验,这是心脏病发作的创可贴。很高兴你尝试没有!