Reputation: 13
Unable to send Pojo via RestTemplate PUT request.
I have a rest service which I need to call from other application. The service is :
@RequestMapping(value = RESET_USER_PASSWORD_URL, method = RequestMethod.PUT, produces = APP_JSON)
public SuccessResponse resetUserPassword(@RequestBody ResetPasswordDTO resetPasswordDTO) throws GenericException {
logger.info("--->reset Password");
return new SuccessResponse(userservice.resetUserPassword(resetPasswordDTO));
}
I am calling above service using RestTemplate, for this I need to send a POJO along with the PUT request. The code using RestTemplate is:
public ResponseEntity<SuccessResponse> resetUserPassword(ResetPasswordDTO resetPasswordDTO)
throws ServiceGenericException {
ResponseEntity<SuccessResponse> ssoUserResponse = null;
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<ResetPasswordDTO> requestEntity = new HttpEntity<ResetPasswordDTO>(resetPasswordDTO,headers);
ssoUserResponse = restTemplate.exchange("http://localhost:5858/api/unsecured/resetpassword", HttpMethod.PUT, requestEntity,
SuccessResponse.class);
return ssoUserResponse;
}
I am not able to make a call. I am getting below exception: org.springframework.web.client.HttpClientErrorException: 400 null.
The POJO I want to send:
public class ResetPasswordDTO implements Serializable {
private static final long serialVersionUID = -2372400429023166735L;
private String password;
private String activationCode;
}
Upvotes: 0
Views: 1044
Reputation: 96
Spring can't parse your json into POJO because variables declared as private and you don't have any getters/setters. You need to make them public or add getters/setters to your ResetPasswordDTO class.
Also, I strongly suggest you to look Lombok, it makes things like that very easy.
Upvotes: 2
Reputation: 1
HttpClientErrorException is thrown when a 4xx error is received. So if the request you send is wrong either setting header or sending wrong data, you could receive this exception. You should check your request and verify the details of POJO being sent from you side while calling the API.
If you are still unable to pinpoint the issue please share your POJO, request details and full stacktrace.
For more details on HTTP 400 error code you can refer 400 BAD request HTTP error code meaning?
Upvotes: 0