1

my code is like this

@PostMapping("/search/get_search_tips")
public String get_search_tips( @RequestParam("keywords") @NotNull String keyWords) {
    return templateService.getSearchTips(keyWords).toJSONString();
}

Then when I miss the param keywords, it would return

{
    "timestamp": "2023-02-01T10:09:16.283+00:00",
    "status": 400,
    "error": "Bad Request",
    "path": "/template/search/get_search_tips"
}

I have written a handle to match @NotNull, when the code is

@PostMapping("/search/get_search_tips")
public String get_search_tips( @RequestParam("keywords",required = false) @NotNull String keyWords) {
    return templateService.getSearchTips(keyWords).toJSONString();
}

Then when I miss the param keywords, it would return

{
    "code": 1080,
    "msg": "error param",
    "result": [
        {
            "key": "keyWords",
            "msg": "不能为null"
        }
    ]
}

I need the rename param like keywords match to keyWords and want to get the second result, but I didn't want to write the code like

@RequestParam("keywords",required = false) @NotNull

What should I do?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • I think English doesn't fully express your question. – 时间只会一直走 Feb 01 '23 at 10:30
  • 我想用@NotNull来检查参数是否为null,当检查到null的时候会匹配到@RestControllerAdvice自定义处理类的一个处理方法。 但是因为要改参数的名字keywords-> keyWords,所以用了@RequestParam("keywords")注解,这时候参数为null的时候不会触发@NotNull的检查,而是触发了@RequestParam的require=true,没法到我写的处理方法,直接返回400给前端了。 写成@RequestParam("keywords",required = false) @NotNull可以达到我需要的效果,但是这样写感觉很怪,想知道有没有更好的解决办法 – lehaifeng000 Feb 01 '23 at 10:36
  • Does this answer your question? [Convert @RequestParam to lower-case](https://stackoverflow.com/questions/61611568/convert-requestparam-to-lower-case) – 时间只会一直走 Feb 01 '23 at 10:48
  • and here https://stackoverflow.com/questions/8922371/requestparam-value-in-spring-mvc-to-be-case-insensitive – 时间只会一直走 Feb 01 '23 at 10:50
  • In fact, the best way is to use lowercase letters directly – 时间只会一直走 Feb 01 '23 at 10:52
  • 对我好像都不适用,实际上前端传给我的是template_id这样的下划线形式,但是java里用下划线形式的变量不合适,所以希望能改名 – lehaifeng000 Feb 01 '23 at 10:57

0 Answers0