gpt4 book ai didi

java - Spring Boot - 源不能为空

转载 作者:行者123 更新时间:2023-12-02 09:15:28 25 4
gpt4 key购买 nike

我创建了一个简单的 Spring Boot 应用程序,其中一个端点在 users 表中创建一条新记录。

但是,当我使用 Postman 到达此端点时,出现以下错误:

java.lang.IllegalArgumentException: Source must not be null
at org.springframework.util.Assert.notNull(Assert.java:198) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.beans.BeanUtils.copyProperties(BeanUtils.java:693) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.beans.BeanUtils.copyProperties(BeanUtils.java:639) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at com.demo.controller.UserController.createUser(UserController.java:45) ~[classes/:na]

问题是什么?

编辑:

显然它确实将记录保存在数据库中,但仍然返回错误。以下是 UserController 的代码:

    @PostMapping
public UserRest createUser(@RequestBody UserDetailsRequestModel userDetails)
{

UserRest result = new UserRest();
UserDto userDto = new UserDto();

BeanUtils.copyProperties(userDetails, userDto);

UserDto createdUser = userService.createUser(userDto);

BeanUtils.copyProperties(createdUser, result);
return result;
}

最佳答案

Java spring 断言参数不能为空。正如您所提到的,您正在调用方法

BeanUtils.copyProperties(createdUser, result)    at com.demo.controller.UserController.createUser(UserController.java:45) ~[classes/:na]

您只需确保值不为空

如果您查看复制方法的源代码,您会发现检查不为空。

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

关于java - Spring Boot - 源不能为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59023814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com