0

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;
}
  • Please post the full exception stacktrace – Ram Dec 28 '18 at 06:16
  • Seems like the issue is in the response of the local URL that is called in rest template. Please post entire code or upload code on GitHub and share its link. – Dharita Chokshi Dec 28 '18 at 06:28
  • Also show us ResetPasswordDTO object and JSON which you pass to the service – i.merkurev Dec 28 '18 at 06:28
  • Please read what a POJO is: https://www.geeksforgeeks.org/pojo-vs-java-beans/ – Nikhil Dec 28 '18 at 08:22
  • same question asked twice..https://stackoverflow.com/questions/53955401/how-can-i-make-a-put-rest-call-along-with-pojo-using-resttemplate – user3145373 ツ Dec 28 '18 at 08:25
  • Possible duplicate of [How can I make a Put rest call along with POJO using RestTemplate](https://stackoverflow.com/questions/53955401/how-can-i-make-a-put-rest-call-along-with-pojo-using-resttemplate) – user3145373 ツ Dec 28 '18 at 08:26

2 Answers2

2

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.

oldborn
  • 86
  • 2
  • Many projects are using `lombok`. However, using `Lombok` makes it difficult to debug the code. – Prashant Dec 28 '18 at 08:10
  • I agree but you can make some restrictions in your team, in our case we only use lombok in our Entity/Model classes. Since they are pretty much handled by objectmappers, generally we don't need to debug them. – oldborn Dec 28 '18 at 08:14
0

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?

  • public class ResetPasswordDTO implements Serializable { /** * */ private static final long serialVersionUID = -2372400429023166735L; private String password; private String activationCode; } – Ravitosh Khatri Dec 28 '18 at 07:00