解决org.springframework.web.method.annotation.MethodArgumentTypeMismatchException警告

场景:

spring项目中无法访问到对应controller,查看日志,没有报错,只有warnring:

org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.logException Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException:
 Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type 
[@org.springframework.web.bind.annotation.RequestParam java.util.Date] for value '2019-06-21'; nested exception is java.lang.IllegalArgumentException]

直接来说就是字符串无法转为Date类型。

引起原因是大部分人使用Date格式作为controller接口的参数,导致springmvc无法将String转为Date,已知springmvc内置了一系列的转换,不过都是基本类型到包装类以及String到包装类,和List、Set、Map等。但是对类似String转Date还是无能为力的。

2种解决办法:

1. 参数改为String类型,在后端处理String到Date的转换,而不是交给SpringMVC来处理。

2 写转换类实现org.springframework.core.convert.converter.Converter,并在SpringMVC中注册。

该Converter接口较为简单,代码略

  <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <bean class="com.l.utils.StringToDateConverter"/>
            </set>
        </property>
    </bean>
    <mvc:annotation-driven conversion-service="conversionService"/>

如果是springboot项目:


    @Bean
    public ConversionService conversionService() {
        FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean();
        DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
        registrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        factory.setFormatterRegistrars(Collections.singleton(registrar));
        factory.afterPropertiesSet();
        return factory.getObject();
    }

原理:

springmvc默认已经提供了很多自动转换,查看org.springframework.context.support.ConversionServiceFactoryBean源码:

查看Converter接口:

@FunctionalInterface
public interface Converter<S, T> {

	/**
	 * Convert the source object of type {@code S} to target type {@code T}.
	 * @param source the source object to convert, which must be an instance of {@code S} (never {@code null})
	 * @return the converted object, which must be an instance of {@code T} (potentially {@code null})
	 * @throws IllegalArgumentException if the source cannot be converted to the desired target type
	 */
	@Nullable
	T convert(S source);

}

已经注册的子类:

比如有从StringToBooleanConverter,就说明参数String到Boolean(参数以Boolean类型)是没有问题的!

  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
### 回答1: org.springframework.web.method.annotation.MethodArgumentTypeMismatchException是Spring框架中的一个异常类,表示方法参数类型不匹配的异常。当请求参数类型与方法参数类型不匹配时,就会抛出这个异常。 ### 回答2: org.springframework.web.method.annotation.methodargumenttypemismatchexception是Spring框架中的异常类,当控制器方法的参数类型与请求中的参数类型不匹配时会抛出此异常。 通常情况下,我们在使用SpringMVC开发WEB应用程序时,控制器的请求处理方法都会有方法参数,这些方法参数的值来源于客户端请求的参数,例如GET和POST请求的参数。SpringMVC在处理请求时,会通过数据绑定的方式将HTTP请求参数绑定到控制器方法的参数上,从而完成参数的传递。 但是,当客户端请求的参数类型与控制器方法的参数类型不一致时,就会抛出methodargumenttypemismatchexception异常。例如,当客户端传递的参数是字符串类型,而方法参数是整数类型时,就会抛出此异常。 当出现此异常时,我们需要通过查看异常信息和请求参数与方法参数的类型,来找出哪个参数类型不匹配,然后修复代码。 另外,为了避免此类异常,我们可以通过加强请求参数的校验,提前过滤无效参数,从而减少参数类型不匹配的情况。同时,我们也可以使用Jackson或Gson等工具完成对象的序列化和反序列化,从而更好地处理请求数据。 ### 回答3: org.springframework.web.method.annotation.methodargumenttypemismatchexception 是 Spring 框架提供的一个异常类,它通常在 MVC 架构中的请求处理阶段发生。当一个请求到达后,Spring 会尝试根据请求中带有的参数类型来寻找对应的处理器方法,如果在方法中传入的参数类型与请求中的参数类型不一致,就会抛出 methodargumenttypemismatchexception 异常。 通常情况下,造成类型不匹配的原因可能是两个:一是用户提交的数据格式与处理器方法参数类型不一致,例如前端页面提交日期格式为字符串但是后端接收日期格式为 Date 类型;另一个可能是用户在传递请求参数时,传递了错误的参数类型,例如将 String 类型的参数传给了 Integer 类型的方法。 当出现这种异常时,需要对请求参数进行校验与数据类型的转换。在 Spring 4.2 之前,通常需要使用 initBinder 方法来处理。在 Spring 4.2 之后,提供了更加简洁的方法,可以使用 @InitBinder、@ControllerAdvice 和 @ControllerAdvice 注解来实现。 总之,org.springframework.web.method.annotation.methodargumenttypemismatchexception 异常可以在 Spring MVC 开发过程中帮助开发者更好地规范参数类型,提高程序的鲁棒性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值