0

I have a scenario where json request is made of different custom objects like:

{
 "person:":{
    "name":"xyx",
    "age":25
  },
  "movieList":[
     {
       "name":"yyy",
       "duration":34,
       "language":"english"
     },
     {
       "name":"zzz",
       "duration":37,
       "language":"english"
     }
   ]

}

and the java class looks like

public class Customer{
 private Person person,
 private List<MovieList> movieList;
}

Condition for validation:Fields cannot be null

Now coming to spring controller i want to validate each object coming from the request,and i am able to validate as whole Customer object since BindingResult is applied on the entire request Body.But my requirement is to validate Person Object and MovieList object separately.

public void createCustomer(@RequestBody Customer customer,BindingResult result){
}

and also my requirement is to throw an exception of specific object which is causing the exception to happen since my framework is designed that way.

Vinay Prajapati
  • 7,199
  • 9
  • 45
  • 86
Prash
  • 544
  • 8
  • 24

2 Answers2

1

You can use validation annotations from spring framework. it will validate each field from POJO. Please check the below example. use the same method in MovieList.

import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.validation.constraints.Email;

public class Person{

@NotNull(message = "Name cannot be null")
private String name;

@AssertTrue
private boolean working;

@Size(min = 10, max = 200, message = "About Me must be between 10 and 200 characters")
private String aboutMe;

@Min(value = 18, message = "Age should not be less than 18")
@Max(value = 150, message = "Age should not be greater than 150")
private int age;

@Email(message = "Email should be valid")
private String email;

// standard setters and getters 
 }
Akhil Jayakumar
  • 2,262
  • 14
  • 25
  • Thanks but I am trying to validate the Customer class a whole not Specific Object and the binding result is tagged for the entire Request body but not for the nested Objects inside Customer. – Prash Apr 01 '18 at 13:11
  • Please refer this. https://stackoverflow.com/questions/46591861/dynamic-pojo-validation-based-on-groups-in-spring/46645395 – Akhil Jayakumar Apr 01 '18 at 13:18
0

First of all, write down the validation code (custom validation or annotation based ) for Person and MovieList. As you said you want to validate Person and MovieList object separately, you could write their validators and then do a manual validation:-

validator.validate(person, bindingResult);  
if (bindingResult.hasErrors()) {  
    //throw your exception for person object  
}

validator.validate(movieList, bindingResult);  
if (bindingResult.hasErrors()) {  
    //throw your exception for movieList object  
}//Or iterate through each object

To throw your exception you could be either throw based on if it's a field error or ObjectError.

for (Object object : bindingResult.getAllErrors()) {
    if(object instanceof FieldError) {
        FieldError fieldError = (FieldError) object;
        //throw error
    }

    if(object instanceof ObjectError) {
        ObjectError objectError = (ObjectError) object;
        //throw error
    }
}

Hope it helps!

Vinay Prajapati
  • 7,199
  • 9
  • 45
  • 86
  • Thanks but my problem is My Request body is customer and bindingResult is for the whole customer which is mad of Person+MovieList. – Prash Apr 01 '18 at 13:09