Search code examples
springrestasynchronouspublish-subscribeasynccallback

Call back rest url using spring framework


I writing rest web services using spring framework 4.0 jars. All the get calls and post are working fine.

I wanted to know how to implement an api which works asynchronously. The client should post to this url and wait for the response ; something like call back mechanism between server and client . Server when gets the data posts response to this url.

Thanks in advance


Solution

  • Spring 4.0 framework has an easy solution to this. Changed the Return type of the method DeferredResult . Create an instance of DeferredResult and assign whenever value is assigned to this instance it would return the response to calling request.

         @RequestMapping(method=RequestMethod.GET)
            public DeferredResult<String>  getNewMessage(HttpServletRequest request, HttpServletResponse response)
            {
                DeferredResult<String> deferredResult = new DeferredResult<String>();
                processRequest(deferredResult);         
                return  deferredResult;
            }
    
    void processRequest( DeferredResult<String> result)
    {
    result.setResult("hello world");
    }