Solucionado (ver solução)
Solucionado
(ver solução)
6
respostas

org.jboss.resteasy.spi.LoggableFailure: RESTEASY003880: Unable to find contextual data of type: javax.servlet.ServletContext

@Path("/pagamento")
public class PagamentoService {

    @Inject
    private CompraDao compraDao;

    @Context
    ServletContext context;

    @Inject
    private PagamentoGateway pagamentoGateway;

    private static ExecutorService executor = Executors.newFixedThreadPool(50);

    @POST
    public void paga(@Suspended AsyncResponse ar, @QueryParam("uuid") String uuid) {
        executor.submit(()->{
            try {
                Compra compra = compraDao.buscaPorUuid(uuid);
                pagamentoGateway.paga(compra.getTotal());
                URI uri = UriBuilder.fromPath("http://localhost:8080"+ context.getContextPath() + "/index.xhtml")
                .queryParam("msg", "Compra realizada com sucesso")
                .build();
                Response response = Response.seeOther(uri).build();
                ar.resume(response);
            }catch(Exception e) {
                ar.resume(new WebApplicationException(e));
            }
        });
    }
}
03:04:56,220 ERROR [stderr] (pool-5-thread-1) org.jboss.resteasy.spi.LoggableFailure: RESTEASY003880: Unable to find contextual data of type: javax.servlet.ServletContext
03:04:56,221 ERROR [stderr] (pool-5-thread-1)     at org.jboss.resteasy.core.ContextParameterInjector$GenericDelegatingProxy.invoke(ContextParameterInjector.java:62)
03:04:56,221 ERROR [stderr] (pool-5-thread-1)     at com.sun.proxy.$Proxy99.getContextPath(Unknown Source)
03:04:56,222 ERROR [stderr] (pool-5-thread-1)     at br.com.casadocodigo.loja.services.PagamentoService.lambda$0(PagamentoService.java:43)
03:04:56,222 ERROR [stderr] (pool-5-thread-1)     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
03:04:56,222 ERROR [stderr] (pool-5-thread-1)     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
03:04:56,223 ERROR [stderr] (pool-5-thread-1)     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
03:04:56,223 ERROR [stderr] (pool-5-thread-1)     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
03:04:56,223 ERROR [stderr] (pool-5-thread-1)     at java.lang.Thread.run(Thread.java:748)
03:05:08,036 ERROR [stderr] (pool-5-thread-1) org.jboss.resteasy.spi.LoggableFailure: RESTEASY003880: Unable to find contextual data of type: javax.servlet.ServletContext
03:05:08,036 ERROR [stderr] (pool-5-thread-1)     at org.jboss.resteasy.core.ContextParameterInjector$GenericDelegatingProxy.invoke(ContextParameterInjector.java:62)
03:05:08,037 ERROR [stderr] (pool-5-thread-1)     at com.sun.proxy.$Proxy99.getContextPath(Unknown Source)
03:05:08,037 ERROR [stderr] (pool-5-thread-1)     at br.com.casadocodigo.loja.services.PagamentoService.lambda$0(PagamentoService.java:43)
03:05:08,037 ERROR [stderr] (pool-5-thread-1)     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
03:05:08,038 ERROR [stderr] (pool-5-thread-1)     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
03:05:08,038 ERROR [stderr] (pool-5-thread-1)     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
03:05:08,038 ERROR [stderr] (pool-5-thread-1)     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
03:05:08,039 ERROR [stderr] (pool-5-thread-1)     at java.lang.Thread.run(Thread.java:748)
03:05:20,690 ERROR [org.jboss.resteasy.resteasy_jaxrs.i18n] (pool-5-thread-1) RESTEASY002010: Failed to execute: javax.ws.rs.WebApplicationException: HTTP 500 Internal Server Error
    at br.com.casadocodigo.loja.services.PagamentoService.lambda$0(PagamentoService.java:49)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.jboss.resteasy.spi.LoggableFailure: RESTEASY003880: Unable to find contextual data of type: javax.servlet.ServletContext
    at org.jboss.resteasy.core.ContextParameterInjector$GenericDelegatingProxy.invoke(ContextParameterInjector.java:62)
    at com.sun.proxy.$Proxy99.getContextPath(Unknown Source)
    at br.com.casadocodigo.loja.services.PagamentoService.lambda$0(PagamentoService.java:43)
    ... 5 more
6 respostas

Oi Rafael,

Tenta injetar um objeto HttpServletRequest ao invés do ServletContext:

@Context
private HttpServletRequest request;

Para pegar o contextPath é o mesmo método:

request.getContextPath()

Veja se resolve.

Bons estudos!

Ainda deu erro:

17:13:53,615 ERROR [org.jboss.resteasy.resteasy_jaxrs.i18n] (pool-4-thread-1) RESTEASY002010: Failed to execute: javax.ws.rs.WebApplicationException: HTTP 500 Internal Server Error
    at br.com.casadocodigo.loja.services.PagamentoService.lambda$0(PagamentoService.java:49)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.jboss.resteasy.spi.LoggableFailure: RESTEASY003880: Unable to find contextual data of type: javax.servlet.http.HttpServletRequest
    at org.jboss.resteasy.core.ContextParameterInjector$GenericDelegatingProxy.invoke(ContextParameterInjector.java:62)
    at com.sun.proxy.$Proxy115.getContextPath(Unknown Source)
    at br.com.casadocodigo.loja.services.PagamentoService.lambda$0(PagamentoService.java:43)
    ... 5 more

Segue meu pom.xml

<?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.casadocodigo</groupId>
    <artifactId>casadocodigo</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
        <finalName>casadocodigo</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.jboss.spec</groupId>
                <artifactId>jboss-javaee-6.0</artifactId>
                <version>3.0.3.Final</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>javax.annotation</groupId>
                <artifactId>jsr250-api</artifactId>
                <version>1.0</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.jboss.spec.javax.faces</groupId>
            <artifactId>jboss-jsf-api_2.2_spec</artifactId>
            <version>2.2.14</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>jsr250-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.0.Draft-16</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>javax.transaction-api</artifactId>
            <version>1.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.10.Final</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.10.Final</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.faces</groupId>
            <artifactId>javax.faces-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.omnifaces</groupId>
            <artifactId>omnifaces</artifactId>
            <version>2.1</version>
        </dependency>
    </dependencies>
</project>

Estou usando wildfly 11

solução!

Para resolver precisei extrair para um variável e tirar de dentro da thread:

String contextPath =context.getContextPath();
@Path("/pagamento")
public class PagamentoService {

    @Inject
    private CompraDao compraDao;

    @Context
    ServletContext context;

    @Inject
    private PagamentoGateway pagamentoGateway;

    private static ExecutorService executor = Executors.newFixedThreadPool(50);

    @POST
    public void paga(@Suspended AsyncResponse ar, @QueryParam("uuid") String uuid) {
    String contextPath =context.getContextPath();
        executor.submit(()->{
            try {
                Compra compra = compraDao.buscaPorUuid(uuid);
                pagamentoGateway.paga(compra.getTotal());
                URI uri = UriBuilder.fromPath("http://localhost:8080"+ contextPath + "/index.xhtml")
                .queryParam("msg", "Compra realizada com sucesso")
                .build();
                Response response = Response.seeOther(uri).build();
                ar.resume(response);
            }catch(Exception e) {
                ar.resume(new WebApplicationException(e));
            }
        });
    }
}

Oi Rafael,

Que bom que deu certo então.

Deve dar erro então , por algum motivo, de acessar essa variavel injetada com @Context dentro da expressão Lambda :D

Sim, pelo visto ele perde o contexto se você chama dentro da sub thread.