Java 如何处理 URL(Spring、REST、CXF)中的转义字符(管道 |)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21940826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle escape characters (pipe |) in URL (Spring, REST, CXF)?
提问by Srikanth K
I am using Spring, CXF, Tomcat for developing web services.
我正在使用 Spring、CXF、Tomcat 来开发 Web 服务。
I have a problem sending characters such as pipes(|) in the URL?
我在 URL 中发送诸如竖线 (|) 之类的字符时遇到问题?
For example:
http://localhost:9080/address/v1/countries/\
|
例如:
http://localhost:9080/address/v1/countries/\
|
throws a 500 error, is there a way to handle it and throw custom error code like 404 or even 400?
抛出 500 错误,有没有办法处理它并抛出自定义错误代码,如 404 甚至 400?
I tried setting the URIEncoding="UTF-8" on the connector object in server.xml in tomcat. But it did not fix it.
我尝试在 tomcat 的 server.xml 中的连接器对象上设置 URIEncoding="UTF-8"。但它没有修复它。
I also tried adding filter-mapping in the web.xml in my application like this:-
我还尝试在我的应用程序的 web.xml 中添加过滤器映射,如下所示:-
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Can someone point me in what way these kind of URI's can return a custom error code and message?
有人可以告诉我这些类型的 URI 可以返回自定义错误代码和消息的方式吗?
回答by Karthik Prasad
回答by Rahul Varadkar
Yes. %7C is PIPE Character that needs to be used in URL. following is another example that I used successfully. In this neighborhood name is "ROOT|RESTFUL" which is represented as ROOT%7CRESTFUL
是的。%7C 是需要在 URL 中使用的 PIPE 字符。以下是我成功使用的另一个示例。在这个街区名称是“ ROOT|RESTFUL”,表示为 ROOT%7CRESTFUL