gpt4 book ai didi

java - Play 框架从 2.1 更新到 2.2 : SimpleResult not working - compile error

转载 作者:行者123 更新时间:2023-11-30 08:04:00 25 4
gpt4 key购买 nike

我们正在将项目从 Play Framework 2.1 升级到 2.2。然而,新的结果结构并没有发挥作用。我已在此 Global.java 文件中的“call”函数中将返回类型 Result 更改为 SimpleResult:

public class Global extends GlobalSettings {

@Override
public void onStart(Application app) {
Logger.info("Global On Start event called");
Logger.info("Admin Application has started");
// get schedule duration from Application.conf
Long scheduleDuration = Play.application().configuration().getLong("schedule-duration");
Logger.debug("Scheduler duration is :"+ scheduleDuration) ;
Logger.debug("Calling Schedule Service");
Cancellable response1 = ScheduleService.scheduleIt(scheduleDuration);

// get schedule duration for Worker Task Expiration Scheduler from application.conf
Long workerTaskExpirationDuration = Play.application().configuration().getLong("worker-task-scheduler-interval");
Cancellable response2 = TaskExpirationScheduleService.schedule(workerTaskExpirationDuration);
/******************fix for CEA-3440*************************************/
Long workerEmailDuration = Play.application().configuration().getLong("worker-email-scheduler-interval");
Cancellable response3 = WorkerEmailScheduleService.schedule(workerEmailDuration);
/******************fix for CEA-3440*************************************/
}

@Override
public void onStop(Application app) {
Logger.info("Global On Stop event called");
Logger.info("Application shutdown started..Stopping Sync Service Actor.");
ActorRef syncActor = Akka.system().actorFor("/user/SyncServiceActor");
Akka.system().stop(syncActor);
Logger.info("Application shutdown started..Stopping Worker Task Expiration Actor.");
ActorRef taskExpirationActor = Akka.system().actorFor("/user/WorkerTaskExpireActor");
Akka.system().stop(taskExpirationActor);
/******************fix for CEA-3440*************************************/
Logger.info("Application shutdown started..Stopping Worker Email Actor.");
ActorRef workerTaskExpirationDuration = Akka.system().actorFor("/user/WorkerTaskExpirationDuration");
Akka.system().stop(workerTaskExpirationDuration);
/******************fix for CEA-3440*************************************/
// shut down system
Akka.system().shutdown();
Logger.info("Actor stopped and Akka System shutdown successfully..");
}




// For CORS
private class ActionWrapper extends Action.Simple {
public ActionWrapper(Action<?> action) {
this.delegate = action;
}

@Override
public Promise<SimpleResult> call(Http.Context ctx) throws java.lang.Throwable {
Http.Response response = ctx.response();
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE");
response.setHeader("Allow", "*");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Referer, User-Agent");
response.setHeader("Access-Control-Allow-Credentials", "true");
Promise<SimpleResult> result = (Promise<SimpleResult>) this.delegate.call(ctx);
return result;
}
}

@Override
public Action<?> onRequest(Http.Request request, java.lang.reflect.Method actionMethod) {

return new ActionWrapper(super.onRequest(request, actionMethod));
}

}

在浏览器上编译时会出现以下错误:

Compiled Play Project

我尝试通过查看类似的 StackOverflow 问题来解决此问题,但无法解决该问题。我对 Play 比较陌生,因此请详细说明您的解决方案。

此外,我使用 Eclipse 作为 IDE,但出现以下错误:

The return type is incompatible with Action.call(Http.Context)

谢谢。

最佳答案

只需将 SimpleResult 替换为 Result 即可?

这是一个操作示例

public F.Promise<Result> call( Http.Context context ) throws Throwable {

if ( !authorized ) {
return F.Promise.promise(() -> unauthorized());
}

// execute the action
return delegate.call( context );
}

这是我的 Global.java

@Override
public F.Promise<Result> onError(Http.RequestHeader requestHeader, Throwable throwable) {
if(Play.isDev()){
return super.onError(requestHeader, throwable);
}
Logger.error("Error : ", throwable);
return F.Promise.pure(Controller.ok(views.html.error.render(requestHeader.uri())));
}

您应该阅读https://www.playframework.com/documentation/2.2.0/Migration22#New-results-structure-in-Java

关于java - Play 框架从 2.1 更新到 2.2 : SimpleResult not working - compile error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31426295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com