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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you so much for the details. I was wondering if RestTemplate supports multipart file upload. I need to send files along with HTTPRequest
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.
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 "

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.