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.
Do you need a webserver? If yes, what webserver should I use? Sorry to bother you (I have a PHP server that needs to call Java).
Regards
Bio
Finally! A Simple web Service tutorial
Great post Nikos
Really appreciate the simplicity in the explanation you provided.
Thanks!
Thanks a lot… I was serching similar code since long time. Finally i found it here. It gave an excellent start in webservice. Thanks again.
Thanks for the tutorial. That’s exactly what I was looking for.
But I have a little problem: since the only requirement for this web service is Java 6, I assume that the webserver is Sun Java System Web Server. But how do I demonstrate this? I created the project in Eclipse and I don’t have any bulid.properties file where I think it should be set the VS.DIR property referencing the webserver location. Thanks again.
Thanks Nikos. What a great post it is? This is the best compact tutorial I have ever seen in my life.You are great Dear.
Great post! Know I just need to figure out how to publish something like this to my server!
Thanks, a very simple and clear example…
Thanks. Very nice.
I get this error: Exception in thread “main” com.sun.xml.internal.ws.wsdl.parser.InaccessibleWSDLException: 2 counts of InaccessibleWSDLException.
java.net.ConnectException: Connection refused: connect
java.net.ConnectException: Connection refused: connect
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.tryWithMex(Unknown Source)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(Unknown Source)
at com.sun.xml.internal.ws.client.WSServiceDelegate.parseWSDL(Unknown Source)
at com.sun.xml.internal.ws.client.WSServiceDelegate.(Unknown Source)
at com.sun.xml.internal.ws.client.WSServiceDelegate.(Unknown Source)
at com.sun.xml.internal.ws.spi.ProviderImpl.createServiceDelegate(Unknown Source)
at javax.xml.ws.Service.(Unknown Source)
at javax.xml.ws.Service.create(Unknown Source)
at com.simple.webservice.SimpleServiceClient.main(SimpleServiceClient.java:18)
Thank you , This is very helpful . A full working example of creation of a service in a single page.
Other web pages are crap . i need to browse 5 pages for this.
Hi,
Really nice post. If anybody has a problem related to not generating the WSDL file in above example then there is a simple step you have left. Just execute the ‘SimpleServicePublisher.java’ before trying to generate the WSDL. It will actually define the endpoint where you can be searched while running the client.