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

跟風遠走 提交于 2020-01-11 06:24:44

问题


I am using Spring, CXF, Tomcat for developing web services.

I have a problem sending characters such as pipes(|) in the URL?

For example: 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?

I tried setting the URIEncoding="UTF-8" on the connector object in server.xml in tomcat. But it did not fix it.

I also tried adding filter-mapping in the web.xml in my application like this:-

<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?


回答1:


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




回答2:


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

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



来源:https://stackoverflow.com/questions/21940826/how-to-handle-escape-characters-pipe-in-url-spring-rest-cxf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!