我正在使用 Spring 框架,我想在 Controller 中获取查询字符串值
api/asset?param1=value¶m2=value
有些参数可以像这样为空
api/asset?param1=value¶m2
这是 Controller 的代码
@RequestMapping(value = "/api/assets", method = RequestMethod.GET)
public @ResponseBody String getAssetList(
@RequestParam("limit") int limit,
@RequestParam("offset") int offset
) {
}
当两个参数都给定时,我可以正常工作,但当一个参数为空时,我无法获取值
最佳答案
默认情况下参数是必需的,但您可以将它们设置为可选。
在这里查看 spring 文档:
Parameters using this annotation are required by default, but you can specify that a parameter is optional by setting @RequestParam's required attribute to false (e.g., @RequestParam(value="id", required=false)).
关于java - 如何获取 URL 查询字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24065555/