【问题标题】:Spring @RequestParam custom type conversionSpring @RequestParam 自定义类型转换
【发布时间】:2022-11-23 02:13:59
【问题描述】:

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

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

@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”的值转换为所需类型 '布尔值';嵌套异常是 java.lang.IllegalArgumentException: 无效的布尔值 [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");
    }
}

这种“有点”有效,因为它现在接受“oui”和“non”,但它确实如此此外到“真”和“假”。我怎样才能让它接受“oui”和“non”代替“真假”?

【问题讨论】:

  • 这回答了你的问题了吗? How to convert spring-boot request parameter
  • @Turing85 不,它没有相当回答问题。请参阅上面的更新。
  • 您可以只添加 truefalse 的案例。
  • @Turing85 不幸的是我没有 Github 帐户,但应用程序很简单。它包含的代码不超过上面显示的代码。我刚刚使用初始化程序创建了一个 Spring Boot 应用程序,添加了上面的代码,所有这些都在 5 分钟内完成。
  • 创建一个 github 帐户大约需要 2 分钟 :)。正如我所说:您可以只添加 truefalse 的案例。

标签: java spring


【解决方案1】:

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

我试过这段代码

@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

【讨论】:

  • 谢谢!就是这样!我为转换器使用了错误的类型。使用 Boolean 而不是 boolean 效果很好。
【解决方案2】:

你不能将它映射到枚举吗?

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

    private boolean value;

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

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

在控制器中,

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

【讨论】:

  • 很好的解决方法,谢谢。
猜你喜欢
  • 2018-03-25
  • 1970-01-01
  • 1970-01-01
  • 2014-04-26
  • 2022-01-22
  • 2019-04-06
  • 2016-11-27
  • 2010-09-09
  • 1970-01-01
相关资源
最近更新 更多