如何连接EJB、REST和Client?

How to connect EJB, REST and Client?

我在 netbeans 中有 2 个 java 项目,我想连接它们。首先是基于 Jboss 服务器,包含 ejb 和 rest。 EJB 与数据库连接,其余服务将对象打包到 xml 并发送到客户端 是基于标准 swing 的 gui 应用程序。问题是我不知道下一步该怎么做,因为当我尝试从服务器接收任何数据时出现空指针异常。我这样做对吗?也许我的整个想法是错误的?请帮忙。

编辑: 我认为故障出在服务器端。我不知道如何创建休息服务。在 netbeans 的 class WholesaleREST 中,警告未配置其余部分。我单击 "Configure REST with Java EE6 Specification",但服务器无法部署它并抛出错误:

Deployment "vfs:///E:/Instalki/jboss/jboss-as-distribution-6.1.0.Final/jboss-6.1.0.Final/server/default/deploy/WholesaleApp.war" is in error due to the following reason(s): org.jboss.deployers.spi.DeploymentException: URL file:/E:/Instalki/jboss/jboss-as-distribution-6.1.0.Final/jboss-6.1.0.Final/server/default/tmp/vfs/automount6dbf7312f2f10b36/WholesaleApp.war-1ad4d6611c73bd02/ deployment failed

这个错误没有告诉我任何相关信息,我不知道该怎么办。有什么想法吗?

添加的文本结束,下面的内容是旧的。

这是我写的代码:

EJB class:

@Stateless
public class WholesaleEJB {

    @PersistenceContext(name="WholesaleAppPU")
    EntityManager entityManager;

    public List<Clients> getClients() {
        Query q = entityManager.createQuery("select c from Clients c");
        @SuppressWarnings("unchecked")
        List<Clients> lista = q.getResultList();
        return lista;
    }

}

休息class:

@Path("/wholesale")
@Stateless
public class WholesaleREST implements WholesaleInterface{

    @EJB
    WholesaleEJB bean;

    @Override
    @GET
    @Path("/get")
    public String getCars() {
        List<Clients> listOfClients = bean.getClients();
        StringWriter sw = new StringWriter();
        ClientsContainer container = new ClientsContainer(listOfClients);
        JAXB.marshal(container, sw);
        return sw.toString();
    }
}

客户端 class 使用 get 方法

public class HttpConnector {

    public static String doGet(String url) {
        try {
            URLConnection connection = new URL(url).openConnection();
            String charset = "UTF-8";
            connection.setRequestProperty("Accept-Charset", charset);
            return getResponse(connection);
        } catch (Exception ex) {
            ex.getMessage();
        }
        return null;
    }

    private static String getResponse(URLConnection connection) 
           throws UnsupportedEncodingException, IOException {
        InputStream response = connection.getInputStream();
        final char[] buffer = new char[0x10000];
        StringBuilder out = new StringBuilder();
        Reader in = new InputStreamReader(response, "UTF-8");
        int read;
        do {
            read = in.read(buffer, 0, buffer.length);
            if (read>0) {
                out.append(buffer, 0, read);
            }
        } while (read>=0);

        return out.toString();
    }

}

最后一个 class 从客户端访问 ejb 方法:

public class ClientRemoteAccess implements ClientInterface{

String url = "http://localhost:8080/WholesaleApp/wholesale";

    @Override
    public List<Clients> getClients() {
        String recivedXML = HttpConnector.doGet(url+"/get");
        ClientsContainer container = JAXB.unmarshal(
             new StringReader(recivedXML), ClientsContainer.class);
        return container.getListOfClients();
    }  
}

我认为你想要实现的架构是这样的:

  1. common-model:在这里放置域模型,例如 JPA 实体和 class ClientsContainer
  2. restfull-service:依赖于 common-model 并包含 EJB/JPA 层与数据库和将数据公开为资源的 RESTful Web 服务进行通信。
  3. restful-client:依赖common-model并与[=通信的swing富客户端14=]restfull-service 通过 HTTP。

请注意,EJB 与客户端之间没有直接通信。