Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring/RestTemplate - PUT entity to server

Please look at this simple code:

final String url = String.format("%s/api/shop", Global.webserviceUrl);

RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

HttpHeaders headers = new HttpHeaders();
headers.set("X-TP-DeviceID", Global.deviceID);
HttpEntity entity = new HttpEntity(headers);

HttpEntity<Shop[]> response = restTemplate.exchange(url, HttpMethod.GET, entity, Shop[].class);
shops = response.getBody();

As you can see, above code is intended to GET list of shops from server (in json format) and map response to array of Shop objects. Now I need to PUT new shop, for example as /api/shop/1. Request entity should have exactly the same format as returned one.

Should I add /1 to my url, create new Shop class object, with all fields filled with my values I want to put and then use exchange with HttpMethod.PUT?

Please, clarify it for me, I'm beginner with Spring. Code example would be appreciated.

[edit] I'm double confused, because I just noticed also method RestTemplate.put(). So, which one should I use? Exchange or put()?

like image 802
user1209216 Avatar asked Oct 20 '15 16:10

user1209216


People also ask

Is RestTemplate being deprecated?

WebClient offers support for both synchronous and asynchronous HTTP requests and streaming scenarios. Therefore, RestTemplate will be marked as deprecated in a future version of the Spring Framework and will not contain any new functionalities. RestTemplate is based on a thread-per-request model.

What is difference between getForObject and getForEntity?

So, basically the difference is that, getForEntity() provides more metadata than getForObject().

What is the use of HttpEntity in RestTemplate?

Use HttpEntity to wrap the request object. Here, we wrap the Product object to send it to the request body. Provide the URL, HttpMethod, and Return type for exchange() method.

How do you POST data with RestTemplate?

Posting JSON With postForObject. RestTemplate's postForObject method creates a new resource by posting an object to the given URI template. It returns the result as automatically converted to the type specified in the responseType parameter.


1 Answers

You could try something like :

    final String url = String.format("%s/api/shop/{id}", Global.webserviceUrl);

    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

    HttpHeaders headers = new HttpHeaders();
    headers.set("X-TP-DeviceID", Global.deviceID);
    Shop shop= new Shop();
    Map<String, String> param = new HashMap<String, String>();
    param.put("id","10")
    HttpEntity<Shop> requestEntity = new HttpEntity<Shop>(shop, headers);
    HttpEntity<Shop[]> response = restTemplate.exchange(url, HttpMethod.PUT, requestEntity, Shop[].class, param);

    shops = response.getBody();

the put returns void whereas exchange would get you a response, the best place to check would be documentation https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html

like image 158
cpd214 Avatar answered Sep 30 '22 18:09

cpd214