-1

I have an Android app contacting a REST API developed with Spring. The app serializes well an object but the Spring application parses every field as null when they aren't null. What can be wrong? what shall I do?

This is the REST API method signature: cambioPassword(@RequestHeader("Authorization") String header, PasswordChange userData)

Note: both the app and the API uses Gson (I'm not using Jackson as default in the Spring application)

I've tried every kind of constructor and I've changed the visibility of the Java class fields

public class PasswordChange {
    private String name;
    private String currentPassword; // Ciphered field
    private String newPassword; // Ciphered field

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCurrentPassword() {
        return currentPassword;
    }

    public void setCurrentPassword(String currentPassword) {
        this.currentPassword= currentPassword;
    }

    public String getNewPassword() {
        return newPassword;
    }

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }
}

The server should be able to deserialize the PasswordChange class and the fields should not be null as they aren't in the original sent JSON.

1 Answers1

0

Could it be that you are missing the @RequestBody annotation, I expect it to look like:

@PostMapping("/cambio")
cambioPassword(@RequestHeader("Authorization") String header, @RequestBody PasswordChange userData){
    ...
}

See https://www.baeldung.com/spring-request-response-body

  • Oops, my bad. Yes, it is. I've been stuck for a day with this stupid failure (my other endpoints with more than one parameter are ok, but this one lacked the annotation as you say). Thank you! – Alsahm Master of Beasts Aug 14 '19 at 13:54