[英]Spring @RequestParam custom type conversion
How do I create a custom type converter for a boolean parameter in a GET request?如何为 GET 请求中的 boolean 参数创建自定义类型转换器?
For example, I want the allowed values in the GET request to be "oui" and "non" instead of "true" and "false".例如,我希望 GET 请求中的允许值是“oui”和“non”,而不是“true”和“false”。 I followed the Spring documentation on how to do this, and tried the following:
我按照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);
}
}
I also tried this:我也试过这个:
@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);
}
}
Neither of these worked.这些都不起作用。 When supplying the value "oui", I got an HTTP 400 response with the following message:
当提供值“oui”时,我收到 HTTP 400 响应,其中包含以下消息:
Failed to convert value of type 'java.lang.String' to required type 'boolean';
无法将“java.lang.String”类型的值转换为所需类型“boolean”; nested exception is java.lang.IllegalArgumentException: Invalid boolean value [oui]"
嵌套异常是 java.lang.IllegalArgumentException:无效的 boolean 值 [oui]”
Update:更新:
I've also now tried using a Converter:我现在也尝试使用转换器:
@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");
}
}
This "kind of" works, in that it now accepts "oui" and "non", but it does so in addition to "true" and "false".这种“kind of”有效,因为它现在接受“oui”和“non”,但除了“true”和“false”之外它还接受。 How can I get it to accept "oui" and "non" instead of "true" and "false"?
我怎样才能让它接受“oui”和“non”而不是“true”和“false”?
In your first case, your requestParam is a boolean but you bind a B oolean.在您的第一种情况下,您的 requestParam 是boolean但您绑定了一个布尔值。
I've tried with this code我试过这段代码
@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);
}
And it's print它是印刷品
true
真的
when I call localhost:8080/e?flag=oui当我打电话给 localhost:8080/e?flag=oui
can't you map it to an enum?你不能把 map 变成一个枚举吗?
enum BooleanInput {
oui(true),
non(false);
private boolean value;
BooleanInput(boolean value) {
this.value = value;
}
Boolean value() {
return this.value;
}
}
and in the controller,在 controller 中,
@GetMapping("/e")
ResponseEntity<String> showRequestParam(@RequestParam BooleanInput flag) {
return new ResponseEntity<>(flag.value(), HttpStatus.OK);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.