使用弹簧引导从REST客户端调用时DTO返回null(DTO returns null when c

2019-10-31 10:45发布

这是使弹簧的启动项目,以保持MVC架构和福祉REST风格。 有一个项目,是完全正常使用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": "kadeoya@oltega.com",
    "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
文章来源: DTO returns null when called from a Rest client using spring boot