0

JavaデスクトップクライアントからWebサービスメソッドを呼び出すにはどうすればよいですか。基本的に、私のJavaプログラムは、インターネット対応のデスクトップで実行されています。私のウェブサービスのURLは例えばです。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)

誰かがそれの何が悪いのかを知っているか、私の問題を解決するための良いチュートリアル/実用的な例を私に提供できますか?

4

3 に答える 3

3

問題は、適切なXMLを投稿していないことのようです。SOAPサービスの場合、SOAPメッセージを手作りすることは非常に困難です。Axisにこれを任せる必要があります。

手始めに、サービスWSDLを使用します。これは、XMLスキーマのサービス定義です。

WSDLを入手したら、クライアントスタブを生成できます。スタブを使用すると、XMLの代わりにJavaオブジェクトを使用してクライアントリクエストを作成できます。

これは、Axisでwsdltojavaツールを使用して行います。ドキュメントは次のとおりです:http://axis.apache.org/axis/java/user-guide.html#WSDL2JavaBuildingStubsSkeletonsAndDataTypesFromWSDL

そしてここに例があります:http ://www.digizol.com/2008/07/web-service-axis-tutorial-client-server.html

于 2013-01-15T05:46:54.530 に答える
1

JDK 6のツールを使用してwsimport、Webサービスのクライアントを取得してみてください。このツールは、プロジェクトで使用できるJavaコードを生成します。このためには、WSDLファイルが必要です。

次のコマンドを試すことができます。

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

そして、WebServiceの呼び出しにそのようなものの生成クラスを使用できます。

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

このように、追加のライブラリは必要ありません。

良い例は、Java6とJAX-WSを使用したWebサービスの利用にあります。

于 2013-01-15T07:17:13.993 に答える
0

メソッド名「Save_Consumption」は、「SOAPAction」という名前のHTTPヘッダーに設定する必要があるようです。

私はServiceメソッドとCallメソッドに精通していませんが、単純な古いHttpURLConnection APIを使用すると、次のようになります。

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

他のポスターが言っているように、wsimportコマンドを使用できますが、私の経験では、これは心臓発作のバンドエイドです。せずにやってみてください!

于 2014-04-21T20:20:28.010 に答える