SpringBoot @Recover не работает для исключений, определенных пользователем - PullRequest
0 голосов
/ 30 апреля 2020

    Iam trying to execute @Recover for the user defined exception but its not getting executed.Kindly help

Контроллер

    @Retryable(value={StudentNotFoundException.class},maxAttempts=2)
    @RequestMapping(value = "/api/getStudents/{sid}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody List<Student> getStudentsbyid(@PathVariable(value = "sid") Integer sid) {
        int count =0;
        count++;
        logger.info("Status of the Counter"+count);
        try (Session session = HibernateUtil.getSessionFactory().openSession()) {
           Query q= session.createQuery("select s from Student s where s.sid = :sid");
           q.setParameter("sid", sid);
           List<Student>st=q.list();
           if(st!=null && st.size()<=0){
               throw new StudentNotFoundException("Student: "+sid+ " Not Found" );
           }
            return st;
        }
    }


Метод восстановления

    @Recover
    public void recoverStudentNotFoundException(StudentNotFoundException stx){

        logger.info(stx.toString());
    }

Определено пользователем Исключение

@ResponseStatus(HttpStatus.NOT_FOUND)
public class StudentNotFoundException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    public StudentNotFoundException(String message) {
        super(message);
      }
}
...