简体   繁体   中英

How to establish communication between 2 restful apps?

I have two webapps that were designed to expose restful services via JSON, being both written in Java using SpringMVC + Jackson.

The first app works more like a proxy, and just forwards requests to the second app, which holds the real business logic (unfortunately, I'm not allowed to give up either one of them).

The restful endpoints look pretty much like this:

@Controller
@RequestMapping("rest")
public class ProxyController {
    @RequestMapping(value = "download", method = RequestMethod.POST, produces="application/json", consumes="application/json")
    @ResponseBody
    public DownloadResponse download() {
        // invoke BackendController by some mean
    }
}

@Controller
@RequestMapping("rest")
public class BackendController {
    @RequestMapping(value = "download", method = RequestMethod.POST, produces="application/json", consumes="application/json")
    @ResponseBody
    public DownloadResponse download() {
        // do some business logic
        return new DownloadResponse();
    }
}

My first idea was to use an HttpClient to fire a POST from ProxyController to BackendController, and I guess it would pretty much work.

I was wondering if anyone would come up with a better idea. Does anyone know whether SpringMVC might make life a little bit easier?

Thanks a lot for any comments

I went by JB Nizet's suggestion and it looks way better! Thanks a lot for the suggestion!

Here's the final code:

public class ProxyController {
    public DownloadResponse download() {
        RestTemplate template = new RestTemplate();
        return template.postForObject("http://<backend-url>/rest/download.do", request, DownloadResponse.class);
    }
}

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