简体   繁体   English

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

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

I have a Spring-Boot application running on a Tomcat . 我有一个在Tomcat上运行的Spring-Boot应用程序。 Within it, i have a RestController with request param. 在其中,我有一个带有请求参数的RestController @RequestParam doesn't read '&' or '#' passed as query parameter. @RequestParam不会读取作为查询参数传递的'&''#'
For example, if I give http://localhost:8080/v1/test?query=& then the controller method doesn't take & as value 例如,如果我输入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);
}

The above program returns empty output for & and # 上面的程序为&和#返回空输出

Why does spring limit these special characters , but it is not limiting any other special characters? 为什么spring限制了这些特殊字符,却没有限制任何其他特殊字符? And also if use query parameter as 'Andaman&Nicobar', the query param is read as 'Andaman' 另外,如果将查询参数用作“ Andaman&Nicobar”,则查询参数将被读取为“ Andaman”

You need to encode your special characters on the front-end side. 您需要在前端对特殊字符进行编码。

Ie '$' is '%26' '$' is '%26'

Just on the side its a good practice to explicitly add the name in your @RequestParam("cityName") 一方面,在您的@RequestParam("cityName")显式添加名称是一个好习惯

Ampersand (&) is used to separate multiple request parameters, while hashtag (#) is used for bookmarks/anchors in HTML. “&”号(&)用于分隔多个请求参数,而#号(#)用于HTML中的书签/锚。

They are both special characters with usage and they need to be encoded if they are not to be used for these purposes. 它们都是具有用法的特殊字符,如果不用于这些目的,则需要对其进行编码。

You can find more information on the topic here . 您可以在此处找到有关该主题的更多信息。

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

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