我想在我的RESTful API中为get方法的查询参数中添加换行符。
这是我在邮递员中的最终GET
URL:-
www.localhost:8091/customRule?someString="this is some string \\n on new line"
我得到例外:-
java.lang.IllegalArgumentException: Invalid character found in the request target [/customRule?someString=%22this%20is%20some%20string%20\\n%20on%20new%20line%22]. The valid characters are defined in RFC 7230 and RFC 3986
我该如何解决?
我已经阅读了有关此主题的一些内容,但认为您所要查找的答案不完全正确。但是我想到了这样的事情。在Java
中,新行字符为'\n
'。
我们需要对换行符like here进行编码。
我尝试了一些像这样的小代码:
@GetMapping(value = "/new")
public ResponseEntity<?> newLineCharacter(@RequestParam("newline") String newLine) {
String s = new StringBuilder("xxx").append(newLine).append("yyy").toString();
log.info(s);
return ResponseEntity.ok().build();
}
当我使用如下编码的换行符%0A
触发此url时:
localhost:8080 / a / new?newLine =%0A
我在日志中看到这样的输出:
xxx
yyy
但是这里似乎有一个重要的要注意的地方,在Linux
,Windows
或Mac
环境中,换行符为different。
您可以看到详细信息here。
因此,在我看来,将换行符用作查询参数不是一个好主意。
希望对您有帮助。