当URL后面可能出现若干个不确定的参数如:
http://localhost:8000/api/search?content=车
http://localhost:8000/api/search?content=车&searchType=baseData
这种情况,如果?后面的所有参数个数可以确定,那么直接在控制器方法中把所有参数写上,如下:
allSearch(@RequestParam String content, @RequestParam String searchType, @RequestParam String dataType) {
但是当前端只发送content参数时有可能会出现下面的问题:
parameter 'searchType' is not present
这个时候只需要,设置每个参数的默认值就可以解决了嗷~
allSearch(@RequestParam(defaultValue="null") String content, @RequestParam(defaultValue="null") String searchType, @RequestParam(defaultValue="null") String dataType) {
当然,这只是一种比较笨的实现方式(有点类似于重载了),我看到网上有人也在用如下的方法,可以借鉴,但是这里我就先不做尝试了,记得有这个方法就行
- 1. 重载,多重载几个方法,尽可能的满足参数的个数。显然这不是什么好办法。
- 2. 将参数作为一个数组传入。