繁体   English   中英

从Java SE程序调用Web服务方法

[英]Call web service method from Java SE program

如何从Java桌面客户端调用Web服务方法。 基本上我的Java程序在启用Internet的桌面上运行。 我的网络服务网址是例如。 localhost:8090/Service.svc ,此Web服务基本上是将数据存储在数据库中,我们只需要调用Web服务方法并传递xml字符串作为参数即可。

我的示例代码如下所示:

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();
        }
    }

但是它显示了异常,这是堆栈跟踪。

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)

谁能知道这有什么问题,或者提供任何好的教程/工作示例来解决我的问题?

问题似乎是您没有发布适当的XML。 对于SOAP服务,手工处理SOAP消息非常困难。 您应该让Axis为您执行此操作。

对于初学者,您将使用服务WSDL。 这是XML模式中的服务定义。

一旦有了WSDL,就可以生成客户端存根。 存根允许您使用Java对象而不是XML来构建客户端请求。

您可以使用wsdltojava工具在Axis中执行此操作 以下是文档: http : //axis.apache.org/axis/java/user-guide.html#WSDL2JavaBuildingStubsSkeletonsAndDataTypesFromWSDL

这是一个示例: http : //www.digizol.com/2008/07/web-service-axis-tutorial-client-server.html

您可以尝试使用JDK 6中的wsimport工具来获取WebService的客户端。 该工具生成可在proyect中使用的Java代码。 为此,您需要WSDL文件。

您可以尝试下一个命令:

 wsimport http://localhost:8090/Service.svc?wsdl -s C:\sources -Xnocompile

您可以将generate类用于调用WebService之类的东西:

ExampleService exampleService = new ExampleService();
Example example = exampleService.getExamplePort();
ReturnValue rv = example.doSomething(param);

通过这种方式,您不需要其他库。

您可以在使用Java 6和JAX-WS使用Web服务中找到一个很好的例子。

看起来应该在名称为“ SOAPAction”的HTTP标头中设置方法名称“ Save_Consumption”。

我不熟悉Service和Call方法,但是使用普通的旧HttpURLConnection API,它将是:

    URL httpUrl = new URL(accessUrl);
    HttpURLConnection connection = (HttpURLConnection) httpUrl.openConnection();
    connection.addRequestProperty("SOAPAction", soapAction);

就像其他张贴者所说的那样,您可以使用wsimport命令,但是根据我的经验,这是心脏病发作的创可贴。 很好,您可以尝试并尝试!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM