1

Using jackson for serialization / deserialization

<dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>1.9.13</version>
    </dependency>

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>

Spring Controller Class

@RequestMapping(value = { "/updateUsers" }, method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ResponseJson updateUsers(@RequestBody UserProfileBean userProfileBean,  HttpSession reqSession)
    {
//do something
}

Bean Class

public class UserProfileBean {
    List<SearchContactDetails> users;

    public List<SearchContactDetails> getUsers() {
        return users;
    }

    public void setUsers(List<SearchContactDetails> users) {
        this.users = users;
    }
}

public class SearchContactDetails {

    private String userName;
    private String userId;
//getters and setters
    }

Now if i am calling this url from my UI- browser but i am getting a bad request -400

inspecting the JSON which im sending to controller it missed the userName key

so this works if i pass both the parameters in request body

{
  "users": [
    {
      "userName": "test1",
      "userId": "1"
    },
    {
      "userName": "test2",
      "userId": "2"
    }
  ]
}

Fails 400 bad request if i miss the userName, username is an optional field for me but some how it wont't work, is there a way we can declare these fields optional

{
  "users": [
    {
      "userId": "1"
    },
    {
      "userId": "2"
    }
  ]
}
user1697113
  • 683
  • 2
  • 14
  • 24
  • You can send userName as undefined from the client. – Ramesh Kotha Dec 21 '16 at 19:58
  • Well, i though of that, but since the request payload (array of users) is coming from another web-service , i don't think its a good idea to process it again and explicitly set the null fields as undefined. – user1697113 Dec 21 '16 at 21:32

0 Answers0