[英]How to get URL Query String values
I'm using Spring Framework and I want to get query string values in Controller 我正在使用Spring Framework,并且想在Controller中获取查询字符串值
api/asset?param1=value¶m2=value
some parameters can be empty like this 这样一些参数可以为空
api/asset?param1=value¶m2
Here is code for controller 这是控制器的代码
@RequestMapping(value = "/api/assets", method = RequestMethod.GET)
public @ResponseBody String getAssetList(
@RequestParam("limit") int limit,
@RequestParam("offset") int offset
) {
}
I got it working when both parameters are given, but I cannot get values when one parameter is empty 给定两个参数时,它可以正常工作,但是当一个参数为空时,我无法获取值
Parameters are mandatory by default but you can set them as optional. 默认情况下,参数是必需的,但您可以将其设置为可选。
Take a look at spring documentation here: 在这里查看spring文档:
http://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#mvc-ann-requestparam http://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#mvc-ann-requestparam
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 (eg, @RequestParam(value="id", required=false)).
默认情况下,使用此批注的参数是必需的,但是您可以通过将@RequestParam的required属性设置为false(例如,@RequestParam(value =“ id”,required = false))来指定参数是可选的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.