MainstreamDeveloper00
MainstreamDeveloper00

Reputation: 8556

Always get null response in ResponseEntity when using RestTemplate.postForEntity

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!

Upvotes: 3

Views: 15723

Answers (2)

hyness
hyness

Reputation: 4905

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.

Upvotes: 1

roboto1986
roboto1986

Reputation: 625

Typo or are you connecting to an https port?

String _baseURL = "https...."//Address of the server;

I think you should monitor the port you are trying to connect to and see if there is a connection even established. One easy way I do that is to make a laptop an ad-hoc network and have an Android device connect to it and then, you should be able to monitor all traffic from your android device with a packet sniffer like wireshark.

Upvotes: 0

Related Questions