How does springMVC validated, verify the fields in the member variables in the Controller request?

RT.
I want to verify that in the request InnerValidatedRequest, if the fields houseName and houseAddress, in the InnerValidatedRequestData member object are annotated with the @ Validated of SpringMVC, what else do you need to add?
if this is the case now, the two parameters will not be verified after a try.

-part of the code is as follows-

CommonController.java

@RestController
@RequestMapping(consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
public class CommonController {
    /**
     * test for nested inner @Validated
     * Exception
     */
    @PostMapping("/inner/validated")
    public String innerValidated(@RequestBody @Validated InnerValidatedRequest request) {
        
        return "GOT!";
    }
}

InnerValidatedRequest.java

@Getter
@Setter
public class InnerValidatedRequest extends BaseUserRequest {

    private InnerValidatedRequestData reqData;

    private String otherParma1;
    private String otherParma2;
}

InnerValidatedRequestData.java

@Getter
@Setter
public class InnerValidatedRequestData implements BaseRequestData {

    @NotBlank(message = "")
    private String houseName;
    @NotBlank(message = "")
    private String houseAddress;
}

ask this question, mainly because there are many interfaces here to send http requests to the background system. If there is an object that can be directly turned into a sent request, it can reduce a lot of work. For example, I can send InnerValidatedRequestData directly as a request to the background system to get the data I want.

Mar.03,2021

nested check? Try adding @ Valid ( javax.validation.Valid ) to the reqData variable as follows:

@Valid
private InnerValidatedRequestData reqData;

add: in fact, you can take a look at the javax.validation package. For example, constraints is a commonly used annotation for verifying general attributes, while the source code of javax.validation.Valid is commented as follows:

/**
 * Marks a property, method parameter or method return type for validation cascading.
 * <p/>
 * Constraints defined on the object and its properties are be validated when the
 * property, method parameter or method return type is validated.
 * <p/>
 * This behavior is applied recursively.
 *
 * @author Emmanuel Bernard
 * @author Hardy Ferentschik
 */

Chinese translation:

/**
 * 
 * <p/>
 * 
 * <p/>
 * 
 */

meets the requirements.


comments for validations that use Hiberate will be found in hibernate-validator- .jar and validation-api- .jar.

in addition to the commonly used empty, size and other validations, you can also customize the validation annotations to meet the actual business logic.

default validation error messages are found in BindingResult bindingResult

SPRING BOOT VALIDATION verification

Menu