Search code examples
xmlanttomcat7apache-axistomcat8

Axis 2: "URI cannot be null" using Tomcat 8


I installed Axis2/Java (1.6.2) as Tomcat (8.0.5) Webapp.

I develope two services (HelloWorld, and simple Calculator) and they work fine.

Now, i try to develope a service with one method that reads information from an external XML.

This file is located in this directory: "$CATALINA_HOME/webapps/axis2/myService/".

I compile .aar archive using Ant.

When i invoke this method by url

http://127.0.0.1:8080/axis2/services/InventoryCheck/doCheck?args0=12&args1=9

i receive:

<soapenv:Reason xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Text xml:lang="en-US">URI cannot be null</soapenv:Text>
</soapenv:Reason>

I try this same service with Tomcat 7 and it works fine.

How i can resolve?

Thanks


This is the service java class:

public class InventoryCheckService {

private String path = "myService/products.xml";

public boolean doCheck(String sku, int quantity){

    MessageContext msgContext = MessageContext.getCurrentMessageContext();
    ServletContext servletContext = (ServletContext) msgContext.getProperty(HTTPConstants.MC_HTTP_SERVLETCONTEXT);
    String realPath = servletContext.getRealPath(path);

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
    factory.setNamespaceAware(true);

    Document doc = null;
    try {
        DocumentBuilder builder = factory.newDocumentBuilder();
        doc = builder.parse(realPath);
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

    ReaderXML reader = new ReaderXML(doc);
    Product product = reader.getBySku(sku);

    return (product != null) && (product.getQuantity() >= quantity);

}
}

If i define the string realPath as absolute path of products.xml file it works fine. So the problem is in these three lines:

MessageContext msgContext = MessageContext.getCurrentMessageContext();
ServletContext servletContext = (ServletContext) msgContext.getProperty(HTTPConstants.MC_HTTP_SERVLETCONTEXT);
String realPath = servletContext.getRealPath(path);

There is also two simple java class that define the Object ReaderXML and Product.

This is services.xml file:

<service name="InventoryCheck">

<description>
    Webservice Inventory Check
</description>

<messageReceivers>
    <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
    <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</messageReceivers>

<parameter name="ServiceClass">com.myService.service.InventoryCheckService</parameter>

</service>

Solution

  • RESOLVED:

    To make this service working also with Tomcat 8, i only changed the String path

    from:

    private String path = "myService/products.xml";
    

    to:

    private String path = "/myService/products.xml";
    

    This string without " / " works only with Tomcat 7 and not working with Tomcat 8.0.5 .