簡體   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