Spring Boot:控制器中的@RequestParam以“&”和“#”为空读取请求

问题描述 投票:1回答:2

我有一个在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'

java spring spring-boot spring-mvc urlencode
2个回答
1
投票

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

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

您可以找到有关here主题的更多信息。


4
投票

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

这是一个耶稣

只是侧面是一个很好的做法,明确添加你的'$' is '%26'名称

© www.soinside.com 2019 - 2024. All rights reserved.