我如何在 Spring MVC 中验证另一个对象 (DTO) 的引用对象

how do i validate the referenced object of another object (DTO) in Spring MVC

my UserDto class is here:

package com.carpoint.dto;

import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;

import com.carpoint.validation.annotation.PasswordMatches;

@PasswordMatches
public class UserDto{

    @NotEmpty
    private String username;

    @NotEmpty @Size(min=6, max=30)
    private String password;

    @NotEmpty
    private String confirmPassword;

    @NotEmpty
    private String firstname;
    @NotEmpty
    private String lastname;
    @NotEmpty @Email
    private String email;

    private UserAddressDto userAddress;

    //getters & setters
}

and UserAddressDto class here:

package com.carpoint.dto;

import org.hibernate.validator.constraints.NotEmpty;

public class UserAddressDto {

    @NotEmpty
    private String address;
    @NotEmpty
    private String country;
    @NotEmpty
    private String city;
    @NotEmpty
    private Integer pincode;

        //getters & setters
    }

我的问题是对 UserDto 的验证工作成功,但它不是对 UserAddressDto 的引用类型,我想验证 UserDto 的引用类型有什么办法吗?


and here is my UserController code snippet:

 @RequestMapping(value = "/add", method = RequestMethod.POST)
        public String addUsers(@Valid @ModelAttribute("userDto")UserDto userDto, 
ModelMap model, SessionStatus status, RedirectAttributes attributes) 
throws IOException {
    ....
    ....
    }

您是否尝试将 @Valid 放在 class UserDto 中的 private UserAddressDto userAddress; 上?我想这应该足够了,因为 spring 会自行处理它。