title: JSR-380 - Bean Validation of Nested Object and FieldError ObjectName
body: <p>I have a very precise request. Imagine I have the following basic model :</p>
  
  <pre><code>@ValidStudent
  public class Student {
      private String firstName;
      private String gender;
      private List&lt;@ValidCourse Course&gt; courses;
  }
  
  public class Course {
      private String title;
      private LocalDate date;
      private String level;
  }
  </code></pre>
  
  <p><code>@ValidCourse</code>is a simple custom annotation which validate a <code>Course</code> object.</p>
  
  <p>In my <code>Spring Boot</code> application there is a custom <code>@ExceptionHandler</code> which returns errors as follow :</p>
  
  <pre><code>{
      "apierror": {
          "status": "BAD_REQUEST",
          "timestamp": "05-03-2020 11:00:09",
          "message": "Validation Error",
          "subErrors": [
              {
                  "object": "Student",
                  "field": "firstName",
                  "rejectedValue": " ",
                  "message": "firstName can't be blank. rejected value={" "}"
              },
              {
                  "object": "Student",
                  "field": "gender",
                  "rejectedValue": "FOO",
                  "message": "gender has not a valid value. rejected value={FOO}"
              },
              {
                  "object": "Student",
                  "field": "course[0].level",
                  "rejectedValue": "BAR",
                  "message": "level has not a valid value. rejected value={BAR}"
              }
          ]
      }
  }
  </code></pre>
  
  <p>Everything seems to work fine BUT if you have a look at the apierror.subErrors[2].object you may notice that the object is "Student" but I want to return "Course" instead. In dept, <code>subError.object</code> is the value of FieldError (package <code>org.springframework.validation</code>) from ObjectError.objectName (same package).</p>
  
  <p>I've already tried to move the <code>@ValidCourse</code> on the <code>Course</code> level class as follow but in this case, the validator is not triggered on <code>POST</code> request when a <code>Student</code> object is send with a <code>List&lt;Course&gt;</code> : </p>
  
  <pre><code>@ValidCourse
  public class Course {
          private String title;
          private LocalDate date;
          private String level;
  }
  </code></pre>
  
  <p>I think the easiest solution is to trigger the <code>@ValidCourse</code> on Course class level when a <code>POST</code> request is submit but I can't figure out how to do that :( For now my controller request is :</p>
  
  <pre><code>@PostMapping
  public Student addStudent(@RequestBody @Valid Student student){
      return studentService.save(student);
  }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>By adding <code>@Validated</code> at controller class level it validates the <code>List&lt;Course&gt;</code> object with <code>@ValidCourse</code> on class Course level. But, it still returns <code>object: Student</code> as <code>FieldError.objectName</code> :(</p>
  
  <p><strong>EDIT-2</strong>
  After more research, I'm almost sure that it will be complex configuration to set up. My <code>subError</code> objects are based on <code>org.springframework.validation.FieldError</code> which extends <code>ObjectError</code> from same package. This ObjectError Pojo define the objectName as final String. So there is no setter. Also, in the javadoc <code>getObjectName()</code>: <code>Return the name of the bound root object.</code> 
  In conclusion, I think that it is possible by creating some custom configuration classes but in order to keep my app simple to maintain and wihtout any simple solution I'll not doing anything for that for the moment.</p>
  
  <p>Thanks a lot for your help :)</p>