java 使用 RestTemplate.postForEntity 时总是在 ResponseEntity 中得到空响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13936649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Always get null response in ResponseEntity when using RestTemplate.postForEntity
提问by MainstreamDeveloper00
I' m trying to send JSON request using Hymanson 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:
我正在尝试使用 Hymanson 库从我的 Android 应用程序向 Web 服务器发送 JSON 请求,但响应始终为空。我仅使用 HttpRequest API 对其进行了测试,一切正常 - 我得到了响应。但是现在我尝试使用 Spring RestTemplate 并且我无法收到结果。这是我的代码:
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?
所以我在_Test 中总是有空值。我怀疑这是因为 https 协议。RestTemplate 可以与 https 一起使用吗?
So what's wrong with that code. How to fix this?
那么这段代码有什么问题。如何解决这个问题?
Thanks in advance. I really need a help!
提前致谢。我真的需要帮助!
采纳答案by hyness
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...
您必须设置 responseType,否则 RestTemplate 将丢弃您的响应正文。它需要 responseType 才能找到正确的消息转换器。使用 null responseType,下面的委托将为 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 Hymanson 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.
使用 RestTemplate 默认构造函数,Spring 包含几乎所有转换器,除了 RSS、XML 和 JSON,这取决于 Rome、JAXB 或 Hymanson 是否在类路径上。我会将 responseType 设置为 String 并使用调试器运行它以查看为什么找不到正确的转换器。我很难说为什么没有看到来自服务器的响应和标头。
回答by roboto1986
Typo or are you connecting to an https port?
打字错误还是您要连接到 https 端口?
String _baseURL = "https...."//Address of the server;
String _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 设备的所有流量。