2016-10-30 30 views
1

2つのControllersAdviceを実装しました。私はNotUniqueUserExceptionを投げるときに例外 CommonAdviceとUserAdviceControllerAdvice経由でExceptionHandlingの優先順位を設定する方法

共通のアドバイス

@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); 
    } 

そして今を扱う、これは、ハンドルと例外CommonAdviceです。

私はテストし、UserAdviceは問題なく動作します。 このクラスに優先順位を設定する方法はありますか?

@Edit - アドオンのControllerAdviceに高い優先度を設定するにはControllelマッピング

@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); 
    } 
} 
+0

にControolerAdviceに低い優先度を設定するには

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 { ... } 

することができます実際のコントローラコードと例外スタックトレースを提供しますか? – developer

+0

はこちらです。しかし、私がすでに言ったように、いつ。 hadleException()がコメントされている場合、handleAlreadyFound()は正常に動作します。 handleAlreadyfound()をhandleExceptionの前に同じクラスに追加すると、それも機能します。だから、これは唯一のpiorityの問題です...私は.. – VANILKA

+0

OK ...申し訳ありません...私は解決策を見つけました。問題は..私の英語...私は優先順位を探しました。 ORDERの代わりに...シンプルなアノテーション@Orderは私の問題を解決します。 – VANILKA

答えて

1

を追加:追加

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 { 
... 
} 
関連する問題