springMVC参数绑定源码分析
创始人
2024-02-17 12:47:06
0

一、遇到的问题

1. 在请求时get方法路径传参,接收时,用枚举接收,出现参数绑定错误

请求路径:http://localhost:9104/api/sent/test2?type=0

后端代码:

@GetMapping("/test2")public String openNewFile2(FileDTO param) {System.out.println("=====" + param);return "222";}
@Data
public class FileDTO {private SortTypeEnum type;
}

 

枚举类:

@Getter
@AllArgsConstructor
public enum SortTypeEnum {/*** 生序*/ASC(0,"生序"),/*** 降序*/DESC(1,"降序"),;@JsonValueprivate final Integer code;private final String Label;
}

 

org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult

org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors\nField error in object 'fileDTO' on field 'type': rejected value [0]; codes [typeMismatch.fileDTO.type,typeMismatch.type,typeMismatch.com.example.demoes.enums.SortTypeEnum,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [fileDTO.type,type]; arguments []; default message [type]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'com.example.demoes.enums.SortTypeEnum' for property 'type'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [com.example.demoes.enums.SortTypeEnum] for value '0'; nested exception is java.lang.IllegalArgumentException: No enum constant com.example.demoes.enums.SortTypeEnum.0]\r\n\tat org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:164)\r\n\tat org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)\r\n\tat org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:167)\r\n\tat org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792)\r\n\tat org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)\r\n\tat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)\r\n\tat org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)\r\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:626)\r\n\tat org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)\r\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:733)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)\r\n\tat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)\r\n\tat org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)\r\n\tat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)\r\n\tat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)\r\n\tat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)\r\n\tat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)\r\n\tat org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)\r\n\tat org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)\r\n\tat org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)\r\n\tat org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1589)\r\n\tat org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)\r\n\tat java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)\r\n\tat java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)\r\n\tat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)\r\n\tat java.lang.Thread.run(Thread.java:745)\r\n

经过排查发现,在StringToEnumConverterFactory---convert

public T convert(String source) {return source.isEmpty() ? null : Enum.valueOf(this.enumType, source.trim());
}

这个代码的时候,出现错误,上边代码是通过枚举的name去转换成枚举,实现参数绑定。

如果页面传枚举下标就会出错。

正确请求方式:

http://localhost:9104/api/sent/test2?type=ASC

这样就能绑定到参数上了。

2. 如果是post请求,就可以需要body传参

请求路径:

http://localhost:9104/api/sent/test4

body参数:

{"type":1
}

后端代码:

@PostMapping("/test4")public String openNewFile5(@RequestBody FileDTO param) {System.out.println("=====" + param);return "222";}

二、源码分析:

1. get参数绑定

在项目启动时会初始化默认的参数转换类:org.springframework.boot.autoconfigure.BackgroundPreinitializer.ConversionServiceInitializer

private static class ConversionServiceInitializer implements Runnable {private ConversionServiceInitializer() {}
// 会执行这个方法,初始化public void run() {new DefaultFormattingConversionService();}}
public DefaultFormattingConversionService(@Nullable StringValueResolver embeddedValueResolver, boolean registerDefaultFormatters) {if (embeddedValueResolver != null) {this.setEmbeddedValueResolver(embeddedValueResolver);}// 开始注册默认的DefaultConversionService.addDefaultConverters(this);
//这个方法也会执行if (registerDefaultFormatters) {addDefaultFormatters(this);}}

继续走到下面org.springframework.core.convert.support.DefaultConversionService#addDefaultConverters

这个方法会注册很多数据类型转换类

 public static void addDefaultConverters(ConverterRegistry converterRegistry) {addScalarConverters(converterRegistry);addCollectionConverters(converterRegistry);converterRegistry.addConverter(new ByteBufferConverter((ConversionService)converterRegistry));converterRegistry.addConverter(new StringToTimeZoneConverter());converterRegistry.addConverter(new ZoneIdToTimeZoneConverter());converterRegistry.addConverter(new ZonedDateTimeToCalendarConverter());converterRegistry.addConverter(new ObjectToObjectConverter());converterRegistry.addConverter(new IdToEntityConverter((ConversionService)converterRegistry));converterRegistry.addConverter(new FallbackObjectToStringConverter());converterRegistry.addConverter(new ObjectToOptionalConverter((ConversionService)converterRegistry));}

在spring容器启动过程中,有个后置处理器

org.springframework.boot.SpringApplication#postProcessApplicationContext

protected void postProcessApplicationContext(ConfigurableApplicationContext context) {if (this.beanNameGenerator != null) {context.getBeanFactory().registerSingleton("org.springframework.context.annotation.internalConfigurationBeanNameGenerator", this.beanNameGenerator);}if (this.resourceLoader != null) {if (context instanceof GenericApplicationContext) {((GenericApplicationContext)context).setResourceLoader(this.resourceLoader);}if (context instanceof DefaultResourceLoader) {((DefaultResourceLoader)context).setClassLoader(this.resourceLoader.getClassLoader());}}
//  这里会获取上边实现*Factory的转换类,放入conversionService字段中if (this.addConversionService) {context.getBeanFactory().setConversionService(ApplicationConversionService.getSharedInstance());}}

这里会有166个转换类,被加载好放进去 

下面进行请求访问

第一次 请求会初始化servlet

org.springframework.web.servlet.DispatcherServlet#initStrategies---

注册本地的转换

initLocaleResolver(context);

 会创建bean--org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver

将上边启动时创建好的ConversionService放入下边的字段中

org.springframework.beans.PropertyEditorRegistrySupport#setConversionService

由请求转发类到参数解析

org.springframework.web.method.support.HandlerMethodArgumentResolverComposite#resolveArgument

public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer, NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {HandlerMethodArgumentResolver resolver = this.getArgumentResolver(parameter);if (resolver == null) {throw new IllegalArgumentException("Unsupported parameter type [" + parameter.getParameterType().getName() + "]. supportsParameter should be called first.");} else {
// 走这里参数解析return resolver.resolveArgument(parameter, mavContainer, webRequest, binderFactory);}}

绑定参数

this.bindRequestParameters(binder, webRequest);
protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request) {ServletRequest servletRequest = request.getNativeRequest(ServletRequest.class);Assert.state(servletRequest != null, "No ServletRequest");ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder;
// 下面参数绑定,先从请求中获取参数servletBinder.bind(servletRequest);}

org.springframework.web.bind.ServletRequestDataBinder#bind

public void bind(ServletRequest request) {
// 请求获取参数MutablePropertyValues mpvs = new ServletRequestParameterPropertyValues(request);MultipartRequest multipartRequest = (MultipartRequest)WebUtils.getNativeRequest(request, MultipartRequest.class);if (multipartRequest != null) {this.bindMultipart(multipartRequest.getMultiFileMap(), mpvs);}this.addBindValues(mpvs, request);this.doBind(mpvs);}

 从请求获取参数的方法,都是用String接收的

org.springframework.web.util.WebUtils#getParametersStartingWith

 

会走到:org.springframework.validation.DataBinder#applyPropertyValues

参数绑定解析结果,找到要绑定的属性

 设置属性

org.springframework.beans.AbstractPropertyAccessor#setPropertyValues(org.springframework.beans.PropertyValues, boolean, boolean)

参数转换

 

相关内容

热门资讯

江苏多地推出公租房调换政策 就... 原题:就医养老更方便 按需调换更贴心 公租房也能“换着住” 公共租赁房是由政府提供支持,为中低收入困...
法治日报:跨境犯罪治理需要更完... 跨境犯罪呈现多重犯罪形态交织特征 各国代表建言 跨境犯罪治理需要更完善的司法保障 编者按 携手30年...
原创 刘... 2025年12月18日,海南自由贸易港全岛封关运作正式启动,标志着我国高水平对外开放进入新阶段。全球...
美联储内部分歧加剧:哈马克称政... 智通财经APP获悉,克利夫兰联邦储备银行行长贝丝·哈马克表示,在评估第一季度累计75个基点的降息对经...
【深圳特区报】深港融通新格局 ... 前海港资企业突破万家、累计105项制度创新成果在全国复制推广、现代服务业增加值达1460亿元……12...
犯罪对象和受贿数额认定问题分析 实践中,有的行贿人为了送给国家工作人员好处,不直接送给国家工作人员财物,而是先委托国家工作人员代为出...
用好制度创新“加速器” 制度创新是破解发展难题、激发区域活力的核心密钥。上海浦东开发开放30余载的实践证明,唯有以制度创新破...
紫牛热点︱家庭纠纷导致情绪失控... 扬子晚报网12月21日讯(记者 郭一鹏) 12月20日下午,一段男子在街头拦停一辆越野车,追砸车辆前...
多城出台政策对老房子“强制体检... 越来越多城市的老房子,要“强制体检”了。 最近,郑州市房管局发布了一则实施方案,将对房龄30年以上的...