There is an incoming post request like this:
curl --location --request POST 'http://example.com/some/path' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'prop1=prop1Value' \
--data-urlencode 'prop2[prop2Inner1]=prop2Inner1Value' \
--data-urlencode 'prop2[prop2Inner2][inner3]=inner3Value' \
--data-urlencode 'prop2[prop2Inner2][inner4]=inner4Value'
In an php script i would get the post request data in an associative array like this automatically:
[
'prop1' => 'prop1Value',
'prop2' => [
'prop2Inner1' => 'prop2Inner1Value',
'prop2Inner2' => [
'inner3' => 'inner3Value',
'inner4' => 'inner4Value',
]
]
]
Now im trying to handle this request in java. I can not change the incoming post request format. I thought that i can make a couple of Classes like this
@NonArgumentConstructor
@AllArgumentsConstructor
@Setter
@Getter
class MyRequest {
private String prop1,
private FirstInnerDto prop2;
}
@NonArgumentConstructor
@AllArgumentsConstructor
@Setter
@Getter
class FirstInnerDto {
private String prop2Inner1;
private SecondInnerDto prop2Inner2;
}
@NonArgumentConstructor
@AllArgumentsConstructor
@Setter
@Getter
class SecondInnerDto {
private String inner3;
private String inner4;
}
And than in the controller do something like this, and get an object of class 'MyRequest' filled with request data:
@RequestMapping("/some/path")
public ResponseEntity<SomeResponseDto> myHandler(@ModelAttribute MyRequest request) {
// Do anything here with 'request' object
return ResponseEntity.status(HttpStatus.OK.value()).body(new SomeResponseDto());
}
but i'm getting this error:
"Invalid property 'prop2[prop2Inner1]' of
bean class [com.testapp.java.myapp.Model.Dto.Request.MyRequest]:
Property referenced in indexed property path 'prop2[prop2Inner1]' is neither an array
nor a List nor a Map;
returned value was [FirstInnerDto{prop2Inner1='null', prop2Inner2='null'}]"
Can anyone tell me what is the right way to handle such requests in spring boot?