I'm having a problem using Spring restTemplate.
For now i'm sending a PUT request for a restful service and that restful service send me back important informations in response.
The question is that restTemplate.put are a void method and not a string so i can't see that response.
Following some answers i've change my method and now i'm using restTemplate.exchange, here are my method:
public String confirmAppointment(String clientMail, String appId) { String myJsonString = doLogin(); Response r = new Gson().fromJson(myJsonString, Response.class); // MultiValueMap<String, String> map; // map = new LinkedMultiValueMap<String, String>(); // JSONObject json; // json = new JSONObject(); // json.put("status","1"); // map.add("data",json.toString()); String url = getApiUrl() + "company/" + getCompanyId() + "/appointment/" + appId + "?session_token=" + r.data.session_token; String jsonp = "{\"data\":[{\"status\":\"1\"}]}"; RestTemplate rest = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.add("Content-Type", "application/json"); headers.add("Accept", "*/*"); HttpEntity<String> requestEntity = new HttpEntity<String>(jsonp, headers); ResponseEntity<String> responseEntity = rest.exchange(url, HttpMethod.PUT, requestEntity, String.class); return responseEntity.getBody().toString(); }
Using the method above, i receive a 400 Bad Request
I know my parameters, url and so, are just fine, cause i can do a restTemplate.put request like this:
try { restTemplate.put(getApiUrl() + "company/" + getCompanyId() + "/appointment/" + appId + "?session_token=" + r.data.session_token, map); } catch(RestClientException j) { return j.toString(); }
The problem (like i said before) is that the try/catch above does not return any response but it gives me a 200 response.
So now i ask, what can be wrong?
For example, the method getForObject() will perform a GET and return an object. getForEntity() : executes a GET request and returns an object of ResponseEntity class that contains both the status code and the resource as an object. getForObject() : similar to getForEntity() , but returns the resource directly.
HttpHeaders headers = new HttpHeaders(); headers. set("Header", "value"); headers. set("Other-Header", "othervalue"); ... HttpEntity<Void> requestEntity = new HttpEntity<>(headers); ResponseEntity<String> response = restTemplate.
RestTemplate Methods postForLocation will do a POST, converting the given object into a HTTP request, and returns the response HTTP Location header where the newly created object can be found.
Here's how you can check the response to a PUT. You have to use template.exchange(...) to have full control / inspection of the request/response.
String url = "http://localhost:9000/identities/{id}"; Long id = 2l; String requestBody = "{\"status\":\"testStatus2\"}"; HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity<String> entity = new HttpEntity<String>(requestBody, headers); ResponseEntity<String> response = template.exchange(url, HttpMethod.PUT, entity, String.class, id); // check the response, e.g. Location header, Status, and body response.getHeaders().getLocation(); response.getStatusCode(); String responseBody = response.getBody();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With