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);
}
上面的程序returns & 和#
的空输出
为什么 spring 限制这些特殊字符,而不限制任何其他特殊字符?
而且,如果使用查询参数作为 'Andaman&Nicobar',查询参数将读取为 'Andaman'
您需要在前端对您的特殊字符进行编码。
即'$' is '%26'
顺便说一下,在您的 @RequestParam("cityName")
中显式添加名称是个好习惯
&符号用于分隔多个请求参数,
而井号 (#) 用于 HTML 中的 bookmarks/anchors。
它们都是有用途的特殊字符,如果不用于这些目的,则需要对其进行编码。
您可以找到有关主题 here 的更多信息。
我在 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);
}
上面的程序returns & 和#
的空输出为什么 spring 限制这些特殊字符,而不限制任何其他特殊字符? 而且,如果使用查询参数作为 'Andaman&Nicobar',查询参数将读取为 'Andaman'
您需要在前端对您的特殊字符进行编码。
即'$' is '%26'
顺便说一下,在您的 @RequestParam("cityName")
&符号用于分隔多个请求参数, 而井号 (#) 用于 HTML 中的 bookmarks/anchors。
它们都是有用途的特殊字符,如果不用于这些目的,则需要对其进行编码。
您可以找到有关主题 here 的更多信息。