The following approaches can be used to create a Web service as a Web application:
Expose a Java class as a Web service
This is the simplest way to create a Web service. This method is easy if you simply want to create a Web service and there are no additional constraints.
Creating a Web service from WSDL
Select this method if, for example, the interface of the Web service must be strictly defined and WSDL must be used for the interface design.
In order to expose a Java class as a Web service, the Java class must first be created. Naturally, an existing Java class can also be made into a Web service.
The Web service can then be created by declaring the @WebService annotation in the Java class and implementing the service you want to publish.
Creating the Java Class
To create the Java class, select [Class] from the [New] wizard. Creation of a special Java class is not required. Specify the source folder, package, and name to create the class.
Declaring the WebService Annotation
Code the @WebService annotation in the created Java class as shown below.
Example of using the WebService Annotation
package sample; import javax.jws.WebService; @WebService public class Calc { public int add(int param1,int param2) { return param1 + param2; } } |
Implementing the Service
Define the method of the function you want to publish as a Web service, and implement the method.
In order to create a Web service from WSDL, the WSDL must first be created. Naturally, existing WSDL can also be used to create a Web service.
Then, the Web server can be created by creating a Web server that conforms to the WSDL and implementing the declared method.
Creating the WSDL
Select [Web Services] > [WSDL] from the [New] wizard and the WSDL can be created by the wizard. In addition, a special-purpose editor can be used to edit the WSDL.
For details on how to create and edit WSDL, refer to "5.3.4 Creating WSDL".
Creating a Web Service that conforms to the WSDL
In order to create a Web service that conforms to the WSDL, a Service Endpoint Interface must first be created from the WSDL. The Service Endpoint Interface can be created by selecting [Web Services] > [Web Service(JAX-WS)] from the [New] wizard.
For details on creating the interface using the wizard, refer to "5.3.5 Creating a Service Endpoint Interface from WSDL".
If the Service Endpoint Interface has been created, use the Java class wizard to create the class that implements the Service Endpoint Interface.
Select [Class] from the [New] wizard. To create the class that implements the Service Endpoint Interface, specify the following to generate it.
Package and name
Specify the package and the class name of the Web service to be created.
Interfaces
Specify the Service Endpoint Interface.
Code the @WebService annotation in the created Java class as shown below.
Example of using the WebService Annotation
package sample; import javax.jws.WebService; @WebService(endpointInterface="sample.Calc", wsdlLocation="CalcService.wsdl") public class CalcImpl implements Calc { public int add(int param1,int param2) { return param1 + param2; } } |
Implementing the Method (Service)
Implement the declared method.