Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Valid does not work for nested object in bean class in spring

I have a Bean class which has one nested object like below.

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class UserRequestDTO {

        private String transactionId;

        private String email;

        @Valid @NotNull HistoryRequestDTO historyRequestDTO;
    }

This is the nested object bean class.

import javax.validation.constraints.*;
    @Data
    public class HistoryRequestDTO {

        @NotNull(message = Constants.INVALID_FIELD_DATA_EN_US)
        @Range(min = 0, max = 100, message = Constants.INVALID_FIELD_DATA_EN_US)
        @NumberFormat(style = NumberFormat.Style.NUMBER)
        Integer pageNumber;

        @NotNull(message = Constants.INVALID_FIELD_DATA_EN_US)
        @Range(min = 50, max = 500, message = Constants.INVALID_FIELD_DATA_EN_US)
        @NumberFormat(style = NumberFormat.Style.NUMBER)
        Integer pageSize;
    }

I already have implemented validator for HistoryRequestDTO and working fine seperately. But when I use UserRequestDTO, HistoryRequestDTO validator does not work.

I tried to implement seperate validator for UserRequestDTO, but still it is not calling HistoryRequestDTO validator.

like image 803
Anjali Avatar asked Feb 15 '26 04:02

Anjali


1 Answers

try this

@Valid usage show here for nested object in bean class just check once..

hibernate validator

like image 114
kumar Avatar answered Feb 18 '26 06:02

kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!