简体   繁体   English

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

[英]Call web service method from Java SE program

How can I call a web service method from java desktop client. 如何从Java桌面客户端调用Web服务方法。 Basically my java program is running on a desktop which is internet enabled. 基本上我的Java程序在启用Internet的桌面上运行。 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. localhost:8090/Service.svc ,此Web服务基本上是将数据存储在数据库中,我们只需要调用Web服务方法并传递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? 谁能知道这有什么问题,或者提供任何好的教程/工作示例来解决我的问题?

The problem seems to be that you are not posting the appropriate XML. 问题似乎是您没有发布适当的XML。 For SOAP services it is very difficult to hand-craft SOAP messages. 对于SOAP服务,手工处理SOAP消息非常困难。 You should let Axis do this for you. 您应该让Axis为您执行此操作。

For starters you will the the service WSDL. 对于初学者,您将使用服务WSDL。 This is the service definition in XML schema. 这是XML模式中的服务定义。

Once you have the WSDL, you can generate client stubs. 一旦有了WSDL,就可以生成客户端存根。 The stubs will allow you to build the client request using Java objects instead of XML. 存根允许您使用Java对象而不是XML来构建客户端请求。

You do this in Axis using the wsdltojava tool. 您可以使用wsdltojava工具在Axis中执行此操作 Here is the documentation: http://axis.apache.org/axis/java/user-guide.html#WSDL2JavaBuildingStubsSkeletonsAndDataTypesFromWSDL . 以下是文档: 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

You can try using the tool wsimport from JDK 6 for getting the client of your WebService. 您可以尝试使用JDK 6中的wsimport工具来获取WebService的客户端。 This tool generates the Java Code that you can use in your proyect. 该工具生成可在proyect中使用的Java代码。 For this, you need the WSDL file. 为此,您需要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: 您可以将generate类用于调用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服务中找到一个很好的例子。

It looks like the method name "Save_Consumption" should be set in an HTTP Header with the name "SOAPAction". 看起来应该在名称为“ SOAPAction”的HTTP标头中设置方法名称“ Save_Consumption”。

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. 就像其他张贴者所说的那样,您可以使用wsimport命令,但是根据我的经验,这是心脏病发作的创可贴。 Good on you to try and do without! 很好,您可以尝试并尝试!

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

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