繁体   English   中英

Springboot:控制器中的@RequestParam读取带有'&'和'#'为空的请求

[英]Springboot : @RequestParam in controller reads the request with '&' and '#' as empty

我有一个在Tomcat上运行的Spring-Boot应用程序。 在其中,我有一个带有请求参数的RestController @RequestParam不会读取作为查询参数传递的'&''#'
例如,如果我输入http://localhost:8080/v1/test?query=&则控制器方法不将&作为值

@RequestMapping(value = "/v1/test", method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
     public String getV2LocationByName(
                                      @RequestParam
                                      String cityName cityName,
                                      @RequestParam(value = LANGUAGE, defaultValue = US_ENGLISH_LOCALE) String language,
                                      HttpServletRequest request) throws InterruptedException, ExecutionException {
--------------
---------------
 System.out.println(cityName);
}

上面的程序为&和#返回空输出

为什么spring限制了这些特殊字符,却没有限制任何其他特殊字符? 另外,如果将查询参数用作“ Andaman&Nicobar”,则查询参数将被读取为“ Andaman”

您需要在前端对特殊字符进行编码。

'$' is '%26'

一方面,在您的@RequestParam("cityName")显式添加名称是一个好习惯

“&”号(&)用于分隔多个请求参数,而#号(#)用于HTML中的书签/锚。

它们都是具有用法的特殊字符,如果不用于这些目的,则需要对其进行编码。

您可以在此处找到有关该主题的更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM