Get STRING response from restTemplate.put

52,793

Solution 1

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();

Solution 2

You can use the Header to send something in brief to your clients. Or else you can use the following approach as well.

restTemplate.exchange(url, HttpMethod.PUT, requestEntity, responseType, ...)

You will be able to get a Response Entity returned through that.

Share:
52,793
Alexandre
Author by

Alexandre

Back-end PHP/JS Web Developer at Lelak;

Updated on July 09, 2022

Comments

  • Alexandre
    Alexandre almost 2 years

    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?