3

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);
    }
}
VANILKA
  • 634
  • 1
  • 13
  • 32
  • can you provide the actual controller code and exception stacktrace? – Vasu Oct 30 '16 at 22:45
  • here you are. But as I told already, When. hadleException() is commented, the handleAlreadyFound() works fine. When I add handleAlreadyfound() int the same class, before handleException) it works too. So, this Is only a problem with piority... I think.. – VANILKA Oct 30 '16 at 23:10
  • OK... sorry... I have found solution. The problem was.. MY ENGLISH... I was searched priority. instead of ORDER... Simple annotation @Order resolve my problem. – VANILKA Oct 30 '16 at 23:17
  • Great! You could post the working solution as an answer and accept your own answer! – alexbt Oct 30 '16 at 23:24
  • eeee how can I do this ? – VANILKA Oct 30 '16 at 23:25
  • Possible duplicate of [Setting Precedence of Multiple @ControllerAdvice @ExceptionHandlers](https://stackoverflow.com/questions/19498378/setting-precedence-of-multiple-controlleradvice-exceptionhandlers) – approxiblue Nov 27 '17 at 19:01

1 Answers1

4

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 {
...
}
VANILKA
  • 634
  • 1
  • 13
  • 32