1

I'm creating a web service by using the Aramex API to calculate the shipping rate for a product, based on this tutorial. The following is my test class:

package com.test;

import net.aramex.ws.shippingapi.v1.*;

import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import javax.xml.ws.WebServiceRef;
import java.net.URL;

public class AramexRateCalculatorClient {

@WebServiceRef(wsdlLocation = "C:\\aramex-rates-calculator-wsdl.wsdl")
private static Service10_Service service;

public static void main(String [] args) {

    try {

    service = new Service10_Service();

    RateCalculatorRequest request = new RateCalculatorRequest();
    ClientInfo clientInfo = new ClientInfo();
    clientInfo.setUserName("mymail@gmail.com");
    clientInfo.setPassword("abcd1234");
    clientInfo.setVersion("v1.0");

    Transaction transaction = new Transaction();
    transaction.setReference1("001");

    Address orgAddress = new Address();
    orgAddress.setCity("Amman");
    orgAddress.setCountryCode("JO");

    Address dstAddress = new Address();
    dstAddress.setCity("Dubai");
    dstAddress.setCountryCode("AE");

    ShipmentDetails shipmentDetails = new ShipmentDetails();
    shipmentDetails.setPaymentType("P");
    shipmentDetails.setProductGroup("EXP");
    shipmentDetails.setProductType("PPX");

    Weight weight = new Weight();
    weight.setUnit("KG");
    weight.setValue(5);
    Weight cweight = new Weight();
    cweight.setUnit("KG");
    cweight.setValue(5);
    shipmentDetails.setActualWeight(weight);
    shipmentDetails.setChargeableWeight(cweight);
    shipmentDetails.setNumberOfPieces(5);

    JAXBElement<ClientInfo>  jaxbElementClientInfo = new JAXBElement(new QName("http://ws.aramex.net/ShippingAPI/v1/") , ClientInfo.class , clientInfo);
    request.setClientInfo(jaxbElementClientInfo);

    JAXBElement<Transaction>  jaxbElementTransaction = new JAXBElement(new QName("http://ws.aramex.net/ShippingAPI/v1/") , Transaction.class , transaction);
    request.setTransaction(jaxbElementTransaction);

    JAXBElement<Address>  jaxbElementorgAddress = new JAXBElement(new QName("http://ws.aramex.net/ShippingAPI/v1/") , Address.class , orgAddress);
    request.setOriginAddress(jaxbElementorgAddress);

    JAXBElement<Address>  jaxbElementDstAddress = new JAXBElement(new QName("http://ws.aramex.net/ShippingAPI/v1/") , Address.class , dstAddress);
    request.setDestinationAddress(jaxbElementDstAddress);

    JAXBElement<ShipmentDetails>  jaxbElementShipmentDetails = new JAXBElement(new QName("http://ws.aramex.net/ShippingAPI/v1/") , ShipmentDetails.class , shipmentDetails);
    request.setShipmentDetails(jaxbElementShipmentDetails);


    RateCalculatorResponse response = service.getService10().calculateRate(request);
    System.out.println("success");

    } catch (Exception ex) {
        System.out.println(ex);
    }
  }
}

When I run my main method, it gives me the following exception (when calling the calculateRate method):

com.sun.xml.ws.fault.ServerSOAPFaultException: Client received SOAP Fault from server: The formatter threw an exception while trying to deserialize the message: Error in deserializing body of request message for operation 'CalculateRate'. Name cannot begin with the '/' character, hexadecimal value 0x2F. Line 1, position 244. Please see the server log to find more detail regarding exact cause of the failure.
    at com.sun.xml.ws.fault.SOAP11Fault.getProtocolException(SOAP11Fault.java:193)
    at com.sun.xml.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:134)
    at com.sun.xml.ws.client.sei.StubHandler.readResponse(StubHandler.java:252)
    at com.sun.xml.ws.db.DatabindingImpl.deserializeResponse(DatabindingImpl.java:181)
    at com.sun.xml.ws.db.DatabindingImpl.deserializeResponse(DatabindingImpl.java:262)
    at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:128)
    at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:102)
    at com.sun.xml.ws.client.sei.SEIStub.invoke(SEIStub.java:154)
    at $Proxy35.calculateRate(Unknown Source)
    at com.aeturnum.ajlan.AramexRateCalculatorClient.main(AramexRateCalculatorClient.java:76)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

What is the reason for this error and how can I fix it?

4

1 回答 1

2

你用QName错了。单参数构造函数采用命名空间,它采用元素的“本地名称”。您需要使用其他构造函数之一。

自行查找此问题的一种方法是检查测试代码生成的 xml 请求。您应该对此感到满意,因为在调试 Web 服务调用问题时经常需要检查实际请求和响应。

于 2013-07-01T14:42:40.307 回答