Always get null response in ResponseEntity when using RestTemplate.postForEntity

14,364

You have to set the responseType, otherwise the RestTemplate will throw away the body of your response. It needs the responseType to find the correct message converter. With a null responseType, the delegate below will be null...

        if (delegate != null) {
            T body = delegate.extractData(response);
            return new ResponseEntity<T>(body, response.getHeaders(), response.getStatusCode());
        }
        else {
            return new ResponseEntity<T>(response.getHeaders(), response.getStatusCode());
        }

With the RestTemplate default constructor, Spring includes just about every converter except for RSS, XML and JSON, which depends on if Rome, JAXB or Jackson is on the classpath. I would set the responseType as String and run it with the debugger to see why it's not finding the correct converter. It's hard for me to say why without seeing the response and headers from the server.

Share:
14,364
MainstreamDeveloper00
Author by

MainstreamDeveloper00

Updated on June 04, 2022

Comments

  • MainstreamDeveloper00
    MainstreamDeveloper00 almost 2 years

    I' m trying to send JSON request using Jackson library from my Android app to the web server but response is always null. I tested it just with the HttpRequest API and all works fine - I've got a response. But now I try to use Spring RestTemplate and I can't receive a result. Here is my code:

    protected Void doInBackground(String... params) {
        LinkedHashMap<String, Object> _map = new LinkedHashMap<String, Object>();
        _map.put("login", "Fred");
        _map.put("password", "pass");
        ObjectMapper _mapper = new ObjectMapper ();
        StringWriter _writer = new StringWriter();
        try {
            _mapper.writeValue(_writer,_map);
        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    
        String _baseURL = "https...."//Address of the server;
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.setContentType(MediaType.APPLICATION_JSON); 
         HttpEntity<String> _entity = new HttpEntity<String>(_writer.toString(),requestHeaders);
        RestTemplate templ = new RestTemplate();
    
        templ.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
        templ.getMessageConverters().add(new StringHttpMessageConverter());
        ResponseEntity<String> _response = templ.postForEntity(_baseURL, _entity,String.class);
        String _Test =  _response.getBody();
    

    So I always have null in _Test. I suspect this is because of https protocol. Can RestTemplate work with https?

    So what's wrong with that code. How to fix this?

    Thanks in advance. I really need a help!

  • MainstreamDeveloper00
    MainstreamDeveloper00 over 11 years
    Yes I' m connecting to https.https: //noticemed.com/app
  • MainstreamDeveloper00
    MainstreamDeveloper00 over 11 years
    I'm using org.springframework.http Class HttpHeaders. Not the standard api. I wrote this in my question
  • MainstreamDeveloper00
    MainstreamDeveloper00 over 11 years
    I've edited my question. I set response type as String.class and add StringHttpMessageConverter() instead of Jackson. But the response is still null.
  • hyness
    hyness over 11 years
    You do not need to add either of those converters, Spring will include them for you (assuming Jackson is on your classpath). I cannot help you anymore without seeing what the server is returning. Are you sure the server is even returning any content and not just a status code? What version of Spring are you using?
  • MainstreamDeveloper00
    MainstreamDeveloper00 over 11 years
    Ok. Thanks for this. I've checked _response.getHeaders().And all OK. I see server type, version of PHP etc.But _response.getBody() return null or [].May be it is because RestTemplate in android don't support https?
  • Roy Clarkson
    Roy Clarkson about 11 years
    hyness, while this is true with standard Spring Framework, the Android version of RestTemplate does not automatically include all the message converters. You can pass true in the constructor to achieve this same result. See reference doc: static.springsource.org/spring-android/docs/1.0.x/reference/‌​…