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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:31:53  来源:igfitidea点击:

How to handle escape characters (pipe |) in URL (Spring, REST, CXF)?

javaspringweb-servicestomcat7cxf

提问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

please refer herefor url encoding. Say suppose if you want to send Sachin|Tendulkar. You can send as Sachin%7CTendulkar.

请参考这里的 url 编码。假设您想发送 Sachin|Tendulkar。您可以作为 Sachin%7CTendulkar 发送。

回答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

http://localhost:8080/BAE_4_3_API/rest/v1/user/rahul1/neighborhood/ROOT%7CRESTFUL/collaboration/1000/whiteboard

http://localhost:8080/BAE_4_3_API/rest/v1/user/rahul1/neighborhood/ROOT%7CRESTFUL/collaboration/1000/whiteboard