错误日志: BeanUtils.copyProperties() 拷贝空对象出现 Source must not be null

1.结论if判断空对象没写,java是强类型语言,以后还需要注意一点。

请求

{
    "code": 400,
    "message": "Source must not be null",
    "error": "非法的参数",
    "path": "/store/home/homePage",
    "verb": "GET"
}
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@978bc02] was not registered for synchronization because synchronization is not active
2023-03-10 21:51:06.662 DEBUG 26464 --- [io-18082-exec-4] o.s.jdbc.datasource.DataSourceUtils      : Fetching JDBC Connection from DataSource
JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@60db5aad] will not be managed by Spring
==>  Preparing: SELECT user_id,merchant_code,merchant_name,deleted,creator_id,gmt_create,modifier_id,gmt_modify FROM hall_merchant WHERE deleted=0 AND (user_id = ?)
==> Parameters: 1(Long)
<==      Total: 0
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@978bc02]
2023-03-10 21:51:06.710 DEBUG 26464 --- [io-18082-exec-4] .m.m.a.ExceptionHandlerExceptionResolver : Using @ExceptionHandler com.bci.bootstrap.exception.ControllerExceptionHandler#handle(IllegalArgumentException, HttpServletRequest)
2023-03-10 21:51:06.710  WARN 26464 --- [io-18082-exec-4] c.b.b.e.ControllerExceptionHandler       : 非法的参数: Source must not be null
2023-03-10 21:51:06.711 DEBUG 26464 --- [io-18082-exec-4] m.m.a.RequestResponseBodyMethodProcessor : Using 'application/json', given [*/*] and supported [application/json, application/*+json, application/json, application/*+json]
2023-03-10 21:51:06.711 DEBUG 26464 --- [io-18082-exec-4] m.m.a.RequestResponseBodyMethodProcessor : Writing [RestResult(code=400, message=Source must not be null, error=非法的参数, data=null, path=/store/home/homeP (truncated)...]
2023-03-10 21:51:06.712 DEBUG 26464 --- [io-18082-exec-4] o.s.s.w.header.writers.HstsHeaderWriter  : Not injecting HSTS header since it did not match the requestMatcher org.springframework.security.web.header.writers.HstsHeaderWriter$SecureRequestMatcher@2dcc11cd
2023-03-10 21:51:06.713 DEBUG 26464 --- [io-18082-exec-4] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [java.lang.IllegalArgumentException: Source must not be null]
2023-03-10 21:51:06.713 DEBUG 26464 --- [io-18082-exec-4] o.s.web.servlet.DispatcherServlet        : Completed 400 BAD_REQUEST
2023-03-10 21:51:06.714 DEBUG 26464 --- [io-18082-exec-4] o.s.s.w.a.ExceptionTranslationFilter     : Chain processed normally
2023-03-10 21:51:06.714 DEBUG 26464 --- [io-18082-exec-4] s.s.w.c.SecurityContextPersistenceFilter : SecurityContextHolder now cleared, as request processing completed
2023-03-10 21:51:06.717 DEBUG 26464 --- [io-18082-exec-4] o.apache.coyote.http11.Http11Processor   : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@4378e206:org.apache.tomcat.util.net.NioChannel@7319d854:java.nio.channels.SocketChannel[connected local=/192.168.110.102:18082 remote=/192.168.110.102:60324]], Status in: [OPEN_READ], State out: [CLOSED]
2023-03-10 21:51:06.717 DEBUG 26464 --- [io-18082-exec-4] o.apache.tomcat.util.threads.LimitLatch  : Counting down[http-nio-18082-exec-4] latch=1
2023-03-10 21:51:06.717 DEBUG 26464 --- [io-18082-exec-4] org.apache.tomcat.util.net.NioEndpoint   : Calling [org.apache.tomcat.util.net.NioEndpoint@3efb1922].closeSocket([org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper@4378e206:org.apache.tomcat.util.net.NioChannel@7319d854:java.nio.channels.SocketChannel[connected local=/192.168.110.102:18082 remote=/192.168.110.102:60324]])
2023-03-10 21:51:14.979 DEBUG 26464 --- [SchedulerThread] org.quartz.core.QuartzSchedulerThread    : batch acquisition of 0 triggers

在这里插入图片描述

错误原因:
hall_merchant 表查询不到,后面copy的时候报错。但是try catch捕获不到异常。

错误代码 :
异常来源
package org.springframework.beans;
BeanUtils.java

private static void copyProperties(Object source, Object target, @Nullable Class<?> editable,
		@Nullable String... ignoreProperties) throws BeansException {

	Assert.notNull(source, "Source must not be null");
	Assert.notNull(target, "Target must not be null");

	Class<?> actualEditable = target.getClass();
	if (editable != null) {
		if (!editable.isInstance(target)) {
			throw new IllegalArgumentException("Target class [" + target.getClass().getName() +
					"] not assignable to Editable class [" + editable.getName() + "]");
		}
		actualEditable = editable;
	}
	PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
	List<String> ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null);

	for (PropertyDescriptor targetPd : targetPds) {
		Method writeMethod = targetPd.getWriteMethod();
		if (writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) {
			PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
			if (sourcePd != null) {
				Method readMethod = sourcePd.getReadMethod();
				if (readMethod != null) {
					ResolvableType sourceResolvableType = ResolvableType.forMethodReturnType(readMethod);
					ResolvableType targetResolvableType = ResolvableType.forMethodParameter(writeMethod, 0);

					// Ignore generic types in assignable check if either ResolvableType has unresolvable generics.
					boolean isAssignable =
							(sourceResolvableType.hasUnresolvableGenerics() || targetResolvableType.hasUnresolvableGenerics() ?
									ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType()) :
									targetResolvableType.isAssignableFrom(sourceResolvableType));

					if (isAssignable) {
						try {
							if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
								readMethod.setAccessible(true);
							}
							Object value = readMethod.invoke(source);
							if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
								writeMethod.setAccessible(true);
							}
							writeMethod.invoke(target, value);
						}
						catch (Throwable ex) {
							throw new FatalBeanException(
									"Could not copy property '" + targetPd.getName() + "' from source to target", ex);
						}
					}
				}
			}
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值