How to consume a webservice from an EJB

18,934

From the official Java EE tutorial

Consuming a Web service

Share:
18,934
Daniel Schneller
Author by

Daniel Schneller

I am a software developer and operations guy. In an earlier life I programmed lots of Java for more than 10 years. Then I focused on mobile development on iOS and Android, but more recently I have moved on towards infrastructure and cloud topics. Currently most of my time is spent with AWS, Terraform, Packer, Consul, several databases, and Ansible, building and improving the infrastructure of CenterDevice, our cloud document management platform. Apart from this I spend my time with everything tech, mostly Linux and macOS, and other insanities ;) Disclaimer: Anything posted here is my personal and private view of things. I do not speak on behalf of my employer or their opinions.

Updated on October 30, 2022

Comments

  • Daniel Schneller
    Daniel Schneller over 1 year

    I am currently trying to wrap my mind around Java EE 5. What I'd like to do is create a sample application that

    • offers a simple stateless EJB (e. g. a simple calulator with an add() method)
    • expose this add method as a webservice
    • consume this webservice from another EJB

    The first two steps are easy and I can deploy and test this bean to Glassfish v2.1 already and test it with a standalone client:

    @WebService
    @Stateless
    public class CalculatorWS {
    
        @WebMethod
        public int add(@WebParam(name = "i") int i, @WebParam(name = "j") int j) {
            int k = i + j;
            return k;
        }
    }
    

    What I do not get however, is how to consume a webservice like this from a second EJB. While not strictly useful in this example, I will have to write some EJBs soon that will wrap external webservices as to keep my internal clients from having to deal with those external resources.

    From what I understand, I should be able to have the container inject me a webservice into a field of my EJB? I did not find an example for this, however. I'd welcome any hints to tutorials covering this - or even better an example right here :-)

    For what it's worth, I am using Eclipse 3.5.

  • eugenevd
    eugenevd about 11 years
    How is that consuming CalculatorWS as a webservice? It will be a plain EJB call will it not?