繁体   English   中英

Spring @RequestParam 自定义类型转换

[英]Spring @RequestParam custom type conversion

如何为 GET 请求中的 boolean 参数创建自定义类型转换器?

例如,我希望 GET 请求中的允许值是“oui”和“non”,而不是“true”和“false”。 我按照Spring 文档了解如何执行此操作,并尝试了以下操作:

@RestController
public class ExampleController {
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Boolean.class, new CustomBooleanEditor("oui", "non", true));
    }

    @GetMapping("/e")
    ResponseEntity<String> showRequestParam(@RequestParam boolean flag) {
        return new ResponseEntity<>(String.valueOf(flag), HttpStatus.OK);
    }
}

我也试过这个:

@RestController
public class DemoController {
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.addCustomFormatter(new Formatter<Boolean>() {
            @Override
            public Boolean parse(String text, Locale locale) throws ParseException {
                if ("oui".equalsIgnoreCase(text)) return true;
                if ("non".equalsIgnoreCase(text)) return false;
                throw new ParseException("Invalid boolean parameter value '" + text + "'; please specify oui or non", 0);
            }

            @Override
            public String print(Boolean object, Locale locale) {
                return String.valueOf(object);
            }
        }, Boolean.class);
    }

    @GetMapping("/r")
    ResponseEntity<String> showRequestParam(@RequestParam(value = "param") boolean param) {
        return new ResponseEntity<>(String.valueOf(param), HttpStatus.OK);
    }
}

这些都不起作用。 当提供值“oui”时,我收到 HTTP 400 响应,其中包含以下消息:

无法将“java.lang.String”类型的值转换为所需类型“boolean”; 嵌套异常是 java.lang.IllegalArgumentException:无效的 boolean 值 [oui]”

更新:

我现在也尝试使用转换器:

@Component
public class BooleanConverter implements Converter<String, Boolean> {
    @Override
    public Boolean convert(String text) {
        if ("oui".equalsIgnoreCase(text)) return true;
        if ("non".equalsIgnoreCase(text)) return false;
        throw new IllegalArgumentException("Invalid boolean parameter value '" + text + "'; please specify oui or non");
    }
}

这种“kind of”有效,因为它现在接受“oui”和“non”,但除了“true”和“false”之外它还接受。 我怎样才能让它接受“oui”和“non”而不是“true”和“false”?

在您的第一种情况下,您的 requestParam 是boolean但您绑定了一个布尔值。

我试过这段代码

@InitBinder
protected void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(Boolean.class, new CustomBooleanEditor("oui", "non", true));
}

@GetMapping("/e")
ResponseEntity<String> showRequestParam(@RequestParam(value="flag") Boolean flag) {
    return new ResponseEntity<>(String.valueOf(flag), HttpStatus.OK);
}

它是印刷品

真的

当我打电话给 localhost:8080/e?flag=oui

你不能把 map 变成一个枚举吗?

enum BooleanInput {
    oui(true),
    non(false);

    private boolean value;

    BooleanInput(boolean value) {
        this.value = value;
    }

    Boolean value() {
        return this.value;
    }
}

在 controller 中,

    @GetMapping("/e")
    ResponseEntity<String> showRequestParam(@RequestParam BooleanInput flag) {
        return new ResponseEntity<>(flag.value(), HttpStatus.OK);
    }

暂无
暂无

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

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