java - Spring mvc 参数类型如何验证?
怪我咯
怪我咯 2017-04-18 09:15:05
0
2
530
@RequestMapping("/test")
@ResponseBody
public String test(Long id){
    return id + "";
}

当请求传入参数不是数字的时候会返回400 Bad Request如何校验传入的这个参数?

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(2)
左手右手慢动作

Your question is "The current situation is that if the incoming parameter is not a number, it will be returned 400 Bad Request and you don't know why?"

Still, "You want to automatically return 400 Bad Request when the incoming parameter is not a number"

Added:

Can you change it like this? If you are using spring4:

@RequestMapping("/test")
@ResponseBody
public String test(@RequestParam("id") Long id, BindingResult result){
    if (result.hasErrors()) {
        return "返回你想返回的";
    }
    return id + "";
}

I haven’t written for a long time, it should be ok

Added:

It seems to be specified@Valid之类的注解的,你得仔细查查spring的文档,我实在不确定annotation是不是我写的这个@RequestParam(depending on whether your parameters come from the url or other places)

伊谢尔伦

If you are so confused, just accept itObject id 然后第一行代码判断id instanceof Long Just kidding. .
If what is passed in is not a number, it probably won’t go to your method.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!