Numerous platforms are available on the market for developing web services based on the Java platform. However, most of these platforms conform to the JAX-WS specification (JSR-000224).
Start developing a simple web service
Software requirements:
- Java SE 1.6 or higher;
- JAX-WS Reference 2.2.5 implementation (available here);
- Apache Tomcat version 6 or higher.
The web service can be developed in two ways
Top-down approach
In this approach, the service interface is created first, and the implementation is provided later.
Bottom-up approach
In this approach, the service implementation is created first and the interface is defined on that basis. This approach is easy for beginners unfamiliar with web services.
We follow the second, bottom-up approach, for reasons of simplicity and to have a faster, ready-to-use working example.
What you need to do.
Create a POJO class (an old Java object) and annotate it with WebService as follows
package com.accrd.blog.blog.ws.jaxws.sample;
import javax.jws.WebService;
@WebService(name="SimpleWebService")
public class SimpleWebService {
public String sayHello (String name){
return "Hello, "+ name + "!" ;
}
Now the web service has a task, and the available operation does this: you pass a name (“web service”), and it returns a hello message with the specified name (“Hello, web service!”).
That’s it. The web service is ready to be deployed.
You can deploy it in two ways,
- standalone deployment. In this approach, you just need to have a main method and call Endpoint.publish (url, provider) . This is mentioned below. This creates the web service runtime environment that comes with Java SE 6 and deploys it to a lightweight http server. The service is accessible from the URL specified as input to the publish method.
- Deployment in a servlet container. In this approach, we can deploy a web service by creating a standard .war file of a java web application and deploy it to a web server that has servlet container support, such as Tomcat or Jetty.