简体   繁体   中英

@RequestParam not mapping entire string

I have a Get endpoint that accepts a query string as request param . The issue with the endpoint it that the request param string can contain characters like ?,/ etc which is causing issues.Is there any way to map a string which contains ?,/ etc to a variable in a rest controller ?

Have already tried using @PathVariable instead of using @RequestParam but it is still not reading the value correctly. When using @PathVariable to map the uri , the ? is getting omitted. When using @RequestParam, it is throwing an Exception.Stacktrace is provided.

@GetMapping("/getResult")
public @ResponseBody ResponseEntity<String> getData(@RequestParam(value="text") String text) {
    //Call to service layer passing text as a param
    return new ResponseEntity<String>("GET Response", HttpStatus.OK);
}

URL : 'localhost/getResult?text=How you doing?'

Stacktrace:

java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens Error parsing HTTP request header Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.

java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:422) ~[tomcat-embed-core-8.5.14.jar:8.5.14] at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:683) ~[tomcat-embed-core-8.5.14.jar:8.5.14] at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.14.jar:8.5.14] at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861) [tomcat-embed-core-8.5.14.jar:8.5.14] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455) [tomcat-embed-core-8.5.14.jar:8.5.14] at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.14.jar:8.5.14] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_05] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_05] at org.apache.tomcat.util.thr eads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.14.jar:8.5.14] at java.lang.Thread.run(Thread.java:745) [na:1.8.0_05]

For the expected result , i would like to map the string "How you doing?" to a path variable or query param variable with the entire string intact, instead of the '?' getting omitted.

If you are calling the url direct from browser or curl or postman. you should use encoding for different special characters like for space encoding is %20. That means when you write "how are you?" this would be "how%20are%20you%3F" in the url. Please refer to this site for more understanding.

And If the call is from any programming language. please use URL encoders.

Providing a Java solution:
You can use the URLEncoder to achieve what you want:

URLEncoder enc = new URLEncoder();
enc.encode(String toEncode, String charset)

There is also a method that accepts only the string to be encoded, but this one is deprecated.

If you are calling from a Javascript client, you can use encodeURIComponent() :

https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent

Encode the question mark(?) as %3F and you're good to go.

There's no other way you can send question mark in query as it is a special character .

You need to encode the parameters with special characters in the URL. You can use encodeURIComponent from front end .

If you are using postman, you need to set following in Pre-request scripts,

var encoded = encodeURIComponent(postman.getEnvironmentVariable("text"));
postman.setEnvironmentVariable("encoded text", encoded);

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