我将Axis2/Java(1.6.2(安装为Tomcat(8.0.5(Webapp。
我开发了两个服务(HelloWorld和简单计算器(,它们运行良好。
现在,我尝试用一种从外部XML读取信息的方法来开发一个服务。
此文件位于以下目录中:"$CATALINA_HOME/webapps/axis2/myService/"。
我使用Ant编译.aar档案。
当我通过url 调用此方法时
http://127.0.0.1:8080/axis2/services/InventoryCheck/doCheck?args0=12&args1=9
我收到:
<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>
我在Tomcat7中尝试了同样的服务,它运行得很好。
我该如何解决?
感谢
这是服务java类:
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);
}
}
如果我将字符串realPath定义为products.xml文件的绝对路径,它就可以正常工作。所以问题出在这三条线上:
MessageContext msgContext = MessageContext.getCurrentMessageContext();
ServletContext servletContext = (ServletContext) msgContext.getProperty(HTTPConstants.MC_HTTP_SERVLETCONTEXT);
String realPath = servletContext.getRealPath(path);
还有两个简单的java类定义了Object ReaderXML和Product。
这是services.xml文件:
<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>
已解决:
为了使该服务也能与Tomcat8一起使用,我只更改了字符串路径
来自:
private String path = "myService/products.xml";
至:
private String path = "/myService/products.xml";
此不带"/"的字符串仅适用于Tomcat 7,不适用于Tomcat 8.0.5。