简体   繁体   English

我如何处理在请求中作为“@PathVariable”出现的“ñ”字符,Spring Boot

[英]How do i Handle a "ñ" character that comes as a "@PathVariable" in a request, Spring Boot

I have a RESTController that has the following method:我有一个具有以下方法的 RESTController:

@GetMapping(value = "/equipos/ciudad/{ciudad}", produces = {"application/json"})
public ResponseEntity<List<EquipoBean>> getEquiposByCiudad(@PathVariable(name = "ciudad") String ciudad) {
    return new ResponseEntity<>(service.getEquiposByCiudad(ciudad), HttpStatus.OK);
}

It works when i send normal characters even if there are multiple words with spaces between them, but when i send it the "ñ" character i get an Exception like this:它在我发送普通字符时有效,即使它们之间有多个带有空格的单词,但是当我发送“ñ”字符时,我会收到这样的异常:

java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:475) ~[tomcat-embed-core-9.0.30.jar:9.0.30]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:260) ~[tomcat-embed-core-9.0.30.jar:9.0.30]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.30.jar:9.0.30]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860) ~[tomcat-embed-core-9.0.30.jar:9.0.30]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1598) ~[tomcat-embed-core-9.0.30.jar:9.0.30]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.30.jar:9.0.30]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.30.jar:9.0.30]
at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]

I tried googling around to search for the answer but i didn't get to the solution so i'm asking here, is there a way for me to set my RESTController to properly handle those types of special characters?我试着在谷歌上搜索答案,但我没有找到解决方案,所以我在这里问,有没有办法设置我的 RESTController 以正确处理这些类型的特殊字符?

Your client is expected to URL-encode such character.您的客户应该此类字符进行URL 编码 So ñ becomes %C3%B1 .所以ñ变成%C3%B1


You seem to be using a Spanish city name as parameter.您似乎使用西班牙城市名称作为参数。 So I will use A Coruña as example.所以我将以A Coruña为例。 If your client is written in Java, you can use URLEncoder :如果您的客户端是用 Java 编写的,则可以使用URLEncoder

String url = "http://example.org?q=" + URLEncoder.encode("A Coruña", "UTF-8");

Starting with Java 10, you can use:从 Java 10 开始,您可以使用:

String url = "http://example.org?q=" + 
    URLEncoder.encode("A Coruña", StandardCharsets.UTF_8);

With that, A Coruña will be encoded to A+Coru%C3%B1a .这样, A Coruña将被编码为A+Coru%C3%B1a A Coruña A+Coru%C3%B1a

The problem is that as the exception says ñ cannot be present in a valid URL.问题是,正如异常所说ñ不能出现在有效的 URL 中。 The RFC defines a very specific set of characters that can be allowed and ALL other characters need to be URL-encoded. RFC 定义了一组可以被允许使用的非常具体的字符,所有其他字符都需要进行 URL 编码。

So the solution should be that whoever is calling your API needs to url-encode their request and your service will work just fine.因此,解决方案应该是调用您的 API 的任何人都需要对他们的请求进行 url 编码,并且您的服务将正常工作。

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

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