简体   繁体   English

如何获取URL查询字符串值

[英]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&param2=value

some parameters can be empty like this 这样一些参数可以为空

api/asset?param1=value&param2

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.

相关问题 如何从URL获取请求查询字符串 - How to get request query string from the URL 如何使用httpUrlConnection获取url的查询字符串 - How to get a query string for a url using httpUrlConnection , android 获取查询字符串值JAVA - Get Query String Values JAVA 如何使用JSOUP或Coldfusion从URL中删除查询字符串和哈希值? - How to remove the Query String and Hash Values from URL Using JSOUP or Coldfusion? 如何传递包含另一个带有查询字符串的URL的URL查询字符串 - How to pass a url query string containing another url with query strings 在Spring MVC Controller中获取查询字符串值 - Get Query String Values in Spring MVC Controller Rest 获取:如何在 URL 的查询参数中添加“带查询参数的 URL”? - Rest Get: How to add “URL with Query Parameters” in Query Parameters of a URL? 如何让servlet容器从查询字符串而不是URL中读取JSESSIONID - How do I get a servlet container to read JSESSIONID from the query string rather than from the URL JSF2和PrettyFaces ......如何获得原始URL或查询字符串? - JSF2 and PrettyFaces… how does one get the original URL or the query string? 如何将 map 转换为 url 查询字符串? - How to convert map to url query string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM