We are about to create a simple Web Service in Java. The only requirement is Java SE 6.
At first, we clearly define the operation to be exposed.
public interface SimpleService {
String sayHello();
}
Let’s declare it as a web service.
@WebService @SOAPBinding(style=Style.RPC) public interface SimpleService { @WebMethod String sayHello(); }
API: WebService, WebMethod, SOAPBinding, SOAPBinding.Style
So we have a web service with a single operation: sayHello. Moreover we choose the simplified RPC style. Let’s implement the operation.
public class SimpleServiceImpl implements SimpleService {
public String sayHello() {
return "Hello from Web Services!";
}
}
We should provide the complete name of the service interface,
@WebService(endpointInterface="simple.SimpleService")
public class SimpleServiceImpl implements SimpleService {
public String sayHello() {
return "Hello from Web Services!";
}
}
where simple is the package of SimpleService.
At this point, it is easy to publish the web service locally.
public class SimpleServicePublisher {
/**
* Publishes a Web Service locally.
*/
public static void main(String[] args) {
Endpoint.publish("http://127.0.0.1:9000/simple", new SimpleServiceImpl());
}
}
API: Endpoint
Click on this link to test it: http://127.0.0.1:9000/simple?wsdl.
You will see an XML document in your browser. It is the notorious WSDL, the contract that describes in detail the available operations of our Web Service. Since it is language-neutral, we may built a client in any programming language.
In order to make a Java client, we first need to specify the URL.
URL url = new URL("http://127.0.0.1:9000/simple");
Then we need to specify the qualified namespace that consists of two things:
- The URI is the value of the @namespace attribute in WSDL.
- The local part is the value of the service[@name] attribute at the bottom of WSDL.
QName name = new QName("http://simple/", "SimpleServiceImplService");
Now we can create the service,
Service service = Service.create(url, name);
get our port,
SimpleService simple = service.getPort(SimpleService.class);
and interact with our Web Service. The complete code of the client follows.
public class SimpleServiceClient {
/**
* Connects to a Web Service.
*/
public static void main(String[] args) throws Exception {
// Specify the URL
final URL url = new URL("http://127.0.0.1:9000/simple");
// Specify the qualified name
final QName name = new QName("http://simple/", "SimpleServiceImplService");
// Create the service
final Service service = Service.create(url, name);
// Get the port
final SimpleService simple = service.getPort(SimpleService.class);
// Call an operation
System.out.println(simple.sayHello());
}
}
Summary
The following classes are involved.
- SimpleService and SimpleServiceImpl define the functionality that will be exposed as a Web Service.
- SimpleServicePublisher publishes the Web Service locally.
- SimpleServiceClient is a client of the Web Service.
Thanks.
hmmmmmmm amazing i would try this stuff this is realy quick way to design web service… i using XFire JAX-WS and also Spring webservice
but with annotation u realy make it simple both service and its client…
realy thanx keep it up..
Thanks Nikos for this excellent yet clarifying example and sharing knowledge. Great blog
[...] the previous post we created a simple Web Service in Java. Now we’ll build on it, by providing more [...]
Thx a lot, EXACTLY what i was looking for
Was a step missed? The code given is clear enough. However, it’s not obvious if something is deployed or if a server is involved. Is something packaged?
Doh…I see these are run as java apps.
thanks for this post. It was an excellent example. I’m starting to read the other posts now.
I am getting the following error when i try to invoke the service
Exception in thread “main” javax.xml.ws.WebServiceException: {http://simple/}SimpleServiceImplService is not a valid service. Valid services are: {http://webservice/}SimpleServiceImplService
at com.sun.xml.internal.ws.client.WSServiceDelegate.(WSServiceDelegate.java:220)
at com.sun.xml.internal.ws.client.WSServiceDelegate.(WSServiceDelegate.java:165)
at com.sun.xml.internal.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:93)
at javax.xml.ws.Service.(Service.java:56)
at javax.xml.ws.Service.create(Service.java:680)
at webserviceclient.SimpleServiceClient.main(SimpleServiceClient.java:19)
I found the issue it was mybad, i gave a different namespace when i created the WS and was using the name from your sample client,
Thank you for the simplest explanation of the WS creation steps.
Hello,
Could you post a similar post for other type of web services please.
Thank you.