Java RestTemplate postForObject返回null,不';我不打发球
我使用springJava RestTemplate postForObject返回null,不';我不打发球,java,spring,Java,Spring,我使用springRestTemplate来访问http服务,该服务在基于浏览器的http客户端上运行良好。但是在我的Java代码中,restemplate返回null。它没有命中服务(我使用调试点进行了测试) 下面是我的rest客户端代码: String url = "http://localhost:8080/webappn/app/decryptPassword"; HttpHeaders headers = new HttpHeaders(); headers.setContentTyp
RestTemplate
来访问http服务,该服务在基于浏览器的http客户端上运行良好。但是在我的Java代码中,restemplate
返回null。它没有命中服务(我使用调试点进行了测试)
下面是我的rest客户端代码:
String url = "http://localhost:8080/webappn/app/decryptPassword";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_PLAIN);
HttpEntity<String> request = new HttpEntity<String>(getConnectionPassword().toString(), headers);
RestTemplate restTemplate = new RestTemplate();
try{
String response = restTemplate.postForObject(url, request, String.class);
this.password = response;
}catch(HttpStatusCodeException e){
utils.writeLoggerError(LOGGER, "Password could not be decrypted");
throw new Exception("Password could not be decrypted");
}
我明白了为什么会这样。我设置了一个身份验证层来过滤所有请求。我在请求中添加了
JSESSIONID
cookie,效果很好
尽管如此,它应该抛出一个异常 您的代码运行良好。你在终点检查结果了吗?我的意思是你从控制器那里得到了空值?尝试从控制器返回硬编码值并检查response@Maxim正如我所提到的,它没有到达终点
@RequestMapping(value="/decryptPassword", method = RequestMethod.POST, produces = "text/plain")
public String decryptPassword(@RequestBody String cipherText){
try{
return myService.decryptPassword(cipherText);
} catch (Exception e){
throw new InternalServerErrorException("Password cannot be decrypted");
}
}