EJB injection into JAX-RS not working

ejb, glassfish, jakarta-ee, jax-rs

Solution

I too encountered similar problem when migrating from GlassFish 2.1 to GlassFish3.1. So according to following link:

https://blogs.oracle.com/sandoz/entry/jersey_glassfish_and_ejbs_as

Step 1: Create either local or remote interface:

@Remote
@Path("/json")
public interface JSONServiceRemote
{

   @GET
   @Path("/get/login/{login}/{heslo}")
   @Produces(MediaType.APPLICATION_JSON)
   public String getTrackInJSON(@PathParam("login") String login, @PathParam("heslo")   String heslo);

   @GET
   @Path("/get/{param}")
   @Produces(MediaType.APPLICATION_JSON)
   public String getting(@PathParam("param") String msg) ;


}

Step2: Now implement the interface in You stateless EJB JSONService

@Stateless
public class JSONService implements JSONServiceInterface
{

    @Override
    public String getTrackInJSON(@PathParam("login") String login, @PathParam("heslo")     String heslo)
   {

     ...
   }


   @Override
   public String getting(@PathParam("param") String msg) 
   {
       .......
   }

} 

Problem

When i try to run JAX-RS i got error message, how i can get it working? PlayerManagementLocal is @Local of ejb PlayerManagement is working fine. error message ``` com.sun.jersey.api.container.ContainerException: An instance of EJB class rest.JSONService could not be looked up using simple form name or the fully-qualified form name.Ensure that the EJB/JAX-RS component implements at most one interface ``` jaxrs.java ``` package rest; import javax.ejb.EJB; import javax.ejb.Stateful; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import prayForHunt.model.Person; import prayForHunt.sb.PlayerManagement; import prayForHunt.sb.PlayerManagementLocal; @Stateless @Path("/json") public class JSONService { @EJB PlayerManagementLocal playerManagement; @GET @Path("/get/login/{login}/{heslo}") @Produces(MediaType.APPLICATION_JSON) public String getTrackInJSON(@PathParam("login") String login, @PathParam("heslo") String heslo) { return "ahojky"; } @GET @Path("/get/{param}") @Produces(MediaType.APPLICATION_JSON) public String getting(@PathParam("param") String msg) { System.out.println(""); if(playerManagement.checkPlayer(msg)){ return "yes"; } return "no"; } } ``` This is the code, cant figure out way how to get it working - eg....find something in the database and compare it to the string in the POST/GET service. playerManagementLocal.java ``` @Local public interface PlayerManagementLocal { PlayerDTO getPlayers(String login); List<PlayerDTO> getAllPlayers(); String getPassword(String login); void newPlayer(PersonDTO user, String password); void updatePlayer(PlayerDTO player); void updatePlayerCard(PlayerCardDTO playerCard); public void removePlayer(PlayerDTO player); boolean checkPlayer(String login); boolean checkPlayer(String login, String password); void saveLocation(FavoriteLocationDTO loc); List<FavoriteLocationDTO> getPlayerLocations(String login); void removeLocation(int id); } ``` playerManagemet.java ``` package prayForHunt.sb; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import prayForHunt.DTO.FavoriteLocationDTO; import prayForHunt.DTO.PersonDTO; import prayForHunt.DTO.PlayerCardDTO; import prayForHunt.DTO.PlayerDTO; import prayForHunt.model.*; /** * * @author Nell */ @Stateless public class PlayerManagement implements PlayerManagementLocal { @PersistenceContext EntityManager em; @Override public PlayerDTO getPlayers(String login) { Player playerHelp = em.find(Player.class, login); if (playerHelp == null) { return null; } else { return new PlayerDTO(playerHelp); } } @Override public List<PlayerDTO> getAllPlayers() { List<Player> list = (List<Player>) em.createNamedQuery(Player.Q_GET_ALL_PLAYERS).getResultList(); ArrayList<PlayerDTO> listDTO = new ArrayList<PlayerDTO>(); Iterator it = list.iterator(); while (it.hasNext()) { listDTO.add(new PlayerDTO((Player) it.next())); } return listDTO; } @Override public void newPlayer(PersonDTO user, String password) { Player player = user.toPlayer(); player.setPassword(password); PlayerCard card = new PlayerCard(); player.setPlayerCard(card); em.merge(player); } @Override public void updatePlayer(PlayerDTO player) { Player en = em.find(Player.class, player.getLogin()); player.toEntity(en); em.merge(en); } @Override public void updatePlayerCard(PlayerCardDTO playerCard) { PlayerCard en = em.find(PlayerCard.class, playerCard.getCardId()); playerCard.toEntity(en); em.merge(en); } @Override public void removePlayer(PlayerDTO player) { Player en = em.find(Player.class, player.getLogin()); player.toEntity(en); en = em.merge(en); em.remove(en); } @Override public boolean checkPlayer(String login) { Player help = em.find(Player.class, login); if (help != null) { return true; } return false; } @Override public boolean checkPlayer(String login, String password) { Player help = em.find(Player.class, login); if (help != null) { return false; } if (password.equals(help.getPassword())) { return true; } return false; } @Override public String getPassword(String login) { return em.find(Player.class, login).getPassword(); } @Override public void saveLocation(FavoriteLocationDTO loc) { FavoriteLocation en = em.find(FavoriteLocation.class, loc.getId()); if (en == null) { en = new FavoriteLocation(); en.setPlayer(em.find(Player.class, loc.getPlayer())); } en.setLat(loc.getLat()); en.setLng(loc.getLng()); en.setTitle(loc.getTitle()); en.setDescription(loc.getDescription()); em.merge(en); } @Override public List<FavoriteLocationDTO> getPlayerLocations(String login) { Player pl = em.find(Player.class, login); List<FavoriteLocation> list = em.createNamedQuery(FavoriteLocation.Q_GET_ALL_PLAYERS_LOCATIONS).setParameter("player", pl).getResultList(); ArrayList<FavoriteLocationDTO> listDTO = new ArrayList<FavoriteLocationDTO>(); Iterator it = list.iterator(); while (it.hasNext()) { listDTO.add(new FavoriteLocationDTO((FavoriteLocation) it.next())); } return listDTO; } @Override public void removeLocation(int id) { FavoriteLocation en = em.find(FavoriteLocation.class, id); em.remove(en); } } ```

Original source