Spring RestTemplate发布回复

我不熟悉Spring RestTemplate。

但对于这个项目,我必须使用Spring RestTemplate发送POST调用来使用rest api。

我正在使用此代码:

String restCall = restTemplate.postForObject(url+restParm, null, String.class); 

这工作正常。

我想要恢复HTTP状态代码(例如:200 OK)。 我怎么能这样做? 谢谢。

您使用postForEntity方法如下…

 ResponseEntity response = restTemplate.postForEntity(url+restParm, null, String.class); HttpStatus status = response.getStatusCode(); String restCall = response.getBody(); 

如其他人所建议的,如果RestTemplate无法获得响应,那将是非常奇怪的。 这根本不是真的。

您只需使用返回a的postForEntity方法

http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/http/ResponseEntity.html

正如文档所示,响应实体具有状态。