Reputation: 459
I have a requirement to write java APIs for one of my spring 3 web application.I should be able to do all actions that I perform using my web UI, through these APIs as well.I have controller methods decorated with @RequestMapping. I recently heard that, these methods can be exposed as Restful service that can be accessed via rest client with minimal modification. I was just wondering the recommended ways to create Rest client for spring3 services. I do not want to use any spring dependencies in these java APIs. I should be able to upload files using these APIs as I have multipart/form-data implemented in my spring application. Can somebody helps me to choose the best way to develop RestClients in java for spring applications?
I have below HTTP implementations :
Java - uses the HTTP implementation provided by the JVM. This has some limitations in comparison with the HttpClient implementations.
HTTPClient3.1 - uses Apache Commons HttpClient 3.1.
HTTPClient4 - uses Apache HttpComponents HttpClient 4.x.
Pls let me know your suggestion.
Upvotes: 1
Views: 1581
Reputation: 1128
Personally, I use org.springframework.web.client.RestClient, since you're already using Spring. They do a decent job of managing what you need, just keep in mind their exception and no content handling sucks. The only modification I had to make was to override their doExecute(URI, HttpMethod, RequestCallback, ResponseExtractor<T>)
and add:
if (response.getStatusCode().equals(HttpStatus.NO_CONTENT)) {
return null;
}
before
if (!getErrorHandler().hasError(response)) { ...
Other than that little quirk (and some custom exception handling), it's been a great tool.
Upvotes: 1