0

I have a Class

public class TestClass {
  private String name;
  private String id;
  private String accountID;
}

Post Method:

@RequestMapping(value="/testclass", method=RequestMethod.POST)
public void create(@RequestBody TestClass testClass) {
  //implementation using testClass
}

This works fine until the body in the request is as requires but When a POST call with body having multiple values of a variable, the @RequestBody takes the last given value of the variable. For Example:

POST call Body: 
{
"name":"qwerty",
"id":"qw123"
"accountID":"111111",
"accountID":"222222",
"accountID":"333333",
"accountID":"444444"
}

then the testClass object has values name=qwerty,id=qw123,accountID=444444

Thus, this allows any kind of request with any number of values in the body to be processed. Is it possible to identify this multiple values in the request so as to validate all the request prior to the implementation?

I.e I want the request to fail before reaching the code. I want to process the request only if it has single values. Also, it's not just about the given variables, the request may contain other variables as well , but the requestbody simply ignores it and takes the relevant variables alone. I want it to fail in that case too –

  • 1
    Change `accountID` to a List – Nicholas K Aug 22 '18 at 08:49
  • 1
    Why don't you make `List accountID;`? – Emre Savcı Aug 22 '18 at 08:49
  • I don't think the question is how to access all of those values but instead how to validate the JSON in the request to be sure that every key appears only once before processing it. – anothernode Aug 22 '18 at 08:51
  • So you want the request to **fail** before reaching your code, if the request body has multiple entries, did I understand that correctly? – Benjamin Maurer Aug 22 '18 at 08:52
  • @BenjaminMaurer That's at least how I interpret the question. To be precise: if the request body has multiple entries _for the same key_. – anothernode Aug 22 '18 at 08:53
  • Put @Valid before your parameter in the method. – huzeyfe Aug 22 '18 at 08:57
  • yes, I want the request to fail before reaching the code. I want to process the request only if it has single values. Also, it's not just about the given variables, the request may contain other variables as well , but the requestbody simply ignores it and take the relevent variables alone. I want it fail in that case too – LakshmiPavani Aug 22 '18 at 08:58
  • 3
    Possible duplicate of [How to use Jackson to validate duplicated properties?](https://stackoverflow.com/questions/37152629/how-to-use-jackson-to-validate-duplicated-properties) – Arnaud Aug 22 '18 at 09:00
  • @Arnaud thanks for pointing out the usage of Jackson. But since I am not doing any conversion from JSON to object, I could not use Jackson for validation. The requestbody itself converts the JSON into the object. That's where i am stuck as i have no control over what values the object should take and when it should fail. This is what i understand according to my knowledge, if it is still possible to use the Jackson for this, please let me know. – LakshmiPavani Aug 22 '18 at 10:06
  • Spring uses Jackson, and you have ways to customize the Jackson mapper it will use, see here : https://stackoverflow.com/questions/7854030/configuring-objectmapper-in-spring – Arnaud Aug 22 '18 at 10:16
  • 1
    @Arnaud Thanks for the help, customized Jackson mapper worked! – LakshmiPavani Aug 27 '18 at 05:29

0 Answers0