简体   繁体   English

使用RestTemplate.postForEntity时,始终在ResponseEntity中获得空响应

[英]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. 我正在尝试使用Jackson库将JSON请求从我的Android应用发送到Web服务器,但响应始终为null。 I tested it just with the HttpRequest API and all works fine - I've got a response. 我只是使用HttpRequest API进行了测试,并且一切正常-我得到了回应。 But now I try to use Spring RestTemplate and I can't receive a result. 但是现在我尝试使用Spring RestTemplate,但无法收到结果。 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. 因此,我在_Test中始终为null。 I suspect this is because of https protocol. 我怀疑这是因为https协议。 Can RestTemplate work with https? RestTemplate可以与https一起使用吗?

So what's wrong with that code. 那么该代码有什么问题。 How to fix this? 如何解决这个问题?

Thanks in advance. 提前致谢。 I really need a help! 我真的需要帮助!

You have to set the responseType, otherwise the RestTemplate will throw away the body of your response. 您必须设置responseType,否则RestTemplate将丢弃响应的主体。 It needs the responseType to find the correct message converter. 它需要responseType来找到正确的消息转换器。 With a null responseType, the delegate below will be null... 如果responseType为null,则下面的委托将为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. 使用RestTemplate的默认构造函数,Spring几乎包括除了RSS,XML和JSON之外的所有转换器,这取决于类路径中是Rome,JAXB还是Jackson。 I would set the responseType as String and run it with the debugger to see why it's not finding the correct converter. 我将responseType设置为String并与调试器一起运行,以查看为什么找不到正确的转换器。 It's hard for me to say why without seeing the response and headers from the server. 对于我来说很难说为什么不查看服务器的响应和标头。

Typo or are you connecting to an https port? 错字还是您要连接到https端口?

String _baseURL = "https...."//Address of the server; 字符串_baseURL =“ https ....” //服务器的地址;

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. 我这样做的一种简单方法是使笔记本电脑成为临时网络并连接一个Android设备,然后,您应该能够使用诸如Wireshark的数据包嗅探器监视来自android设备的所有流量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Mockito 模拟 restTemplate.postForEntity - How to mock restTemplate.postForEntity using Mockito 为什么我在 restTemplate.postforEntity 之后的值为 null - Why is my value after restTemplate.postforEntity is null Mockito mocking restTemplate.postForEntity - Mockito mocking restTemplate.postForEntity 当实体具有与预期不同的字段名称时如何调用restTemplate.postForEntity - how to call restTemplate.postForEntity when entity is having different field name than expected org.springframework.web.client.HttpClientErrorException 400 RestTemplate.postForEntity - org.springframework.web.client.HttpClientErrorException 400 RestTemplate.postForEntity 使用 RestTemplate 时如何映射 ResponseEntity 中的响应对象? - How response objects in ResponseEntity are mapped while using RestTemplate? 返回ResponseEntity和Http错误时,RestTemplate.postForObject返回Null - RestTemplate.postForObject Returns Null When ResponseEntity and Http Error Are Returned RestTemplate 响应中的错误消息始终为“null” - Error message always "null" from RestTemplate response RestTemplate Junit:无法调用“org.springframework.http.ResponseEntity.getBody()”,因为“response”为空 - RestTemplate Junit: Cannot invoke "org.springframework.http.ResponseEntity.getBody()" because "response" is null 应用Mockito时,返回restTemplate.exchange(url,HttpMethod.POST,entity1,Response.class))空对象,而不是响应实体存根数据 - restTemplate.exchange(url,HttpMethod.POST,entity1,Response.class)) is returned null object instead of responseEntity stubbed data when mockito applied
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM