简体   繁体   English

设置 Java XML-RPC Servlet

[英]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.我必须制作一个 Java webapp 来监听传入的 HTTP POST 请求,然后检索包含在请求正文中的 XML 以处理它。 I am using Maven 4, Hibernate 3 and XML-RPC server.我正在使用 Maven 4、Hibernate 3 和 XML-RPC 服务器。 I successfully imported XML-RPC jar files using Maven.我使用 Maven 成功导入了 XML-RPC jar 文件。

Though everyone seem to say XML-RPC is the simpliest thing on earth, I am having a hard time implementing it.尽管每个人似乎都说 XML-RPC 是地球上最简单的东西,但我很难实现它。 I am quite new to webapps.我对 webapps 很陌生。 Looking at Apache XML-RPC tutorial I understand I need to create a class such as:查看 Apache XML-RPC 教程,我知道我需要创建一个类,例如:

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.但这不会编译,因为“execute”方法需要一个 XmlRpcRequest 参数。 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().除此之外,我不明白我将如何从我的函数 myProcess() 中获取请求的正文。 I have a MyProcessHandler class (which extends no class) implementing a myProcess() function.我有一个实现 myProcess() 函数的 MyProcessHandler 类(不扩展任何类)。 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.主要问题在于 XML-RPC 版本:Apache 首先制作了一个名为 helma-xmlrpc 的旧版本,然后将其重构为 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.尽管 Apache 更新了其 XML-RPC 在线文档的某些部分,但其他一些部分仍然引用 helma-xmlrpc,但在每个页面上都没有提示所使用的版本。

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.此外,Apache 表示使用 helma-xmlrpc 的项目只需更新其导入即可使用新版本 org.apache.xmlrpc,对代码没有影响 - 这是完全错误的。 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.所以一旦我明白了这一点,我也明白了大多数处理 Apache XML-RPC 的 Internet 教程使用旧的 helma-xmlrpc 版本,但将它们的导入显示为 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.因此,如果您将其粘贴到您自己的使用 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.我在 Internet 上查找了最新的 org.apache.xmlrpc-implementation-with-servlets 教程,但没有结果。 Hence I decided to use old helma-xmlrpc and all went well.因此我决定使用旧的 helma-xmlrpc 并且一切顺利。 With Helma, the code I posted in my first message now compiles and is right.使用 Helma,我在第一条消息中发布的代码现在可以编译并且是正确的。

As for the link between the listener and MyProcessHandler class, it exists thanks to the addHandler function.至于侦听器和 MyProcessHandler 类之间的链接,它的存在归功于 addHandler 函数。 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.一旦声明了处理程序,当处理指令 server.execute(...) 时,所有带有 methodName 的传入请求(如“myProcess.myFunction”)都会自动重定向到 MyProcessHandler.myFunction()。

For that to work, myFunction() must declare one String input parameter.为此,myFunction() 必须声明一个 String 输入参数。 When myFunction() will be called, this parameter contains the body of the request (extracted by request.getInputStream() in the servlet).当 myFunction() 将被调用时,该参数包含请求的主体(由 servlet 中的 request.getInputStream() 提取)。 myFunction() also has to return something, which will be returned into the byte[] result variable of the servlet. myFunction() 也必须返回一些东西,它会被返回到 servlet 的 byte[] 结果变量中。

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我很好地利用了下面的链接,非常完整,并且只处理了 helma-xmlrpc,没有伪装使用 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.我希望这个答案足够清楚(我的英语口语并不完美......)并且它会帮助其他开发人员理解 Apache XML-RPC。

You can use XmlRpcServletServer (apache xml-rpc 3.1.3):您可以使用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);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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