Encoded slash (/) with Spring RequestMapping path param gives HTTP 400

前端 未结 9 771
陌清茗
陌清茗 2020-12-05 00:10

This is not a duplicate referenced question, because it is Spring specific. Whoever added that (3 years after the fact!) didn\'t bother to read the question or comment

9条回答
  •  离开以前
    2020-12-05 00:28

    Here is a fix for Spring 3.2.4 (should work for other versions as well). One must overwrite the default UrlPathHelper

        public class UrlPathHelperFixed extends UrlPathHelper {
    
            public UrlPathHelperFixed() {
                super.setUrlDecode(false);
            }
    
            @Override
            public void setUrlDecode(boolean urlDecode) {
                if (urlDecode) {
                    throw new IllegalArgumentException("Handler [" + UrlPathHelperFixed.class.getName() + "] does not support URL decoding.");
                }
            }
    
            @Override
            public String getServletPath(HttpServletRequest request) {
                return getOriginatingServletPath(request);
            }
    
            @Override
            public String getOriginatingServletPath(HttpServletRequest request) {
                return request.getRequestURI().substring(request.getContextPath().length());
            }
        }
    

    And inject it to the Mapping Handler:

        
            
            
                
            
        
    

    After a day of hard works it works now for me :-)

    It was suggested to Spring team as https://jira.springsource.org/browse/SPR-11101

提交回复
热议问题