Make object containing map property to querystring for Java @RequestParam

Viewed 26

This is my class:

@Getter
@Setter
public class Example {
    private boolean taxFree;
    private String email;
    private Map<Enum,String> additionalConstraints;
}

This is my endpoint:

@GetMapping(value = "/search", produces = MediaType.APPLICATION_JSON_VALUE)
public TransformedExample<Example> getPurchases(ExampleSearchQueryModel searchModel,
                                                  BindingResult result) {
    if (result.hasErrors() || searchModel == null) {
        LOGGER.error(result.getAllErrors().toString());
        return null;
    }

    return exampleService.findByExampleSearchModel(searchModel);
}

If i have a class and endpoint like this how would my querystring have to look like so that spring can populate the map field "additionalConstraints" ?

additionalConstraints.EMAIL=test or additionConstraints[EMAIL]=test

always result in the map being mapped to null or a size(0) map. How do i do this correctly?

1 Answers

Instead of using a @GetMapping I'd suggest to use @PostMapping so Spring can map the object directly from the body of the request.

Follow this link

Related