4

当我尝试运行 JAX-RS 时,我收到错误消息,我怎样才能让它工作?PlayerManagementLocal 是 ejb PlayerManagement 的 @Local 工作正常。

错误信息

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";
}
}

这是代码,无法弄清楚如何让它工作 - 例如....在数据库中找到一些东西并将其与 POST/GET 服务中的字符串进行比较。

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);

}
}
4

1 回答 1

5

从 GlassFish 2.1 迁移到 GlassFish3.1 时,我也遇到了类似的问题。所以根据以下链接:

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

第 1 步:创建本地或远程接口:

@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:现在在You无状态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) 
   {
       .......
   }

} 
于 2012-06-20T06:58:27.817 回答