In all my previous post under Spring RestTemplate, I got response as Java objects which were non-generic classes. Below is an example for your reference ResponseEntity<Post> postResponseEntity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, Post.class); System.out.println(postResponseEntity.getBody()); When we call “getBody” method, we get an instance of “Post” class. “Post” is not a generic class. What if we want…… Continue reading Getting generic class instance as response
Tag: Spring RestTemplate
Setting request headers example
In this post under Spring RestTemplate, I will show with example how to set request headers. In our previous posts under Spring RestTemplate, I have covered the below methods1) getForObject2) postForObject3) getForEntity4) put5) postForLocation6) postForEntityetc None of these above methods provide you the option of setting request headers. So to achieve this we use the…… Continue reading Setting request headers example
getForEntity example
In this post under Spring RESTTemplate, I will show with example the purpose of “getForEntity” method available on “RestTemplate” class. In our previous post, we have to make a GET http call, we used “getForObject” method available on “RestTemplate” class. But “getForObject” method only returns response body and not the header information. To get header…… Continue reading getForEntity example
PUT example
In this post under Spring REST Template, I will show with example how to perform http PUT operation. For our example I will be updating a “Post” object with id 1 at resource “http://jsonplaceholder.typicode.com/posts”. Below is the class structure of “Post” class. Post package package7; public class Post { private int id; private String title;…… Continue reading PUT example
postForLocation example
In this post under Spring RestTemplate, I will explain with example the purpose of “postForLocation” method in “RestTemplate” class. The “postForLocation” method is similar to “postForObject” both are used to perform a POST request. The only difference is “postForLocation” method returns a url which points to the location where the newly created resource can be…… Continue reading postForLocation example
postForObject example
In this post under Spring RestTemplate, I will show with example to make a POST http request. To make a POST http request, Spring RestTemplate framework provides “postForObject” method in “RestTemplate” class. This method takes 3 arguments, which are1) url2) Object to be posted3) Response type Below is the complete main code for your reference.…… Continue reading postForObject example
Query Parameter example
In this post under Spring RestTemplate, I will show with example how to pass query paramter. For our example I will perform a GET http request on url “http://jsonplaceholder.typicode.com/posts” with “userId” as query parameter. I will create a url template and then create a map which will contain an entry where key will be “userId”…… Continue reading Query Parameter example
delete example
In this post under Spring RestTemplate, I will show with example how to perform DELETE Http request. To perform DELETE http request, we call “delete” method of “RestTemplate”. This method takes only the url with parameter map as optional. Below is the complete main code for your reference Main class package package3; import org.springframework.web.client.RestTemplate; public…… Continue reading delete example
Path Parameter example
In this post under Spring RestTemplate, I will show with example the purpose and how to use path parameter. If we have to make a call to get the “Post” data with id 1 from “http://jsonplaceholder.typicode.com”. Our url will be http://jsonplaceholder.typicode.com/posts/1 Now if I have to make a call to get the “Post” data with…… Continue reading Path Parameter example
getForObject example
In this post under Spring RestTemplate, I will explain with example the purpose of “getForObject” method. “getForObject” method is provided by RestTemplate to make GET HTTP calls. Below is the main method showing how to use it Main class package package1; import org.springframework.web.client.RestTemplate; public class Example1 { public static void main(String[] args) { RestTemplate restTemplate…… Continue reading getForObject example