Vaadin 12, javaScript: java.lang.IllegalArgumentException: Can't encode class java.util.HashMap to json

我与影子孤独终老i 提交于 2019-12-11 07:37:01

问题


In my Vaadin 12 and Kotlin project:

Here myhtml.html

<!DOCTYPE html>
<html>
    <head>
        <script src="https://myjavascript.js"></script>
        <script>
            var checkObject = Util(
                'pk_aaaa',
                {
                        betas: ['bbbb_4']
                }
            );
            window.checkObject = checkObject;
            function goToCheckout(){
                checkObject.redirectToCheckout({
                    sessionId: "abcddddddddd",
                })
            }
        </script>
    </head>
</html>

And this page success call method redirectToCheckout in browser. But I need to do this from Vaadin 12.

I need to call method checkObject.redirectToCheckout from my Vaadin code:

import com.vaadin.flow.component.UI
import com.vaadin.flow.component.dependency.HtmlImport
import com.vaadin.flow.component.dependency.JavaScript
import com.vaadin.flow.component.html.Div
import com.vaadin.flow.router.Route
import com.vaadin.flow.server.VaadinRequest
import org.slf4j.LoggerFactory
@Route(value = "redir")
@JavaScript("https://myjavascript.js")
@HtmlImport("styles/myhtml.html")
class RedirectForm : Div() {
    private val logger = LoggerFactory.getLogger(this::class.java)

    init {
        val request = VaadinRequest.getCurrent()
        val param = request.getParameter("myParam")
        val map = HashMap<String, String>()
        map["sessionId"] = param 
        UI.getCurrent().getPage().executeJavaScript("checkObject.redirectToCheckout($0)", map)

    }

}

But I get error:

Caused by: java.lang.IllegalArgumentException: Can't encode class java.util.HashMap to json

2019-04-02 18:09:26,835 12247 [qtp1750286943-20] ERROR c.v.flow.router.InternalServerError - There was an exception while trying to navigate to 'redir'
java.lang.IllegalArgumentException: Unable to create an instance of 'myproject.view.RedirectForm'. The constructor threw an exception.
    at com.vaadin.flow.internal.ReflectTools.createProxyInstance(ReflectTools.java:510)
    at com.vaadin.flow.internal.ReflectTools.createInstance(ReflectTools.java:447)
    at com.vaadin.flow.di.DefaultInstantiator.getOrCreate(DefaultInstantiator.java:65)
    at com.vaadin.flow.di.Instantiator.createRouteTarget(Instantiator.java:158)
    at com.vaadin.flow.router.internal.AbstractNavigationStateRenderer.lambda$getRouteTarget$1(AbstractNavigationStateRenderer.java:121)
    at java.util.Optional.orElseGet(Optional.java:267)
    at com.vaadin.flow.router.internal.AbstractNavigationStateRenderer.getRouteTarget(AbstractNavigationStateRenderer.java:120)
    at com.vaadin.flow.router.internal.AbstractNavigationStateRenderer.handle(AbstractNavigationStateRenderer.java:178)
    at com.vaadin.flow.router.Router.handleNavigation(Router.java:219)
    at com.vaadin.flow.router.Router.navigate(Router.java:190)
    at com.vaadin.flow.router.Router.initializeUI(Router.java:92)
    at com.vaadin.flow.server.BootstrapHandler.createAndInitUI(BootstrapHandler.java:1117)
    at com.vaadin.flow.server.BootstrapHandler.synchronizedHandleRequest(BootstrapHandler.java:396)
    at com.vaadin.flow.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:40)
    at com.vaadin.flow.server.VaadinService.handleRequest(VaadinService.java:1507)
    at com.vaadin.flow.server.VaadinServlet.service(VaadinServlet.java:242)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:867)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1623)
    at org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter.doFilter(WebSocketUpgradeFilter.java:214)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1610)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:540)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:146)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
    at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:257)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1588)
    at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:255)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1345)
    at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:203)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:480)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1557)
    at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:201)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1247)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:144)
    at org.eclipse.jetty.server.handler.HandlerList.handle(HandlerList.java:61)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
    at org.eclipse.jetty.server.Server.handle(Server.java:502)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:364)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:260)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:305)
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:103)
    at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:118)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:765)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:683)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException: null
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.vaadin.flow.internal.ReflectTools.createProxyInstance(ReflectTools.java:479)
    ... 45 common frames omitted
Caused by: java.lang.IllegalArgumentException: Can't encode class java.util.HashMap to json
    at com.vaadin.flow.internal.JsonCodec.encodeWithoutTypeInfo(JsonCodec.java:165)
    at com.vaadin.flow.internal.JsonCodec.encodeWithTypeInfo(JsonCodec.java:80)
    at com.vaadin.flow.component.page.Page.executeJavaScript(Page.java:338)
    at myproject.view.RedirectForm.<init>(RedirectView.kt:29)
    ... 50 common frames omitted

回答1:


Use JsonObject instead HashMap:

val request = VaadinRequest.getCurrent()
val param = request.getParameter("myParam")
val json = Json.createObject()
json.put("sessionId", param)
UI.getCurrent().getPage().executeJavaScript("checkObject.redirectToCheckout($0)", json)


来源:https://stackoverflow.com/questions/55478212/vaadin-12-javascript-java-lang-illegalargumentexception-cant-encode-class-ja

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!