NullPointerException ao renderizar View com Java EE 8 MVC 1.0 + ReactJS

23/03/2018

0

E ai pessoal, blz?

Estou tentando criar uma aplicação com Java EE 8 MVC + ReactJS rodando no Nashorn do JDK9 mas estou enroscado no maldito NullPointerException e não consigo identificar o porque dele. Parece ser algo relacionado ao JavaScript mas não sei como debugar ele no Intellij.

Estou utilizando como base o EngineView do Ozark, os módulos JS são compilados com NPM e estou utilizando o Webpack e Babel. Perdoem a minha ignorância, não tenho muita experiência com o mundo JavaScript mas queria muito fazer esse projeto rodar para utilizar em meus projetos e aumentar a produtividade com relação ao desenvolvimento de Views RIA. Segue abaixo o código da classe que esta lançando a exceção:

public class React {
    @Inject
    private ObjectPool<ScriptEngine> scriptEngineObjectPool;

    String render(String function, Object object){

        Object html;

        try {

            ScriptEngine scriptEngine = scriptEngineObjectPool.borrowObject();

            if(function.contains(".")){
                String[] parts = function.split("\\.");

                html = ((ScriptObjectMirror) scriptEngine.get(parts[0])).callMember(parts[1], object);
            }else{
                html = ((Invocable) scriptEngine).invokeFunction(function, object);
            }

            scriptEngineObjectPool.returnObject(scriptEngine);

            return String.valueOf(html);
        }catch (Exception e){
            throw new IllegalStateException("failed to render react component", e);
        }
    }
}



A exceção esta sendo lançada na linha 16
html = ((ScriptObjectMirror) scriptEngine.get(parts[0])).callMember(parts[1], object);


Abaixo esta a classe ReacViewEngine que implementa a interface ViewEngine do MVC 1.0 (De criação do próprio Niko Köbler, no qual é base para esse meu projeto):

/**
 * @author Niko Köbler, http://www.n-k.de, @dasniko
 */
@Priority(Priorities.FRAMEWORK)
public class ReactViewEngine implements ViewEngine {

    private static final String viewPrefix = "react:";

    @Inject
    React react;

    @Inject
    ServletContext servletContext;

    private ObjectMapper mapper = new ObjectMapper();

    @Override
    public boolean supports(String view) {
        return view.startsWith(viewPrefix);
    }

    @Override
    public void processView(ViewEngineContext context) throws ViewEngineException {
        // parse view and extract the actual template and the react.js function to call
        String view = context.getView();
        String[] viewParts = view.substring(viewPrefix.length()).split("\\\\\\\\?");
        if (viewParts.length < 2) {
            throw new ViewEngineException("You have to specify at least a view and a function (e.g. react:view.jsp?function=renderOnServer)!");
        }

        String template = viewParts[0];

        Map<String, String> params = parseQueryString(viewParts[1]);
        String function = params.get("function");

        String dataKey = params.getOrDefault("data", "data");
        String contentKey = params.getOrDefault("content", "content");

        // get "data" from model
        Models models = context.getModels();
        Object data = models.get(dataKey);

        // call given js function on data
        String content = react.render(function, data);

        // and put results as string in model
        models.put(contentKey, content);
        try {
            models.put(dataKey, mapper.writeValueAsString(data));
        } catch (JsonProcessingException e) {
            throw new ViewEngineException(e);
        }

        try {
            processRequest(context, models, template);
        } catch (ServletException | IOException e) {
            throw new ViewEngineException(e);
        }
    }

    private Map<String, String> parseQueryString(final String query) {
        return Arrays.asList(query.split("&")).stream().map(p -> p.split("=")).collect(Collectors.toMap(s -> s[0], s -> s[1]));
    }

    private void processRequest(final ViewEngineContext context, final Models models, final String view) throws ServletException, IOException {
        RequestDispatcher requestDispatcher = null;
        HttpServletRequest request = context.getRequest();

        for (String name : models) {
            request.setAttribute(name, models.get(name));
        }

        final String viewExtension = getViewExtension(view);
        for (Map.Entry<String, ? extends ServletRegistration> e : servletContext.getServletRegistrations().entrySet()) {
            final Collection<String> mappings = e.getValue().getMappings();
            if (mappings.contains(viewExtension)) {
                requestDispatcher = servletContext.getNamedDispatcher(e.getKey());
                request = new HttpServletRequestWrapper(context.getRequest()) {
                    @Override
                    public String getRequestURI() {
                        return resolveView(context, view);
                    }

                    @Override
                    public String getServletPath() {
                        return getRequestURI();
                    }

                    @Override
                    public String getPathInfo() {
                        return null;
                    }

                    @Override
                    public StringBuffer getRequestURL() {
                        return new StringBuffer(getRequestURI());
                    }
                };
            }
        }

        if (requestDispatcher == null) {
            requestDispatcher = servletContext.getRequestDispatcher(resolveView(context, view));
        }

        requestDispatcher.forward(request, context.getResponse());
    }

    private String getViewExtension(final String view) {
        return "*" + view.substring(view.lastIndexOf("."));
    }

    private String resolveView(final ViewEngineContext context, final String view) {
        if (!view.startsWith("/")) {
            String viewFolder = (String) context.getConfiguration().getProperty(VIEW_FOLDER);
            viewFolder = viewFolder == null ? DEFAULT_VIEW_FOLDER : viewFolder;
            viewFolder += !viewFolder.endsWith("/") ? "/" : "";
            return viewFolder + view;
        }
        return view;
    }
}


Aqui o controller que que retorna a view para ser renderizada no cliente

@Controller
@Path("tic-tac-toe")
public class IndexController {

    @Inject
    Models models;

    @Inject
    MessageService messageService;

    @GET
    public String index() throws IOException{

        models.put("data", messageService.getMessage());

        return "react:index.jsp?function=TicTacToeApp.renderServer";
    }
}
Diego Silva

Diego Silva

Responder

Posts

23/03/2018

Guilherme Borges

Você poderia postar o log da excessão?
Responder

25/03/2018

Diego Silva

Oi Guilherme, esqueci do mais importante, foi mal...


Context Path:
/javaee8-reactor-tic-tac-toe

Servlet Path:
/game

Path Info:
/tic-tac-toe

Query String:
null

Stack Trace:

org.jboss.resteasy.spi.UnhandledException: java.lang.IllegalStateException: failed to render react component
at org.jboss.resteasy.resteasy-jaxrs//org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:257)
at org.jboss.resteasy.resteasy-jaxrs//org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:195)
at org.jboss.resteasy.resteasy-jaxrs//org.jboss.resteasy.core.SynchronousDispatcher.writeResponse(SynchronousDispatcher.java:539)
at org.jboss.resteasy.resteasy-jaxrs//org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:461)
at org.jboss.resteasy.resteasy-jaxrs//org.jboss.resteasy.core.SynchronousDispatcher.lambda$invoke$4(SynchronousDispatcher.java:231)
at org.jboss.resteasy.resteasy-jaxrs//org.jboss.resteasy.core.SynchronousDispatcher.lambda$preprocess$0(SynchronousDispatcher.java:137)
at org.jboss.resteasy.resteasy-jaxrs//org.jboss.resteasy.core.interception.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:361)
at org.jboss.resteasy.resteasy-jaxrs//org.jboss.resteasy.core.SynchronousDispatcher.preprocess(SynchronousDispatcher.java:140)
at org.jboss.resteasy.resteasy-jaxrs//org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:217)
at org.jboss.resteasy.resteasy-jaxrs//org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:227)
at org.jboss.resteasy.resteasy-jaxrs//org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
at org.jboss.resteasy.resteasy-jaxrs//org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
at javax.servlet.api//javax.servlet.http.HttpServlet.service(HttpServlet.java:791)
at io.undertow.servlet//io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74)
at io.undertow.servlet//io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
at io.undertow.servlet//io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:67)
at io.undertow.servlet//io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
at org.wildfly.extension.undertow//org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
at io.undertow.core@2.0.0.Final//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet//io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
at io.undertow.servlet//io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
at io.undertow.core@2.0.0.Final//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.core@2.0.0.Final//io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
at io.undertow.servlet//io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
at io.undertow.core@2.0.0.Final//io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
at io.undertow.servlet//io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
at io.undertow.core@2.0.0.Final//io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
at io.undertow.core@2.0.0.Final//io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
at io.undertow.core@2.0.0.Final//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at org.wildfly.extension.undertow//org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
at io.undertow.core@2.0.0.Final//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at org.wildfly.extension.undertow//org.wildfly.extension.undertow.deployment.GlobalRequestControllerHandler.handleRequest(GlobalRequestControllerHandler.java:68)
at io.undertow.core@2.0.0.Final//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet//io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
at io.undertow.servlet//io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
at io.undertow.servlet//io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
at io.undertow.servlet//io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
at io.undertow.servlet//io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
at io.undertow.servlet//io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
at org.wildfly.extension.undertow//org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create$0(SecurityContextThreadSetupAction.java:105)
at org.wildfly.extension.undertow//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1526)
at org.wildfly.extension.undertow//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1526)
at org.wildfly.extension.undertow//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1526)
at org.wildfly.extension.undertow//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1526)
at io.undertow.servlet//io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
at io.undertow.servlet//io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
at io.undertow.servlet//io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
at io.undertow.core@2.0.0.Final//io.undertow.server.Connectors.executeRootHandler(Connectors.java:360)
at io.undertow.core@2.0.0.Final//io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:830)
at org.jboss.threads@2.3.1.Final//org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
at org.jboss.threads@2.3.1.Final//org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1985)
at org.jboss.threads@2.3.1.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1487)
at org.jboss.threads@2.3.1.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1378)
at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: java.lang.IllegalStateException: failed to render react component
at deployment.javaee8-reactor-tic-tac-toe.war//br.com.diego.view.React.render(React.java:37)
at deployment.javaee8-reactor-tic-tac-t
Responder

25/03/2018

Diego Silva

Estou desconfiando que ele não esta funcionando com as libs js de qualquer forma este é o pom.xml... A tags code do fórum não estão funcionando, não consigo colocar código formatado.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>br.com.diego.tic-tac-toe</groupId>
<artifactId>reactjs-tic-tac-toe-javaee8</artifactId>
<version>1.0-SNAPSHOT</version>

<packaging>war</packaging>

<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
</dependency>
<dependency>
<groupId>javax.mvc</groupId>
<artifactId>javax.mvc-api</artifactId>
<version>1.0-pr</version>
</dependency>
<dependency>
<groupId>org.mvc-spec.ozark</groupId>
<artifactId>ozark-core</artifactId>
<version>1.0.0-m03</version>
</dependency>
<dependency>
<groupId>org.mvc-spec.ozark</groupId>
<artifactId>ozark-jersey</artifactId>
<version>1.0.0-m03</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.23.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.9</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>materializecss</artifactId>
<version>0.97.5</version>
</dependency>
</dependencies>

<build>
<finalName>javaee8-reactor-tic-tac-toe</finalName>
<!-- To define the plugin version in your parent POM -->
<pluginManagement>
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>1.2.1.Final</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>1.2.0.Final</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<configuration>
<nodeVersion>v8.10.0</nodeVersion>
<npmVersion>5.6.0</npmVersion>
</configuration>
</execution>
<execution>
<id>npm install</id>
<goals>
<goal>npm</goal>
</goals>
</execution>
<execution>
<id>webpack build</id>
<goals>
<goal>webpack</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Responder

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar