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

狂风中的少年 提交于 2020-01-06 10:05:12

问题


This is making a spring boot project to maintain the MVC architecture and as well being RestFul. There is a project that is fully working properly using the MVC architecture, I'll like to make it work as well by being called from a Rest client. Posting from the Thymeleaf UI works fine, however, when I try to post from a Rest client all the DTO properties are null. I don't know why. How do I make the posting from the client get all the properties posted?

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 client call

{
    "firstName": "Kehinde",
    "lastName": "Adeoya",
    "username": "ken4ward",
    "email": "kadeoya@oltega.com",
    "password": "o201115@Adel",
    "matchingPassword": "o201115@Adel",
    "statusName": "ROLE_ADMIN"
}

This is the

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

This is the controller:

@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;
    }

Error

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

来源:https://stackoverflow.com/questions/58149526/dto-returns-null-when-called-from-a-rest-client-using-spring-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!