ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Spring MVC 实现请求URL和参数名称的不区分大小写

2022-05-30 13:32:02  阅读:138  来源: 互联网

标签:return URL Spring class MVC Override import public


URL不区分大小写

spring mvc对于请求URL默认是区分大小写的。

如定义一个controller,其请求path为/welcome。

@Controller
public class HomeController {

@RequestMapping(value = "/welcome", method = RequestMethod.GET)
	public String printWelcome(@RequestParam String who,ModelMap model) {
		model.addAttribute("message", "welcome "+who);
		return "hello";
	}
}

本地访问该路径,如:

  1. http://localhost:8080/welcome
  2. http://localhost:8080/Welcome

则,第一个URL可以成功访问,而访问第二个URL则返回404错误。

分析:

URL请求到达DispatcherServlet时,会使用RequestMappingHandlerMapping查找URL匹配的HandlerMethod。其中,会用到RequestMappingHandlerMapping的基类AbstractHandlerMappingPathMatcher的PathMatcher属性进行URL映射处理。RequestMappingHandlerMapping类默认使用AntPathMatcher作为PathMatcher,通过设置AntPathMatcher的CaseSensitive属性为false,来指定PathMatcher对URL的大小字母不敏感。

解决:

code-based configuration

@Configuration
@ComponentScan(value = "com.abc.web", useDefaultFilters = false, includeFilters = {
		@Filter(type = FilterType.ANNOTATION, classes = { Controller.class }) })
public class SpringWebConfig extends WebMvcConfigurationSupport {
	
	@Override
	protected void configurePathMatch(PathMatchConfigurer configurer) {
		AntPathMatcher pathMatcher = new AntPathMatcher();
		pathMatcher.setCaseSensitive(false);
		configurer.setPathMatcher(pathMatcher);
		
	}	

}

参数名称不区分大小写

通过filter机制,创建HttpServletRequest的子类,重写获取请求参数的方法,使参数值的获取不区分参数名称大小写。最后,配置Filter。

import java.io.IOException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Map;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.LinkedCaseInsensitiveMap;
import org.springframework.web.filter.OncePerRequestFilter;

public class CaseInsensitiveRequestParameterNameFilter extends OncePerRequestFilter {

	@Override
	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {
		filterChain.doFilter(new CaseInsensitiveParameterNameHttpServletRequest(request), response);
	}

	public static class CaseInsensitiveParameterNameHttpServletRequest extends HttpServletRequestWrapper {
		private final LinkedCaseInsensitiveMap<String[]> map = new LinkedCaseInsensitiveMap<>();

		@SuppressWarnings("unchecked")
		public CaseInsensitiveParameterNameHttpServletRequest(HttpServletRequest request) {
			super(request);
			map.putAll(request.getParameterMap());
		}

		@Override
		public String getParameter(String name) {

			String[] array = this.map.get(name);
			if (array != null && array.length > 0)
				return array[0];
			return null;
		}

		@Override
		public Map<String, String[]> getParameterMap() {
			return Collections.unmodifiableMap(this.map);
		}

		@Override
		public Enumeration<String> getParameterNames() {
			return Collections.enumeration(this.map.keySet());
		}

		@Override
		public String[] getParameterValues(String name) {
			return this.map.get(name);
		}

	}

}

代码配置Filter

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import com.company.project.web.filter.CaseInsensitiveRequestParameterNameFilter;

public class SpringWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

	@Override
	protected Class<?>[] getRootConfigClasses() {
		return new Class<?>[] { SpringRootConfig.class };
	}

	@Override
	protected Class<?>[] getServletConfigClasses() {
		return new Class<?>[] { SpringWebConfig.class };
	}

	@Override
	protected String[] getServletMappings() {
		return new String[] { "/" };
	}

	@Override
	public void onStartup(ServletContext servletContext) throws ServletException {
		super.onStartup(servletContext);
		Dynamic filter = servletContext.addFilter(CaseInsensitiveRequestParameterNameFilter.class.getSimpleName(),
				CaseInsensitiveRequestParameterNameFilter.class);
		filter.addMappingForUrlPatterns(null, true, "/*");

	}

}

启动程序,访问http://localhost:8080/WELCOME?wHo=me试试吧

转载于:https://my.oschina.net/yqz/blog/814559

标签:return,URL,Spring,class,MVC,Override,import,public
来源: https://www.cnblogs.com/liftsail/p/16326442.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有