'Can I get it as BindingResult parameter in method of spring @ControllerAdvice class?

@Component
public class EmployeeValidator implements Validator 

{
 
   
final Logger logger
=LoggerFactory.getLogger(EmployeeValidator.class);
    
@Autowired
    CRUDService crudService;

    @Override
    public boolean supports(Class<?> clazz) {
        return Employee.class.isAssignableFrom(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        logger.info("EmployeeValidator#validator");
        Employee employee = (Employee) target;

        try {
            String name = employee.getFirstName() + " " + employee.getLastName();
            if (!isEmployee(employee.getEmployeeId(), name))
               errors.reject("employee.notFound");

        } catch (Exception e) {
            logger.error("EmployeeValidator#validate");
            e.printStackTrace();
        }
    }
}

//Controller

@RequestMapping("/login")

public class LoginController {

   @PostMapping("/login")

    public String login(@Valid Employee employee, BindingResult result, String toURL, boolean rememberId,HttpServletRequest request, HttpServletResponse response, Model model) throws EmployeeNotFoundException,Exception  {
        logger.info("POST /login");

        if (result.hasErrors()) throw new EmployeeNotFoundException();


//Global Catcher

@ControllerAdvice("com.brandedcompany.controller")

public class ControllerCatcher {


    final Logger logger = LoggerFactory.getLogger(ControllerCatcher.class);



    @ResponseStatus(HttpStatus.BAD_REQUEST)

    @ExceptionHandler(EmployeeNotFoundException.class)

    public String employeeNotFoundException(BindingResult result, Model model) {
        logger.error("EmployeeNotFoundException");

        for (ObjectError error : result.getAllErrors()) {
            model.addAttribute("message",error.getCode());

            System.out.println("error.getCodes() = " + error.getCodes());

        }

        return "redirect:/login";

In the global catcher class like the code above

I want to receive BindingResult as a parameter, is there any way?

BindingResult is NULL when executed as above.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source