RestTemplate postForObject返回null,不会点击服务

时间:2018-06-04 15:22:45

标签: java spring

我正在使用spring RestTemplate来访问一个http服务,该服务可以从基于浏览器的HTTP客户端运行。但在我的Java代码中,RestTemplate返回null。它没有达到服务(我使用调试点测试)。

以下是我的其余客户端代码:

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");
}

response在此代码执行时变为空。

这是其余的服务代码:

@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");
    }
}

1 个答案:

答案 0 :(得分:0)

I figured out why this was happening. I have an authentication layer set up which filters all the requests. I added the JSESSIONID cookie in my request and it worked fine.

Although, it should have thrown an exception.