How to set priority in ExceptionHandling via ControllerAdvice

exception, spring

Solution

To set Higher priority to an ControllerAdvice on add :

import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import com.genealogytree.webapplication.dispatchers.requestUserMapper;

@ControllerAdvice(assignableTypes = { requestUserMapper.class })
@Order(Ordered.HIGHEST_PRECEDENCE)
public class UserAdvice {
...
}

To set Lower priority to an ControolerAdvice on add

import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import com.genealogytree.webapplication.dispatchers.requestUserMapper;

@ControllerAdvice(assignableTypes = { requestUserMapper.class })
@Order(Ordered.LOWEST_PRECEDENCE)
public class CommonAdvice {
...
}

Problem

I was implement 2 ControllersAdvice to. handle exception CommonAdvice and UserAdvice Common Advice ``` @ControllerAdvice(annotations = RestController.class) public class CommonAdvice { @ExceptionHandler(Exception.class) public ResponseEntity<ExceptionBean> handleException(Exception e) { ExceptionBean exception = new ExceptionBean(Causes.ANOTHER_CAUSE); return new ResponseEntity<ExceptionBean>(exception, HttpStatus.INTERNAL_SERVER_ERROR); } } ``` UserAdvice ``` @ControllerAdvice(assignableTypes = { requestUserMapper.class }) public class UserAdvice { @ExceptionHandler(NotUniqueUserLoginException.class) public ResponseEntity<ExceptionBean> handleAlreadyFound(NotUniqueUserLoginException e) { System.out.println("this is me : " + Causes.USER_ALREADY_EXIST.toString()); ExceptionBean exception = new ExceptionBean(Causes.USER_ALREADY_EXIST); return new ResponseEntity<ExceptionBean>(exception, HttpStatus.INTERNAL_SERVER_ERROR); } ``` And now, when I throw NotUniqueUserException, this is a CommonAdvice which handle and exception. I tested and UserAdvice works fine. There is the way to set priority on this classes ? @Edit - add Controllel Mapping ``` @RequestMapping(value = "add", method = RequestMethod.POST) public ResponseEntity<GT_User> addUser(@RequestBody GT_User newUser) throws NotUniqueUserLoginException, Exception { if (this.userService.exist(newUser.getLogin())) { throw new NotUniqueUserLoginException(Causes.USER_ALREADY_EXIST.toString()); } else { GT_User addesUser = this.userService.addUser(newUser); return new ResponseEntity<GT_User>(addesUser, HttpStatus.OK); } } ```

Original source

Related problems