简体   繁体   中英

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

I have a Spring-Boot application running on a Tomcat . Within it, i have a RestController with request param. @RequestParam doesn't read '&' or '#' passed as query parameter.
For example, if I give http://localhost:8080/v1/test?query=& then the controller method doesn't take & as value

@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? And also if use query parameter as 'Andaman&Nicobar', the query param is read as 'Andaman'

You need to encode your special characters on the front-end side.

Ie '$' is '%26'

Just on the side its a good practice to explicitly add the name in your @RequestParam("cityName")

Ampersand (&) is used to separate multiple request parameters, while hashtag (#) is used for bookmarks/anchors in 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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