lundi 25 mai 2015

Cannot use properly (@Inject).

When i try to use @Inject to inject my DAO class, in a manager to make it able to give a gson file to a current address i get this exception when i go to the specific web address.Can someone explain me what is the problem. I thought that the problem come maybe from the @Inject and it does not work correctly, but i am not sure.

java.lang.RuntimeException: org.apache.cxf.interceptor.Fault: Cannot obtain a free instance.; nested exception is: 
javax.enterprise.inject.UnsatisfiedResolutionException: Api type [cinema.dao.ProjectionDAO] is not found with the qualifiers 
Qualifiers: [@javax.enterprise.inject.Default()]
for injection into Field Injection Point, field name :  projectionDAO, Bean Owner : [ProjectionManager, Name:null, WebBeans Type:DEPENDENT, API Types:[cinema.services.ProjectionManager,java.lang.Object], Qualifiers:[javax.enterprise.inject.Any,javax.enterprise.inject.Default]] while invoking public java.util.Collection cinema.services.ProjectionManager.getAllProjections() with params [].
org.apache.cxf.interceptor.AbstractFaultChainInitiatorObserver.onMessage(AbstractFaultChainInitiatorObserver.java:116)
org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:324)
org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:240)
org.apache.openejb.server.cxf.rs.CxfRsHttpListener.onMessage(CxfRsHttpListener.java:187)
org.apache.openejb.server.rest.RsServlet.service(RsServlet.java:53)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Code for the manager:

package cinema.services;

import java.util.Collection;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import cinema.dao.ProjectionDAO;
import cinema.model.Projection;

@Stateless
@Path("projection")
public class ProjectionManager {

@Inject
private ProjectionDAO projectionDAO;

@GET
@Produces("application/json")
public Collection<Projection> getAllProjections(){
    return projectionDAO.getAllProjections();
}

}

Here is the ProjectionDAO:

package cinema.dao;

import java.util.Collection;

import javax.inject.Singleton;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

import cinema.model.Projection;
import cinema.model.User;

@Singleton
public class ProjectionDAO {

@PersistenceContext
private EntityManager em;


public Collection<Projection> getAllProjections(){
    return em.createNamedQuery("getAllProjections", Projection.class).getResultList();
}

public void addProjection(Projection projection){

        em.persist(projection);

}

public Projection findProjectionByMovieTitle(String movieTitle){
    try {
        return em.createNamedQuery("getProjectionByMovieTitle", Projection.class)
            .setParameter("movieTitle", movieTitle).getSingleResult();
    } catch (NoResultException e){
        return null;
    }
}

public void buyTicket(Projection projection, User user){
    Projection foundProjection = findProjectionByMovieTitle(projection.getMovieTitle());
    if(foundProjection != null){
        user.getCurrentProjections().add(projection);
        int newFreeSpaces = foundProjection.getFreeSpaces() - 1;
        foundProjection.setFreeSpaces(newFreeSpaces);
    }
}

}

Projection is a simple model which give the movieTitle and start time of different projections in a cinema.

Aucun commentaire:

Enregistrer un commentaire