3

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.

Anjali
  • 1,623
  • 5
  • 30
  • 50
  • Do you get any errors or so? Do you have java bean standards followed? Please add the imports as well – Keerthivasan Oct 19 '18 at 10:53
  • I have added imports in question. I am not getting any error as such, just that it is not calling nested object validator. because of this I am able to send wrong data in request. – Anjali Oct 19 '18 at 11:05

1 Answers1

2

try this

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

hibernate validator

kumar
  • 497
  • 4
  • 12
  • I am doing same but its not working. its not calling nested class validator to prform validation. – Anjali Oct 19 '18 at 11:07