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

时间:2013-01-15 05:35:50

标签: java web-services axis

如何从java桌面客户端调用Web服务方法。基本上我的java程序是在支持互联网的桌面上运行的。我的网络服务网址是例如。 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)

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

3 个答案:

答案 0 :(得分:3)

问题似乎是您没有发布适当的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

答案 1 :(得分:1)

您可以尝试使用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);

通过这种方式,您不需要额外的库。

您可以在Consuming a Web Service with Java 6 and JAX-WS

中找到一个很好的例子

答案 2 :(得分:0)

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

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

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

就像其他海报所说,你可以使用wsimport命令,但根据我的经验,这是心脏病发作的创可贴。你很乐意尝试不做!