简体   繁体   English

使包含 map 属性的 object 成为 Java @RequestParam 的查询字符串

[英]Make object containing map property to querystring for Java @RequestParam

This is my class:这是我的 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"?如果我有一个 class 和这样的端点,我的查询字符串将如何显示,以便 spring 可以填充 map 字段“additionalConstraints”?

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

always result in the map being mapped to null or a size(0) map.总是导致 map 被映射到 null 或 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.我建议不要使用@PostMapping ,而是使用@GetMapping ,这样 Spring 可以直接从请求正文中 map object 。

Follow this link按照这个链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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