This topic describes three different starting points for developing web services:
In this approach to developing a web service, you begin by defining the WSDL file (or getting a pre-existing one). This is the web service contract that defines how the web service communicates with clients, including the data types conveyed, the available methods, and the protocols and message formats used. Hence, this approach to web service development is sometimes called "contract first" or "top down" development. Note that you can only use JAX-RPC types when using this development approach, XMLBean types are not available.
To generate a web service from a WSDL:
Two artifacts will be created: a web service implementation class and a JAR file. The web service class will contain the web methods described by the WSDL (the publicly accessible methods and callbacks) without any method bodies. The developer must fill in the web service's implementation details. The JAR file contains a web service interface class and types referenced in the original WSDL and is located in the project's WEB-INF/lib directory.
For example the generated web service implementation class will resemble the following:
@WebService( serviceName="MailingListServiceService", targetNamespace="http://services", endpointInterface="model.MailingListService") @WLHttpTransport(contextPath="ServicesWeb",serviceUri="MailingListService",portName="MailingListServiceSoapPort") public class MailingListServiceImpl implements MailingListService { public MailingListServiceImpl() { } public java.lang.String getCustomers() { //replace with your impl here return null; }
Notice that this class implements MailingListService, the interface file found in the generated JAR file.
The developer must fill in the method body for the method getCustomers().
In this approach to developing a web service, you begin with an XML schema (XSD file) that defines XML data structures to be used as parameters and return types in the web service operations. XMLBean Java types are then automatically generated from the schema for use in your web service. This approach gives you the following benefit of a common set of data structures for all of the web services in a project or set of projects.
Once the XMLBean types are available, web service development proceeds according to the "Start with a Java Class" method described below.
To develop starting from an XML schema:
Schemas will be automatically compiled into XMLBeans, which you can use in your web service. The XMLBean types will be automatically re-compiled whenever the schemas in the schema directory are updated.
Note: You do not have to use the XML beans builder facet to create XMLBean types. Alternatively, you can generate a JAR by right-clicking any XSD or WSDL in the project and selecting Web Services > Generate Types JAR File. This will open the Types JAR File Generation Wizard, from which you can generate a JAR containing XMLBeans. Or, if you already have a JAR containing the XMLBean types, you can import it into the project and use those types in a web service. Neither of these options provide automatic updating of the XMLBean JAR when the original schema changes.
Available XMLBean types can be seen in the Navigator view (Window > Show View > Navigator) in the directory .xbean_src. The .xbean_src and .xbean_bin directories contain generated files that should never be directly edited.
The following example shows one way to incorporate XMLBean types into a web service. Suppose you import the following schema:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ws="http://openuri.org/bea/samples/workshop" targetNamespace="http://openuri.org/bea/samples/workshop" elementFormDefault="qualified"> <xs:element name="applicant"> <xs:complexType mixed="true"> <xs:choice minOccurs="0" maxOccurs="1"> <xs:element name="bankrupt" type="xs:boolean"/> <xs:element name="name_first" type="xs:string"/> <xs:element name="name_last" type="xs:string"/> <xs:element name="risk_estimate" type="xs:string"/> <xs:element name="score_info" type="ws:score_infoType"/>
</xs:choice> </xs:complexType> </xs:element> <xs:complexType name="score_infoType" mixed="true"> <xs:choice minOccurs="0" maxOccurs="1"> <xs:element name="credit_score" type="xs:short"/> </xs:choice> </xs:complexType> </xs:schema>
The corresponding generated XMLBean types are ApplicantDocument, ScoreInfoType, etc.
The following web service method uses the ApplicantDocument as an input parameter and performs a simple risk assessment calculation.
@WebMethod public String getRiskEstimate(ApplicantDocument appDoc) { boolean bankrupt = appDoc.getApplicant().getBankrupt(); short balanceRemain = appDoc.getApplicant().getBalanceRemaining(); if (bankrupt == true && balanceRemain < 200) appDoc.getApplicant().setRiskEstimate("high"); else appDoc.getApplicant().setRiskEstimate("low"); return appDoc.getApplicant().getRiskEstimate(); }
Note that use of Xbeans as a parameter or return type with doc/lit/bare bindings is not supported in operations or callbacks and will result in a failure during deployment. Use doc/lit/wrapped for services that use XBeans as parameters or return types.
For detailed information about using XMLBeans see Using XMLBeans in the IDE and the XMLBeans tutorial.
In this approach, you develop a web service as a Java class. Methods become web service operations and method parameters and return types can be simple Java Beans. The Java class is annotated to indicate what methods should be exposed and to set other properties for the service. The following guidelines will help you utilize all of Workshop's web service features.