Versão atual:

É possível obter um objeto HttpServletRequest em uma Metoto @POST em uma RESFUL?

Olá,

Implementei um filtro que injeta o entitymanager na requisição Http, quando uso um método @GET posso obter a requisição usando a annotation @context, mas no método @POST acontece esse erro:

The server encountered an internal error () that prevented it from fulfilling this request

org.jboss.resteasy.spi.UnhandledException: java.lang.NullPointerException
    org.jboss.resteasy.core.SynchronousDispatcher.handleApplicationException(SynchronousDispatcher.java:340)
    org.jboss.resteasy.core.SynchronousDispatcher.handleException(SynchronousDispatcher.java:214)
    org.jboss.resteasy.core.SynchronousDispatcher.handleInvokerException(SynchronousDispatcher.java:190)
    org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:540)
    org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:502)
    org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:119)

Esse é meu filtro:

package br.com.serverchattcc.filter;

import java.io.IOException;

import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

@WebFilter(servletNames={"Faces Servlets"})
public class JPAFilter implements Filter {

    private EntityManagerFactory factory;

    @Override
    public void destroy() {
        this.factory.close();
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {

        EntityManager entityManager = this.factory.createEntityManager();
        request.setAttribute("entityManager", entityManager);

        try {
            entityManager.getTransaction().begin();
        } catch (Exception e) {
            System.out.println("-------------------Erro ao abrir transação--------------------");
        }

        chain.doFilter(request, response);

        try {
            entityManager.getTransaction().commit();
        } catch (Exception e) {
            entityManager.getTransaction().rollback();
        } finally{
            entityManager.close();
        }

    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        this.factory = Persistence.createEntityManagerFactory("projetotcc");
    }

}

E essa é meu Web Service:

package br.com.serverchattcc.resources;

import javax.persistence.EntityManager;
import javax.ws.rs.Consumes;
import javax.ws.rs.Encoded;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import br.com.serverchattcc.adapter.UsuarioEntityJsonAdapter;
import br.com.serverchattcc.enuns.Charset;
import br.com.serverchattcc.json.elements.UsuarioJson;
import br.com.serverchattcc.repository.UsuarioRepository;

@Path("/usuario")
public class UsuarioResource {

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.TEXT_PLAIN)
    @Path("/add")
    public String add(@Context HttpServletRequest request, 
                    @FormParam("usuario") UsuarioJson usuario) {

       try{
        EntityManager entityManager = (EntityManager) request.getAttribute("entityManager ");
            UsuarioRepository usuarioRepository = new UsuarioRepository(entityManager);
            UsuarioEntityJsonAdapter usuarioAdapter = new UsuarioEntityJsonAdapter();
            usuarioRepository.persist(usuarioAdapter.convertJsonToEntity(usuario));
            return "ok";
        } catch(Exception e){
            return "erro##@@"+e.getMessage();
        }
    }

}

Versões(2):

Ver a versão formatada

É possível obter um objeto HttpServletRequest em uma Metoto @POST em uma RESFUL?

Comentário

new question