String 查询参数中的换行符

Newline characters in String query parameter

我想在 RESTful API 的 get 方法的查询参数中添加换行符。

这是我在 Postman 中的最后 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

但是这里似乎有一个重要的点需要注意, 在LinuxWindowsMac环境下,换行符不同.

可以看到详情here

因此,使用换行符作为我的查询参数在我看来并不是一个好主意。

希望对您有所帮助。