简体   繁体   中英

Setting up a Java XML-RPC Servlet

I have to make a Java webapp that would listen for incoming HTTP POST requests, then retrieve the XML contained in the request body in order to process it. I am using Maven 4, Hibernate 3 and XML-RPC server. I successfully imported XML-RPC jar files using Maven.

Though everyone seem to say XML-RPC is the simpliest thing on earth, I am having a hard time implementing it. I am quite new to webapps. Looking at Apache XML-RPC tutorial I understand I need to create a class such as:

public class MyServer extends XmlRpcServlet {
    private XmlRpcServer server = new XmlRpcServer();
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        server.addHandler("myProcess", new MyProcessHandler);
        byte[] result = server.execute (request.getInputStream());
        response.setContentType ("text/xml");
        response.setContentLength (result.length());
        OutputStream out = response.getOutputStream();
        out.write (result);
        out.flush ();
    }
}

But that won't compile because the "execute" method expects a XmlRpcRequest parameter. Any hint about what I am doing wrong?

Aside from that, I don't understand how I am going to get the request's body from my function myProcess(). I have a MyProcessHandler class (which extends no class) implementing a myProcess() function. Do I need to add a parameter in this function? Is so then which type would it be?

The main problem lies in the XML-RPC version: Apache first made an old version named helma-xmlrpc, then refactored it with deep changes into org.apache.xmlrpc. Though Apache updated some parts of its XML-RPC online documentation, some other parts still reference helma-xmlrpc with no hint, on each page, of the version used.

Moreover, Apache says that projects using helma-xmlrpc only have to update their imports in order to use the new version org.apache.xmlrpc, no impacts on the code - That's totally wrong. Some methods and even some classes disappeared, or the signature changed, and some classes have been put into sub-directories, so the imports don't work anymore.

Okay. So once I figured out that, I also understood that most of the Internet tutorials dealing with Apache XML-RPC use the old helma-xmlrpc version, but show their imports as org.apache.xmlrpc. As a result, the implementations they present won't work if you paste it in your own project which uses org.apache.xmlrpc. It doesn't even compile.

I looked over the Internet for up-to-date org.apache.xmlrpc-implementation-with-servlets tutorials with no result. Hence I decided to use old helma-xmlrpc and all went well. With Helma, the code I posted in my first message now compiles and is right.

As for the link between the listener and MyProcessHandler class, it exists thanks to the addHandler function. Once the handler is declared, all incoming requests with methodName like 'myProcess.myFunction' are automatically redirected toward MyProcessHandler.myFunction() when the instruction server.execute(...) is processed.

For that to work, myFunction() must declare one String input parameter. When myFunction() will be called, this parameter contains the body of the request (extracted by request.getInputStream() in the servlet). myFunction() also has to return something, which will be returned into the byte[] result variable of the servlet.

I made good use of the link below, very complete and treating of helma-xmlrpc only with no pretense of using org.apache.xmlrpc...: http://www.perfectxml.com/oreilly/chapter.asp?row_id=11

I hope this answer is clear enough (my English speaking is not perfect...) and it will help other developers to understand Apache XML-RPC.

You can use XmlRpcServletServer (apache xml-rpc 3.1.3):

public class EmbeddedXmlRpc extends HttpServlet
{
  ...
  protected XmlRpcServletServer server = new XmlRpcServletServer();
  ...
  server.setHandlerMapping(phm);
  ...
  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException
  {
    server.execute(request, response);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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