This discussion is archived
1 Reply Latest reply: May 10, 2012 6:16 AM by gimbal2 RSS

Best way of implementing WebService in existing EJB application

916153 Newbie
Currently Being Moderated
I created EJB server application which communicates well with clients(Java desktop apps). Remote clients just use JNDI lookup to get access to EJBeans deployed on Glassfish server.
After this step I want to extend server application to be more flexible for working with non-Java based clients. I mean browser applications based on Javascript.
The best way is to implement WebService on Glassfish that will call my EJB beans. What I have tried:

1. Created separate "Dynamic Web Project" application in Eclipse calling via JNDI enterpsise beans from the first app. Although WebService was properly deployed it cannot lookup beans from other
application deployed on Glassfish. It was strange because I used portable JNDI names suggested by Glassfish server. Always got exception "ClassNotFound..." and my lookups from remote clients
worked. After many tries I gave up. (Now I am not sure if calling EJB from another EJB app by WebService app deployed on sam Glassfish server is possible)

2. Thanks to this forum (user "gimbal") I found materials how to implement WebService on existing EJB using JAX-WS (@WebService,@WebMethod annotaions). So I didn't have to create
separate WebService application. I just adjusted one of my EJB with mentioned annotaions. This worked fine but I have problems to force JAX-WS to return data in JSON format not in XML(default).
I needed JSON.

3. I read more abut implementing WebServices in Java and found (I think the most fresh) approach using JAX-RS. JAX-RS uses annotations like: @Path,@GET,@POST,@Produces...etc
which can turn any class into WebService. The problem is that I cannot access EJB exposed as WebService using JAX-RS because I have no URL path configured.
I have no web.xml file in my Eclipse project where I can configure URL to my EJB. This is because in Eclipse web.xml configuration file is not created for EJB module project - only from Dynamic Web Projects.
I don't know what to do next...

Generally I want to know which of my 1,2,3 approaches is best. In which direction (regarding WebService) should I develop existing EJB module under Eclipse considering my goals below:
1. Solution must use already implemented EJBs in existing application
2. Solution must provide data to clients in JSON format
  • 1. Re: Best way of implementing WebService in existing EJB application
    gimbal2 Guru
    Currently Being Moderated
    Slawomir wrote:
    1. Created separate "Dynamic Web Project" application in Eclipse calling via JNDI enterpsise beans from the first app. Although WebService was properly deployed it cannot lookup beans from other
    application deployed on Glassfish. It was strange because I used portable JNDI names suggested by Glassfish server. Always got exception "ClassNotFound..." and my lookups from remote clients
    worked. After many tries I gave up. (Now I am not sure if calling EJB from another EJB app by WebService app deployed on sam Glassfish server is possible)
    Problem better suited for the Glassfish forum: http://www.java.net/forums/glassfish/glassfish
    >
    2. Thanks to this forum (user "gimbal") I found materials how to implement WebService on existing EJB using JAX-WS (@WebService,@WebMethod annotaions). So I didn't have to create
    separate WebService application. I just adjusted one of my EJB with mentioned annotaions. This worked fine but I have problems to force JAX-WS to return data in JSON format not in XML(default).
    I needed JSON.
    Yeah. The joke is that you're more in the territory of JAX-RS (RESTful webservices) in stead of JAX-WS then. JAX-WS is built around SOAP which is XML based. Of course you can return JSON data as a single field and then parse that upon retrieval, but that is just silly engineering.
    3. I read more abut implementing WebServices in Java and found (I think the most fresh) approach using JAX-RS. JAX-RS uses annotations like: @Path,@GET,@POST,@Produces...etc
    which can turn any class into WebService. The problem is that I cannot access EJB exposed as WebService using JAX-RS because I have no URL path configured.
    I have no web.xml file in my Eclipse project where I can configure URL to my EJB. This is because in Eclipse web.xml configuration file is not created for EJB module project - only from Dynamic Web Projects.
    I don't know what to do next...
    Make everything part of an EAR; every module in the EAR will or should have access to each others resources through their local interface. On top of that, since JEE6 it is possible to deploy EJBs as part of a WAR; perhaps you don't even need a separate EJB module.

    I'm not a Glassfish user myself, I prefer JBoss. I wrote this JBoss 7 startup guide which may interest you:

    http://entjavastuff.blogspot.com/2011/12/jboss-7-getting-started.html

    In there I detail the quest of setting up a WAR project in Eclipse and getting that to deploy and run on a JBoss 7 server, including having EJBs in the war itself. Perhaps seeing the steps I take some things will click in your own Glassfish adventures :)

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points