如何连接EJB,REST和客户端?

时间:2015-09-16 16:56:19

标签: java rest java-ee jboss

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

编辑: 我认为故障发生在服务器端。我不知道如何创建休息服务。在netbeans类WholesaleREST中,有警告说没有配置休息。我点击了#34;使用Java EE6规范配置REST"并且服务器无法部署它并引发错误:

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类:

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

}

休息班:

@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();
    }
}

带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();
    }

}

最后一个从客户端访问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 个答案:

答案 0 :(得分:1)

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

  1. 通用模型:此处放置域模型,例如JPA实体和类 ClientsContainer
  2. restfull-service :取决于通用模型,并包含与数据库通信的EJB / JPA层以及将数据公开为资源的RESTful Web服务
  3. restful-client :依赖于通用模型的swing富客户端,并通过HTTP与 restfull-service 进行通信。< / LI>

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