在Play 2.5.18中使用FormFactory会导致InvocationTargetException错误

时间:2018-04-10 20:01:44

标签: java forms playframework

我正在升级Play 2.5.18中的Form对象,以使用此处列出的FormFactory对象:

https://www.playframework.com/documentation/2.5.18/JavaForms

我的控制器看起来像这样:

public class Application extends Controller {

    public final MailerClient mailerClient;
    public final FormFactory formFactory;
    public final MessagesApi messagesApi;
    public final Collection<Lang> candidates;
    public final Messages messages;

    @Inject
    public Application(MailerClient mailerClient, FormFactory formFactory, MessagesApi messagesApi) {
        this.mailerClient = mailerClient;
        this.formFactory = formFactory;
        this.messagesApi = messagesApi;
        this.candidates = Collections.singletonList(new Lang(Locale.US));
        this.messages = messagesApi.preferred(candidates);
    }

    public Result authenticate() {
        String errorMessage = "";

        Form<Login> loginForm = formFactory.form(Login.class).bindFromRequest();
        Logger.debug("authenticate");

        ...
    }
    ...
}

当我运行应用程序并尝试登录时,我收到此错误:

! @77hah0j80 - Internal server error, for (POST) [/auth] ->

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[RuntimeException: java.lang.reflect.InvocationTargetException]]
        at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:255)
        at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:182)
        at play.core.server.AkkaHttpServer$$anonfun$$nestedInanonfun$executeHandler$1$1.applyOrElse(AkkaHttpServer.scala:230)
        at play.core.server.AkkaHttpServer$$anonfun$$nestedInanonfun$executeHandler$1$1.applyOrElse(AkkaHttpServer.scala:229)
        at scala.concurrent.Future.$anonfun$recoverWith$1(Future.scala:414)
        at scala.concurrent.impl.Promise.$anonfun$transformWith$1(Promise.scala:37)
        at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:60)
        at play.api.libs.streams.Execution$trampoline$.executeScheduled(Execution.scala:109)
        at play.api.libs.streams.Execution$trampoline$.execute(Execution.scala:71)
        at scala.concurrent.impl.CallbackRunnable.executeWithValue(Promise.scala:68)
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
        at play.data.Form.callLegacyValidateMethod(Form.java:495)
        at play.data.Form.bind(Form.java:530)
        at play.data.Form.bindFromRequest(Form.java:257)
        at controllers.Application.authenticate(Application.java:342)
        at router.Routes$$anonfun$routes$1.$anonfun$applyOrElse$64(Routes.scala:2254)
        at play.core.routing.HandlerInvokerFactory$$anon$3.resultCall(HandlerInvoker.scala:136)
        at play.core.routing.HandlerInvokerFactory$$anon$3.resultCall(HandlerInvoker.scala:135)
        at play.core.routing.HandlerInvokerFactory$JavaActionInvokerFactory$$anon$8$$anon$2$$anon$1.invocation(HandlerInvoker.scala:110)
        at play.core.j.JavaAction$$anon$1.call(JavaAction.scala:78)
        at play.http.DefaultActionCreator$1.call(DefaultActionCreator.java:31)
Caused by: java.lang.reflect.InvocationTargetException: null
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at play.data.Form.callLegacyValidateMethod(Form.java:486)
        at play.data.Form.bind(Form.java:530)
        at play.data.Form.bindFromRequest(Form.java:257)
        at controllers.Application.authenticate(Application.java:342)
        at router.Routes$$anonfun$routes$1.$anonfun$applyOrElse$64(Routes.scala:2254)
        at play.core.routing.HandlerInvokerFactory$$anon$3.resultCall(HandlerInvoker.scala:136)
Caused by: java.lang.NullPointerException: null
        at play.i18n.MessagesApi.preferred(MessagesApi.java:131)
        at controllers.Application$Login.validate(Application.java:133)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at play.data.Form.callLegacyValidateMethod(Form.java:486)
        at play.data.Form.bind(Form.java:530)
        at play.data.Form.bindFromRequest(Form.java:257)
        at controllers.Application.authenticate(Application.java:342)

错误:

at controllers.Application.authenticate(Application.java:342)

是这行代码:

Form<Login> loginForm = formFactory.form(Login.class).bindFromRequest();

我找到了一些帖子,但他们似乎已经指出了我正在做的事情:

The method form(Class<T>) from Form class is deprecated in Play! Framework

formFactory.form() doesn't exist ! PlayFramework

我认为这是一个简单的解决方案,并感谢您的帮助。

----------------------------编辑------------------ ------------

Eclipse中变量的图像: enter image description here

1 个答案:

答案 0 :(得分:0)

由于某种原因,您的messagesApi对象null似乎存在实际问题。请执行以下操作。

  1. 将断点放在Application.java的构造函数中并验证

  2. 检查所有配置,了解messageApi对象注入为null的原因;