【SpringBoot中@RequestParam使用与详解】

SpringBoot中@RequestParam使用与详解

一:@RequestParam使用

1.1-代码实际使用

//请求路径:http://127.0.0.1/user/?name=tom&key1=value1&key2=value2
@RequestMapping(value="/user")
public String getUserBlog(@RequestParam(value="name") String username) {
    return name;
}

1.2-注意事项

1.2.1
如果参数前写了@RequestParam(xxx),
那么前端必须有对应的xxx名字才行
(不管其是否有值,当然可以通过设置该注解的required属性来调节是否必须传),
如果没有xxx名的话,那么请求会出错,报400。
1.2.2
如果参数前不写@RequestParam(xxx)的话,
那么就前端可以有可以没有对应的xxx名字才行,
如果有xxx名的话,那么就会自动匹配;
没有的话,请求也能正确发送。

猜你喜欢

转载自blog.csdn.net/weixin_44188105/article/details/131698596