-2

in this small project in spring-boot that I created, through a simple get, passing it the fields "firstname" and "username", I get back a message in json with all the information on that particular User. This is my Request class

public class UsersRequest implements Serializable {
    private String firstName;
    private String lastName;
}

I have this in my controller

   public UsersResponse getMyUsers(@RequestBody UsersRequest request) throws IOException {
    //some code
    }

I want to be able to pass null as a value in the "firstName" field, only that when I put the null in the firstName, my get completely deletes the firstName field and does it only with the "lastName"

For my request:

{
   firstName : "" , //i have a deserializer which converts empty strings to null
   lastName : "Treehard"
}

The request that I then see being made is this

Request{"lastName":"Trehard"}

Is there a way to pass null as a value and make it possible to get both fields without deleting any of them?

Jacket
  • 353
  • 6
  • 18

1 Answers1

0

If you pass key or not pass key it will work by adding required attribute , Try to do this way @RequestParam(required = false ,value = "key")String key

KJA
  • 85
  • 5