1

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.

ASChakkalakal
  • 459
  • 2
  • 8
  • 18

1 Answers1

1

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.

MaddHacker
  • 1,118
  • 10
  • 17
  • Thank you so much for the details. I was wondering if RestTemplate supports multipart file upload. I need to send files along with HTTPRequest – ASChakkalakal Jun 08 '12 at 10:36
  • http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/client/RestTemplate.html https://jira.springsource.org/browse/SPR-5904 yup – MaddHacker Jun 08 '12 at 11:32
  • You can also look here: http://stackoverflow.com/questions/4118670/sending-multipart-file-as-post-parameters-with-resttemplate-requests – MaddHacker Jun 08 '12 at 11:33
  • I have one more question: I need to perform an action upon a rest call. Which HTTP verb is suitable to perform an action? According to REST specification : To create a resource on the server, use POST. To retrieve a resource, use GET. To change the state of a resource or to update it, use PUT. To remove or delete a resource, use DELETE. – ASChakkalakal Jun 11 '12 at 06:50
  • But I need to perform an application specific action. I was just wondering if an application specific action is against REST principle. In other words, if I need to perform a specific action say, process an uploaded file and covert it into a different format and send to a remote location, should nt I use REST? I may not be talking in the correct context of REST. All I need to do is "I should create java APIs for my server, users should be able to perform action as if they perform via WEBUI " – ASChakkalakal Jun 11 '12 at 06:59