简体   繁体   English

@RequestParam没有映射整个字符串

[英]@RequestParam not mapping entire string

I have a Get endpoint that accepts a query string as request param . 我有一个Get端点,它接受一个查询字符串作为请求参数。 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 ? 端点的问题是请求参数字符串可以包含像?,/ etc这样的字符,这会导致问题。有没有办法将包含?,/ etc的字符串映射到休息控制器中的变量?

Have already tried using @PathVariable instead of using @RequestParam but it is still not reading the value correctly. 已经尝试过使用@PathVariable而不是使用@RequestParam,但它仍然没有正确读取值。 When using @PathVariable to map the uri , the ? 当使用@PathVariable映射uri时,? is getting omitted. 正在被省略。 When using @RequestParam, it is throwing an Exception.Stacktrace is provided. 使用@RequestParam时,它会抛出Exception.Stacktrace。

@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. java.lang.IllegalArgumentException:方法名称中找到无效字符。 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. HTTP方法名称必须是令牌错误解析HTTP请求标头注意:将在DEBUG级别记录更多出现的HTTP标头解析错误。

java.lang.IllegalArgumentException: Invalid character found in method name. java.lang.IllegalArgumentException:方法名称中找到无效字符。 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 HTTP方法名称必须是org.apache.coyote上的org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:422)〜[tomcat-embed-core-8.5.14.jar:8.5.14]中的标记。 http:http://www.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] 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] 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 '?' 到一个路径变量或查询param变量,整个字符串完整,而不是'?' 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. 你应该对不同的特殊字符使用编码,比如空格编码是%20。 That means when you write "how are you?" 这意味着当你写“你好吗?” this would be "how%20are%20you%3F" in the url. 这将是网址中“如何%20are%20you%3F” Please refer to this site for more understanding. 有关更多信息,请参阅此站点

And If the call is from any programming language. 如果呼叫来自任何编程语言。 please use URL encoders. 请使用URL编码器。

Providing a Java solution: 提供Java解决方案:
You can use the URLEncoder to achieve what you want: 您可以使用URLEncoder来实现您想要的:

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() : 如果您从Javascript客户端调用,则可以使用encodeURIComponent()

https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/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. question mark(?)编码为%3F ,你就可以了。

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. 您需要在URL中使用特殊字符对参数进行编码。 You can use encodeURIComponent from front end . 您可以从前端使用encodeURIComponent

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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