Use XML to define web services through Java classes

Source: Internet
Author: User
Tags wsdl

Java class

Java classes are provided to form a simple Web Service Java project. You can also use the Java class you created. The project used in this article contains three types:OneWayRequestClass,TwoWayRequestClass andTwoWayResponseClass. Look at each class in order. Let's start fromOneWayRequestClass (see Listing 1 ).

Listing 1. onewayrequest Java class

                package com.ibm.devWorks.xml.simpleService;public class OneWayRequest {  protected String requestData;  public String getRequestData() {    return this.requestData;  }  public void setRequestData(String requestData) {    this.requestData = requestData;  }}

The code above contains only one field, that isrequestDataIt contains the requested data. This class has two methods: one for setting the field value and the other for retrieving the field value.

Next, let's take a look atTwoWayRequestClass.

Listing 2. twowayrequest Java class

                package com.ibm.devWorks.xml.simpleService;public class TwoWayRequest {  protected String echoString;  protected boolean booolean;  public boolean getBooolean() {    return this.booolean;  }  public void setBooolean(boolean booolean) {    this.booolean = booolean;  }  public String getEchoString() {    return this.echoString;  }  public void setEchoString(String echoString) {    this.echoString = echoString;  }}

This class is a little longer. It defines two fields: one isechoStringAnd the other isbooolean. Same as the first class, this class definesgetterAnd onesetterMethod.

Listing 3 shows the last class, that isTwoWayResponseClass.

Listing 3. twowayresponse Java class

                package com.ibm.devWorks.xml.simpleService;public class TwoWayResponse {  protected String echoString;  protected boolean invertedBoolean;  public boolean getInvertedBoolean() {    return this.invertedBoolean;  }  public void setInvertedBoolean(boolean invertedBoolean) {    this.invertedBoolean = invertedBoolean;  }  public String getEchoString() {    return this.echoString;  }  public void setEchoString(String echoString) {    this.echoString = echoString;  }}

This class corresponds to the previousTwoWayRequestClass is quite similar, but this is not necessary. The response class may contain some data items in a database record. In this way, the Java class is complete! Next, we will introduce the WSDL used to expose these classes as Web Services.

Expose Java classes as services through WSDL.

In part 1 of this system article, we will use axis2 to read the WSDL created in this section to create a JAVA Implementation of Web Services.

Before learning deeply, we need to know some knowledge about Web Services.OneWayRequestClass indicates a one-way Web service operation, which has only one request, used to send request data in the fieldrequestData.TwoWayRequestClass andTwoWayResponseClass indicates a two-way Web service operation, which has a request and a response in the responseechoStringField and requestechoStringFields have the same value. In the responseinvertedBooleanField Value and in the requestinvertedBooleanThe field value is the opposite (ifinvertedBooleanIf the field value is falseinvertedBooleanThe field value is true ).

Return to the WSDL. First, create a namespace, as shown in Listing 4.

Listing 4. Define the namespace of the WSDL.

                <?xml version="1.0" encoding="UTF-8"?><wsdl:definitions   xmlns:apachesoap="http://xml.apache.org/xml-soap"   xmlns:impl="http://ibm.com/developerWorks/xml/SimpleService"   xmlns:intf="http://ibm.com/developerWorks/xml/SimpleService"   xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"   xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"   xmlns:xsd="http://www.w3.org/2001/XMLSchema"   targetNamespace="http://ibm.com/developerWorks/xml/SimpleService">...

Basically, the WSDL is ready. We will reference the namespace in the WSDL later. When axis2 reads the WSDL file, it needs to know the value of the referenced namespace.

Next, define the mode, as shown in listing 5.

Listing 5. Define the Mode

                   targetNamespace="http://ibm.com/developerWorks/xml/SimpleService">  <wsdl:types>    <schema       elementFormDefault="qualified"       targetNamespace="http://ibm.com/developerWorks/xml/SimpleService"       xmlns="http://www.w3.org/2001/XMLSchema">            <!-- ELEMENTS -->            <element name="OneWayRequest">        <complexType>          <sequence>            <element name="requestData" type="xsd:string"/>          </sequence>        </complexType>      </element>            <element name="TwoWayRequest">        <complexType>          <sequence>            <element name="echoString" type="xsd:string"/>            <element name="booolean" type="xsd:boolean"/>          </sequence>        </complexType>      </element>      <element name="TwoWayResponse">        <complexType>          <sequence>            <element name="echoString" type="xsd:string"/>            <element name="invertedBoolean" type="xsd:boolean"/>          </sequence>        </complexType>      </element>    </schema>  </wsdl:types>...

You should be familiar with this code. This is essentially a Java class defined by the schema. Note that fields in the Java class match the sub-elements of the preceding pattern element. Note that the namespace ishttp://ibm.com/developerWorks/xml/SimpleServiceAfter you create a jibx definition description, you will find its importance.

Then define the message, port type, binding, and WSDL service (as shown in Listing 6 ).

Listing 6. Complete the definition of WSDL

                ...  </wsdl:types>  <!-- MESSAGES -->  <wsdl:message name="OneWayRequestMessage">    <wsdl:part name="input" element="impl:OneWayRequest"/>  </wsdl:message>  <wsdl:message name="TwoWayRequestMessage">    <wsdl:part name="input" element="impl:TwoWayRequest"/>  </wsdl:message>  <wsdl:message name="TwoWayResponseMessage">    <wsdl:part name="output" element="impl:TwoWayResponse"/>  </wsdl:message>  <!-- Port type (operations) -->  <wsdl:portType name="SimpleServicePortType">    <wsdl:operation name="OneWay" parameterOrder="input">      <wsdl:input name="OneWayRequestMessage"                  message="impl:OneWayRequestMessage"/>    </wsdl:operation>    <wsdl:operation name="TwoWay" parameterOrder="input">      <wsdl:input name="TwoWayRequestMessage"                  message="impl:TwoWayRequestMessage"/>      <wsdl:output name="TwoWayResponseMessage"                   message="impl:TwoWayResponseMessage"/>    </wsdl:operation>  </wsdl:portType>  <!-- BINDING (bind operations) -->  <wsdl:binding     name="SimpleServiceSoapBinding"     type="impl:SimpleServicePortType">    <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>    <wsdl:operation name="OneWay">      <wsdlsoap:operation soapAction="OneWay"/>      <wsdl:input>        <wsdlsoap:body use="literal"/>      </wsdl:input>    </wsdl:operation>    <wsdl:operation name="TwoWay">      <wsdlsoap:operation soapAction="TwoWay"/>      <wsdl:input>        <wsdlsoap:body use="literal"/>      </wsdl:input>      <wsdl:output>        <wsdlsoap:body use="literal"/>      </wsdl:output>    </wsdl:operation>  </wsdl:binding>  <!-- SERVICE -->  <wsdl:service name="SimpleService">    <wsdl:port binding="impl:SimpleServiceSoapBinding"               name="SimpleService">      <wsdlsoap:address location="http://localhost:8080/axis2/services/SimpleService"/>    </wsdl:port>  </wsdl:service></wsdl:definitions>

The defined message is mapped to the request and response elements in the mode, which are defined by the port type. The input and output of each operation are mapped to one message. In essence, the binding part maps a soap operation and defines the protocol, transmission method, and related operations of the binding service. Finally, the available services are specified in the Service Section, which displays the service endpoints of each Service (URLs that can access the service ).

This completes the definition of the web service. Next, we will map the XML to the Java class to use jibx for data binding in the next part of this series.

Create a jibx definition description

The main purpose of the jibx definition description is to map Java classes to namespaces, including any expected fields. After reading the files created in this section, you will learn more. First, take a lookOneWayRequestHow classes are mapped (as shown in listing 7 ).

Listing 7. ing onewayrequest Java class

                <?xml version="1.0" encoding="UTF-8"?><binding>  <mapping name="OneWayRequest"           class="com.ibm.devWorks.xml.simpleService.OneWayRequest">    <namespace uri="http://ibm.com/developerWorks/xml/SimpleService"               default="elements"/>    <value name="requestData" field="requestData" usage="required"/>  </mapping>...

In the mapping labelnameAttribute specifiedOneWayRequestName of the directly associated element (in the WSDL mode created in listing 5.classProperty maps elements to Java classes, as shown in Listing 1. In the namespace labeluriThe namespace for property ing ishttp://ibm.com/developerWorks/xml/SimpleService, And in listing 5OneWayRequestThe element namespace is the same. Finally, you can seerequestDataHow to map child elements (in the WSDL in listing 5) to Java classrequestDataField (in the value tag in Listing 1.nameThe attribute specifies the child element in the WSDL, and the field attribute specifies the field name in the Java class.

Next, we will introduce how the other two Java classes map, as shown in listing 8.

Listing 8. ing twowayrequest and twowayresponse Java classes

                ...  <mapping name="TwoWayRequest"            class="com.ibm.devWorks.xml.simpleService.TwoWayRequest">    <namespace uri="http://ibm.com/developerWorks/xml/SimpleService"                default="elements"/>    <value name="echoString" field="echoString" usage="required"/>    <value name="booolean" field="booolean" usage="required"/>  </mapping>  <mapping name="TwoWayResponse"            class="com.ibm.devWorks.xml.simpleService.TwoWayResponse">    <namespace uri="http://ibm.com/developerWorks/xml/SimpleService"                default="elements"/>    <value name="echoString" field="echoString" usage="required"/>    <value name="invertedBoolean" field="invertedBoolean"            usage="required"/>  </mapping></binding>

In listing 8, we can see thatOneWayRequestClass,TwoWayRequestClass andTwoWayResponseClass ing similarity. However, the name, class, and field here pointTwoWayRequestAndTwoWayResponseThe appropriate data of the element. However, one of the differences is that these two elements contain two child elements or fields. If the mapping tag does not have child elements, no value tag exists. If the mapping tag has multiple child elements, the value tag is listed one after another, as shown in listing 8.

Conclusion

Great! We have already completed half of the work. We use existing Java classes to define an XML schema for them and publish them through the operations in the WSDL and Web services. We created a jibx definition description, which maps the elements in the Java class and the schema, so that axis2 jibx Data Binding can correctly bind the Web Service soap data with the Java class.

In section 2nd, axis2 uses the jibx definition description to generate a prototype of the Service trunk and client. We will deploy the final Apache axis2 Web Service on Apache Geronimo, and then generate and create a client that communicates with the deployed Web service to complete the test.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.