2017-02-09 57 views
0

我碰到下面的堆棧跟蹤,每當我把我的任何方法與播放與呆錯誤

@SubjectRequired@SubjectNotRequired註釋(邦特不加載的頁面上!):

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[CompletionException: java.lang.NullPointerException]] 
    at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:293) 
    at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:220) 
    at play.api.GlobalSettings$class.onError(GlobalSettings.scala:160) 
    at play.api.DefaultGlobal$.onError(GlobalSettings.scala:188) 
    at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:100) 
    at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:100) 
    at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:99) 
    at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:344) 
    at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:343) 
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32) 
Caused by: java.util.concurrent.CompletionException: java.lang.NullPointerException 
    at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:273) 
    at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:280) 
    at java.util.concurrent.CompletableFuture.uniCompose(CompletableFuture.java:961) 
    at java.util.concurrent.CompletableFuture$UniCompose.tryFire(CompletableFuture.java:926) 
    at java.util.concurrent.CompletableFuture$Completion.run(CompletableFuture.java:442) 
    at play.core.j.HttpExecutionContext$$anon$2.run(HttpExecutionContext.scala:56) 
    at play.core.j.HttpExecutionContext$$anon$2.run(HttpExecutionContext.scala:56) 
    at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:39) 
    at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:415) 
    at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) 
Caused by: java.lang.NullPointerException: null 
    at be.objectify.deadbolt.java.cache.DefaultSubjectCache.apply(DefaultSubjectCache.java:80) 
    at be.objectify.deadbolt.java.cache.DefaultSubjectCache.apply(DefaultSubjectCache.java:38) 
    at be.objectify.deadbolt.java.ConstraintLogic.getSubject(ConstraintLogic.java:348) 
    at be.objectify.deadbolt.java.ConstraintLogic.subjectTest(ConstraintLogic.java:116) 
    at be.objectify.deadbolt.java.ConstraintLogic.subjectPresent(ConstraintLogic.java:72) 
    at be.objectify.deadbolt.java.actions.SubjectPresentAction.lambda$testSubject$129(SubjectPresentAction.java:91) 
    at java.util.Optional.orElseGet(Optional.java:267) 
    at be.objectify.deadbolt.java.actions.AbstractSubjectAction.lambda$execute$118(AbstractSubjectAction.java:76) 
    at java.util.concurrent.CompletableFuture.uniCompose(CompletableFuture.java:952) 
    at java.util.concurrent.CompletableFuture$UniCompose.tryFire(CompletableFuture.java:926) 

我的代碼如下像這樣:

在課堂上實現DeadboltHandler

@Override 
public CompletionStage<Optional<? extends Subject>> getSubject(Context elContext) { 
    return CompletableFuture.supplyAsync(
     () -> { 
      try { 
       return Optional.ofNullable(da.getUser(Integer.parseInt(elContext.session().get("userId")))); 
      } catch (Exception e) { 
       e.printStackTrace(); 
       return null; 
      } 
     }); 
} 

這裏是有一個構造函數:

DataAccess da; 

DeadboltSecureHandler(Database db){ 
    da = new DataAccess(db); 
} 

,並在類實現HandlerCache

@Inject 
public DeadboltHandlerCache(Database db){ 
    DeadboltHandler defaultHandler = new DeadboltSecureHandler(db); 
    handlers.put(HandlerKeys.DEFAULT.key, defaultHandler); 
} 

@Override 
public DeadboltHandler apply(final String key){ 
    return handlers.get(key); 
} 

@Override 
public DeadboltHandler get(){ 
    return defaultHandler; 
} 

什麼我搞砸任何想法?

回答

1

一些東西......

deadboltHandler你使用是空的,這是最可能的解釋是HandlerKeys.DEFAULT.key是不一樣的be.objectify.deadbolt.java.ConfigKeys.DEFAULT_HANDLER_KEY

如果您檢查documentation,你會發現下面的位(這使我您使用的版本呆的比舊的2.5.3猜測):

的使用注意事項ConfigKeys的。 DEFAULT_HANDLER_KEY - 這是所有註釋中指定的默認處理程序密鑰。在之前的Deadbolt版本中,註釋驅動約束將使用HandlerCache#apply(DEFAULT_HANDLER_KEY)來獲取處理程序,因此默認處理程序必須與DEFAULT_HANDLER_KEY關聯。截至Deadbolt 2.5.3,這已得到改進,使用默認處理程序密鑰的任何註釋驅動約束將改爲使用HandlerCache#get

你要與你當前的代碼打接下來的問題是:

當您返回null,你打破了getSubject方法的合同。無論何時返回CompletionStage<Optional<? extends Subject>>CompletionStage必須包含Optional。而不是返回null,返回Optional.empty()