简体   繁体   English

Spring Boot - 源不能为空

[英]Spring Boot - Source must not be null

I created a simple Spring Boot app with one endpoint that creates a new record in a users table.我创建了一个简单的 Spring Boot 应用程序,其中一个端点在users表中创建了一条新记录。

However, when i hit this endpoint with Postman, i'm getting the following error:但是,当我使用 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]

What is the issue?问题是什么?

EDIT:编辑:

Apparently it does save the record in the DB, but returns an error still.显然它确实将记录保存在数据库中,但仍然返回错误。 Here's the code for UserController :这是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 has asserting that parameter can't be null. Java spring 断言该参数不能为空。 So as you mentioned you are calling method所以正如你提到的,你正在调用方法

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

You simply need to make sure that values are not null您只需要确保值不为空

If you look in source of copy method you will find check for not to be null.如果您查看复制方法的来源,您会发现检查不为空。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM