使用 spring boot 从 Rest 客户端调用时,DTO 返回 null

DTO returns null when called from a Rest client using spring boot

提问人:ken4ward 提问时间:9/29/2019 更新时间:9/29/2019 访问量:1173

问:

这是在制作一个 Spring Boot 项目来维护 MVC 架构以及 RestFul。有一个项目使用 MVC 架构完全正常工作,我想通过从 Rest 客户端调用它来使其也能正常工作。从 Thymeleaf UI 发布工作正常,但是,当我尝试从 Rest 客户端发布时,所有 DTO 属性都是 null。我不知道为什么。如何使客户端的发布获得所有属性的发布?

UserDto
@PasswordMatches
public class UserDto {

    @NotNull
    @Size(min = 1)
    private String firstName;

    @NotNull
    @Size(min = 1)
    private String lastName;

    @NotNull
    @Size(min = 1)
    private String username;

    @ValidPassword
    private String password;

    @NotNull
    @Size(min = 1)
    private String matchingPassword;

    @ValidEmail
    @NotNull
    @Size(min = 1)
    private String email;

    private boolean isUsing2FA;
    private String statusName;

    public String getStatusName() {
        return statusName;
    }

    public void setStatusName(String statusName) {
        this.statusName = statusName;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(final String email) {
        this.email = email;
    }

    private Integer role;

    public Integer getRole() {
        return role;
    }

    public void setRole(final Integer role) {
        this.role = role;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(final String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(final String lastName) {
        this.lastName = lastName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(final String password) {
        this.password = password;
    }

    public String getMatchingPassword() {
        return matchingPassword;
    }

    public void setMatchingPassword(final String matchingPassword) {
        this.matchingPassword = matchingPassword;
    }

    public boolean isUsing2FA() {
        return isUsing2FA;
    }

    public void setUsing2FA(boolean isUsing2FA) {
        this.isUsing2FA = isUsing2FA;
    }

    @Override
    public String toString() {
        final StringBuilder builder = new StringBuilder();
        builder.append("UserDto [firstName=").append(firstName)
                .append(", lastName=").append(lastName)
                .append(", username=").append(username)
                .append(", password=").append(password)
                .append(", matchingPassword=").append(matchingPassword)
                .append(", email=").append(email)
                .append(", isUsing2FA=").append(isUsing2FA)
                .append(", role=").append(role).append("]");
        return builder.toString();
    }

Rest 客户端调用

{
    "firstName": "Kehinde",
    "lastName": "Adeoya",
    "username": "ken4ward",
    "email": "[email protected]",
    "password": "o201115@Adel",
    "matchingPassword": "o201115@Adel",
    "statusName": "ROLE_ADMIN"
}

这是

public User save(UserDto user) {
            Set<ConstraintViolation<UserDto>> violations = validator.validate(user);
            if (violations.size() > 0) {
                throw new BadRequestException();
            }
............

这是控制器:

@RequestMapping(value = "/registration", method = RequestMethod.POST )
    @ResponseBody
    public User registerUserAccount(final UserDto accountDto, final HttpServletRequest request) {
        final User registered = userInterface.save(accountDto);
        eventPublisher.publishEvent(new OnRegistrationCompleteEvent(registered, request.getLocale(), getAppUrl(request)));
        return registered;
    }

错误

web - 2019-09-28 19:50:54,552 [http-nio-8081-exec-1] INFO  o.a.c.c.C.[.[.[/] - Initializing Spring DispatcherServlet 'dispatcherServlet'
web - 2019-09-28 19:50:54,987 [http-nio-8081-exec-1] DEBUG o.b.w.c.RegistrationController - Registering user account with information: UserDto [firstName=null, lastName=null, username=null, password=null, matchingPassword=null, email=null, isUsing2FA=false, role=null]
web - 2019-09-28 19:50:55,049 [http-nio-8081-exec-1] ERROR o.b.w.c.e.RestResponseEntityExceptionHandler - 500 Status Code
j.l.NullPointerException: null
    at o.b.s.UserService.save(UserService.java:81)
    at o.b.w.c.RegistrationController.registerUserAccount(RegistrationController.java:85)
    at j.i.r.NativeMethodAccessorImpl.invoke0(NativeMethodAccessorImpl.java)
    at j.i.r.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at j.i.r.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    ... 75 frames truncated
java spring-boot spring-mvc nullreferenceexception dto

评论

1赞 Roddy of the Frozen Peas 9/29/2019
那个帐户Dto应该有吗?@RequestBody
0赞 ken4ward 9/29/2019
实际上我删除了它,但结果是一样的。
1赞 Roddy of the Frozen Peas 9/29/2019
但是您希望在请求正文中传递 DTO,而不是作为查询参数?
1赞 ken4ward 9/29/2019
@RoddyoftheFrozenPeas,请提供此作为答案,以帮助他人。我真的很感谢你的帮助。荣誉。

答: 暂无答案