I have the following:
@RestController
public class MyController {
@PostMapping
MyDto test(@RequestBody MyDto myDto) {
return myDto;
}
@GetMapping
MyDto test2(MyDto myDto) {
return myDto;
}
@Data
static class MyDto {
private String a;
@JsonUnwrapped
private MySecondDto secondDto;
@Data
static class MySecondDto {
private String b;
}
}
}
However:
GET http://localhost:8080?a=a&b=b
returns
{
"a": "a"
}
while
POST http://localhost:8080
{
"a": "a",
"b": "b"
}
returns
{
"a": "a",
"b": "b"
}
so it looks like @JsonUnwrapped and GET mapped Pojos don't work together as expexted.
Any hint on how to use complex nested Pojos to accomodate GET request params ?
I may be late, But spring uses jackson to deserialize the body, while it converts query parameters directly
You can, however, use resolvers or converters to redefine the way your parameters are converted
You can use jackson deserializer and skip query parameters conversion in the following way:
@RequestParam with Map<String, String>. Spring docs says: "If the method parameter is Map<String, String> or MultiValueMap<String, String> and a parameter name is not specified, then the map parameter is populated with all request parameter names and values." https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestParam.html@GetMapping(value = BASE_URL)
public ResponseEntity<List<MyDto>> getDtos(@RequestParam Map<String, String> criteriaMap, Pageable pageable)
getDtos body or write custom service for it.ObjectMapper objectMapper = new ObjectMapper();
MyDto criteria = objectMapper.convertValue(criteriaMap, MyDto.class);
Then your @JsonUnwrapped annotation should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With