简体   繁体   中英

How to connect EJB, REST and Client?

I have 2 java projects in netbeans and I want to connect them. First is based on Jboss server and contains ejb and rest. EJB connects with databse and rest service packs object to xml and sends to client witch is standard swing based gui application. Problem is that I don't know what to do next, because I got null pointer exception when I try to recive any data from server. Am I doing it right way? Maybe my whole idea is wrong? Please help.

EDIT: I figured that the fault is on server side. I don't know how to create a rest service. In class WholesaleREST in netbeans there were warning that rest is not configured. I clicked "Configure REST with Java EE6 Specification" and server can't deploy it and throws an error:

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

This error doesn't tell me anything about it and I dont know what to do. Any Ideas please?

End of added text, rest below is old.

Here is code I wrote:

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

}

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

Client side class with get method

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

}

And last class which access ejb methods from client side:

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

I think the architecture you want to achieve is something like this:

  1. common-model : here you place the domain model, for example the JPA entities and the class ClientsContainer
  2. restfull-service : depends on the common-model and contains the EJB/JPA layer that communicates with the database and the RESTful web service that exposes the data as resources.
  3. restful-client : the swing rich client that depends on the common-model and communicates with the restfull-service via HTTP.

Note that there is no direct communication between the EJB and the client.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM