Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Right click on the package org.apache.geronimo.samples.jws and select
    New->Class.
  2. Name the class CalculatorService
  3. Accept all the defaults and Select Finish.





  4. Add the following code to CalculatorService.class
    Code Block
    titleCalculatorService.class
    borderStylesolid
    package org.apache.geronimo.samples.jws;
    
    import javax.annotation.Resource;
    import javax.jws.WebService;
    import javax.xml.ws.WebServiceContext;
    
    @WebService(serviceName = "Calculator",
                portName="CalculatorPort",
                endpointInterface = "org.apache.geronimo.samples.jws.Calculator",
                targetNamespace = "http://jws.samples.geronimo.apache.org",
                wsdlLocation = "WEB-INF/CalculatorService.wsdl")
    public class CalculatorService implements Calculator {
    
        @Resource
        private WebServiceContext context;
    
        public int add(int value1, int value2) {
            System.out.println("User Principal: " + context.getUserPrincipal());
            return value1 + value2;
        }
    }
    

This completes the development of Web Services implementation code.

Generating the wsdl for the web service

...

Code Block
titleCalculatorService.wsdl
borderStylesolid

<wsdl:definitions name="Calculator"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://jws.samples.geronimo.apache.org"
xmlns:tns="http://jws.samples.geronimo.apache.org">
<wsdl:types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://jws.samples.geronimo.apache.org"
targetNamespace="http://jws.samples.geronimo.apache.org"
attributeFormDefault="unqualified"
elementFormDefault="qualified">
<xsd:element name="add">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value1" type="xsd:int"/>
<xsd:element name="value2" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="addResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="return" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
<wsdl:message name="add">
<wsdl:part name="add" element="tns:add"/>
</wsdl:message>
<wsdl:message name="addResponse">
<wsdl:part name="addResponse" element="tns:addResponse"/>
</wsdl:message>
<wsdl:portType name="CalculatorPortType">
<wsdl:operation name="add">
<wsdl:input name="add" message="tns:add"/>
<wsdl:output name="addResponse" message="tns:addResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="CalculatorSoapBinding"
type="tns:CalculatorPortType">
<soap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="add">
<soap:operation soapAction="add" style="document"/>
<wsdl:input name="add">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="addResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="Calculator">
<wsdl:port name="CalculatorPort"
binding="tns:CalculatorSoapBinding">
<soap:address location="http://localhost:8080/jaxwscalculator-
1.0/calculator"/>
<wswa:UsingAddressing
xmlns:wswa="http://www.w3.org/2005/08/addressing/wsdl"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

...

Let us try to understand each annotation

    • @WebService- This annotation can be used with a Java class as well as with interface. In our case we used it with both interface as well as the POJO. This annotation declares the POJO as a WebService. @WebService annotation is utilized in generating the WSDL file.
      serviceName is same as the WSDL element service
      name is same as the WSDL element <portType name>
      endpointInterface suggests the user defined name for the Service Endpoint Interface(SEI).
      portName is the element portName
      *targetNamespace is the XML namespace of the WSDL and some of the XML elements generated from the WebService
    • @WebMethod- This annotation is applied to a method to expose it as a WebService method. In case you have multiple methods you can use this annotation to selectively expose methods as WebService method. If you donot use this annotation all the public methods will be exposed as WebService.
    • @WebParam- This annotation is used along with @WebMethod annotation to define the WebService. It is used to customize parameter used in the message part of the wsdl.

This completes the development of the Web Service Implementation code.

Generating the wsdl for the web service

Geronimo provides a new jaxws-tools.bat which helps in generating the WSDL file from service endpoint interface.

  • Export the source files to a jar as shown in the figure.
    !!
    !!
    !!
  • Open a command prompt and point it to the bin directory of server installation.
    1. Run gsh. This will start the Gshell.
    2. Run the command
      Code Block
      titleWSDL Generation command
      borderStylesolid
      
      Administrator@T60J9:/> jaxws/wsgen -classpath C:/WSDL/source.jar -d C:/WSDL/ -wsdl:soap1.1 org.apache.geronimo.samples.jws.CalculatorService
      
      In this command -classpath is used to set source.jar(exported from eclipse) in the classpath, -d defines the location where all the generated artifacts will be placed, -wsdl:soap1.1 suggests a wsdl generation following soap1.1 protocol, org.apache.geronimo.samples.jws.CalculatorService is the SEI used to generate the WSDL.
    3. Once the above command is run Calculator_schema1.xsd and Calculator.wsdl will be generated at C:/wsdl.
    4. Rename Calculator.wsdl as CalculatorService.wsdl and add both the generated files above to WEB-INF directory of the project.
      Info
      titleWhy are we using gsh if we already have a jaxws-tools.bat

      This is because there are some issues involved with jaxws-tools.bat in AG 2.1 so we are using a gshell to call the web services generator.

      To learn more about GShell refer Gshell section in user guide. To know about more options associated with jaxws. Run jaxws/wsgen -help or jaxws/wsimport -help from gshell.

Developing a web client for Calculator

...