I am using spring RestTemplate
for hitting a http service which works fine from a browser based HTTP client. But in my Java code, RestTemplate
returns null. It doesn't hit the service (I tested using a debug point).
Below is my rest client code:
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
is coming null when this code executes.
This is the rest service code:
@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");
}
}
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.