Case 1) Create Spring Boot Project With spring-boot-starter-web, lombok and jackson-databind, jackson-core, jackson-annotations 2) Create Models
@Getter @Setter @NoArgsConstructor @AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
class ModelClass { NestedModelClass data; }
@Getter @Setter @NoArgsConstructor @AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
class NestedModelClass {
Long id;
String status;
....
}
3) Create Post-Mapping endpoint that consumes MediaType.APPLICATION_FORM_URLENCODED_VALUE
@PostMapping(value = "endpoint", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public void endpoint(@ModelAttribute("payload") ModelClass body) {
NestedModelClass event = objectMapper.convertValue(body.getData(), NestedModelClass.class);
....
}
4) Post Data sample curl --location --request POST '/endpoint' \ --header 'Content-Type: application/x-www-form-urlencoded' \ --data-urlencode 'data[id]=1000' \ --data-urlencode 'data[status]=success' ```
Error: org.springframework.beans.InvalidPropertyException: Invalid property 'data[id]' of bean class [...ModelClass]: Property referenced in indexed property path 'data[id]' is neither an array nor a List nor a Map; returned value was [...NestedModelClass@....]
Comment From: wilkinsona
@vivekitis We have already chatted about this on Gitter. As discussed there, if you can't alter the data[id]
format of the request, you should use a Map
rather than your ModelClass
POJO.