简体   繁体   中英

Make object containing map property to querystring for Java @RequestParam

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM