I have a requirement to use EJB 3.1 where only some of its methods will be exposed as webservices and will be consumed by our external client applications where as other components of our application(Servlet etc) should have access to all of its method. At this point,Im not even sure if it is possible with single EJB to achieve both.
E.g. If EJB has 3 method,the external client will be exposed to 1 or 2 method where as the other component of the application should have access to all of the EJB methods.
On the Java class that implements your Web service, specify the @WebMethod annotation on each method that you want to customize for the service. You can use this annotation to exclude a method from your service. By default, all public methods are exposed in a service, including inherited methods that are below the Object class.
Oracle Certified Professional: Java SE 6 Programmer
Oracle Certified Expert: Java EE 6 Web Component Developer
Oracle Certified Expert: Java EE 6 Enterprise JavaBeans Developer
Joined: Sep 19, 2008
I think I did not post my question properly.
We are using EJB 3.1 which has been exposed as Webserice and this webservice will be consumed by the application outside of our company.
The same EJB can be used by our other part of our applications like another EJB//Servlet running under the same JVM but in different ear or war file.
My question here is can I inject this EJB into another EJB/Servlet or should I call them using Webserice?
Hope my question is clear to you.
Joined: Aug 06, 2010
Sure you can. You expose your EJB using a Web Service endpoint interface and a Remote business interface. You specify the methods you need for each view in the interface (as long as it's not Stateful, of course). You can even use the same interface for the Remote client view and the Web Service endpoint, though that's not necessarily a good idea.
Be advised though, that your parameters and return value have to fulfill all the requirements for both SOAP and EJB.
Then you declare the methods you want to expose in their relevant interfaces. If you want to expose a WebService superset in the remote EJB, you can have the Remote interface extend the Endpoint interface and declare the additional methods... For configuration of the two, check the spec. But they are easy to combine since EJB 3.
You can easily inject the remote view into a Servlet as @EJB, and use the WebService at the same time. It should be possible to reuse the Remote interface as a web service endpoint interface and use @WebMethod annotations to expose the methods you need in the web service as opposed to the remote ejb, but I've never done it as it would look kind of dirty, and it may cause problems later on when changes are required. Better to use two different interfaces.
I hope that answers your question. Otherwise I misunderstood again. :/
Joined: Sep 19, 2008
Thanks Dieter..Exactly what I was looking for.
Tested the same with sample application.
Appreciate your help.
subject: EJB with Webservice and Remote client view