JAX-WS Hello World Example – RPC Style
JAX-WS is bundled with JDK 1.6, which makes Java web service development easier to develop. This tutorial shows you how to do the following tasks:
- Create a SOAP-based RPC style web service endpoint by using JAX-WS.
- Create a Java web service client manually.
- Create a Java web service client via wsimport tool.
- Create a Ruby web service client.
You will be surprise of how simple it is to develop a RPC style web service in JAX-WS.
In general words, “web service endpoint” is a service which published outside for user to access; where “web service client” is the party who access the published service.
JAX-WS Web Service End Point
The following steps showing how to use JAX-WS to create a RPC style web service endpoint.
1. Create a Web Service Endpoint Interface
File : HelloWorld.java
package com.mkyong.ws; import javax.jws.WebMethod; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; //Service Endpoint Interface @WebService @SOAPBinding(style = Style.RPC) public interface HelloWorld{ @WebMethod String getHelloWorldAsString(String name); }
2. Create a Web Service Endpoint Implementation
File : HelloWorldImpl.java
package com.mkyong.ws; import javax.jws.WebService; //Service Implementation @WebService(endpointInterface = "com.mkyong.ws.HelloWorld") public class HelloWorldImpl implements HelloWorld{ @Override public String getHelloWorldAsString(String name) { return "Hello World JAX-WS " + name; } }
3. Create a Endpoint Publisher
File : HelloWorldPublisher.java
package com.mkyong.endpoint; import javax.xml.ws.Endpoint; import com.mkyong.ws.HelloWorldImpl; //Endpoint publisher public class HelloWorldPublisher{ public static void main(String[] args) { Endpoint.publish("http://localhost:9999/ws/hello", new HelloWorldImpl()); } }
Run the endpoint publisher, and your “hello world web service” is deployed in URL “http://localhost:9999/ws/hello“.
4. Test It
You can test the deployed web service by accessing the generated WSDL (Web Service Definition Language) document via this URL “http://localhost:9999/ws/hello?wsdl” .
Web Service Clients
Ok, web service is deployed properly, now let’s see how to create web service client to access to the published service.
1. Java Web Service Client
Without tool, you can create a Java web service client like this :
package com.mkyong.client; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; import com.mkyong.ws.HelloWorld; public class HelloWorldClient{ public static void main(String[] args) throws Exception { URL url = new URL("http://localhost:9999/ws/hello?wsdl"); //1st argument service URI, refer to wsdl document above //2nd argument is service name, refer to wsdl document above QName qname = new QName("http://ws.mkyong.com/", "HelloWorldImplService"); Service service = Service.create(url, qname); HelloWorld hello = service.getPort(HelloWorld.class); System.out.println(hello.getHelloWorldAsString("mkyong")); } }
Output
Hello World JAX-WS mkyong
2. Java Web Service Client via wsimport tool
Alternative, you can use “wsimport” tool to parse the published wsdl file, and generate necessary client files (stub) to access the published web service.
This wsimport tool is bundle with the JDK, you can find it at “JDK_PATH/bin” folder.
Issue “wsimport” command.
wsimport -keep http://localhost:9999/ws/hello?wsdl
It will generate necessary client files, which is depends on the provided wsdl file. In this case, it will generate one interface and one service implementation file.
File : HelloWorld.java
package com.mkyong.ws; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; /** * This class was generated by the JAX-WS RI. * JAX-WS RI 2.1.1 in JDK 6 * Generated source version: 2.1 * */ @WebService(name = "HelloWorld", targetNamespace = "http://ws.mkyong.com/") @SOAPBinding(style = SOAPBinding.Style.RPC) public interface HelloWorld { /** * * @param arg0 * @return * returns java.lang.String */ @WebMethod @WebResult(partName = "return") public String getHelloWorldAsString( @WebParam(name = "arg0", partName = "arg0") String arg0); }
File : HelloWorldImplService.java
package com.mkyong.ws; import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; import javax.xml.ws.WebEndpoint; import javax.xml.ws.WebServiceClient; import javax.xml.ws.WebServiceFeature; /** * This class was generated by the JAX-WS RI. * JAX-WS RI 2.1.1 in JDK 6 * Generated source version: 2.1 * */ @WebServiceClient(name = "HelloWorldImplService", targetNamespace = "http://ws.mkyong.com/", wsdlLocation = "http://localhost:9999/ws/hello?wsdl") public class HelloWorldImplService extends Service { private final static URL HELLOWORLDIMPLSERVICE_WSDL_LOCATION; static { URL url = null; try { url = new URL("http://localhost:9999/ws/hello?wsdl"); } catch (MalformedURLException e) { e.printStackTrace(); } HELLOWORLDIMPLSERVICE_WSDL_LOCATION = url; } public HelloWorldImplService(URL wsdlLocation, QName serviceName) { super(wsdlLocation, serviceName); } public HelloWorldImplService() { super(HELLOWORLDIMPLSERVICE_WSDL_LOCATION, new QName("http://ws.mkyong.com/", "HelloWorldImplService")); } /** * * @return * returns HelloWorld */ @WebEndpoint(name = "HelloWorldImplPort") public HelloWorld getHelloWorldImplPort() { return (HelloWorld)super.getPort( new QName("http://ws.mkyong.com/", "HelloWorldImplPort"), HelloWorld.class); } /** * * @param features * A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy. * Supported features not in the <code>features</code> parameter will have their default values. * @return * returns HelloWorld */ @WebEndpoint(name = "HelloWorldImplPort") public HelloWorld getHelloWorldImplPort(WebServiceFeature... features) { return (HelloWorld)super.getPort( new QName("http://ws.mkyong.com/", "HelloWorldImplPort"), HelloWorld.class, features); } }
Now, create a Java web service client which depends on the above generated files.
package com.mkyong.client; import com.mkyong.ws.HelloWorld; import com.mkyong.ws.HelloWorldImplService; public class HelloWorldClient{ public static void main(String[] args) { HelloWorldImplService helloService = new HelloWorldImplService(); HelloWorld hello = helloService.getHelloWorldImplPort(); System.out.println(hello.getHelloWorldAsString("mkyong")); } }
Here’s the output
Hello World JAX-WS mkyong
3. Ruby Web Service Client
Often time, web service development is mixed use with other programming language. So, here’s a Ruby web service client example, which is used to access the published JAX-WS service.
# package for SOAP-based services require 'soap/wsdlDriver' wsdl_url = 'http://localhost:9999/ws/hello?wsdl' service = SOAP::WSDLDriverFactory.new(wsdl_url).create_rpc_driver # Invoke service operations. data1 = service.getHelloWorldAsString('mkyong') # Output results. puts "getHelloWorldAsString : #{data1}"
Output
getHelloWorldAsString : Hello World JAX-WS mkyong
Tracing SOAP Traffic
From top to bottom, showing how SOAP envelope flows between client and server. See #1 web service client again :
URL url = new URL("http://localhost:9999/ws/hello?wsdl"); QName qname = new QName("http://ws.mkyong.com/", "HelloWorldImplService"); Service service = Service.create(url, qname); HelloWorld hello = service.getPort(HelloWorld.class); System.out.println(hello.getHelloWorldAsString("mkyong"));
To monitor SOAP traffic is very easy, see this guide – “How to trace SOAP message in Eclipse IDE“.
1. Request a WSDL file
First, client send a wsdl request to service endpoint, see HTTP traffic below :
Client send request :
GET /ws/hello?wsdl HTTP/1.1 User-Agent: Java/1.6.0_13 Host: localhost:9999 Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2 Connection: keep-alive
Server send response :
HTTP/1.1 200 OK Transfer-encoding: chunked Content-type: text/xml;charset=utf-8 <?xml version="1.0" encoding="UTF-8"?> <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws.mkyong.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.mkyong.com/" name="HelloWorldImplService"> <types></types> <message name="getHelloWorldAsString"> <part name="arg0" type="xsd:string"></part> </message> <message name="getHelloWorldAsStringResponse"> <part name="return" type="xsd:string"></part> </message> <portType name="HelloWorld"> <operation name="getHelloWorldAsString" parameterOrder="arg0"> <input message="tns:getHelloWorldAsString"></input> <output message="tns:getHelloWorldAsStringResponse"></output> </operation> </portType> <binding name="HelloWorldImplPortBinding" type="tns:HelloWorld"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"></soap:binding> <operation name="getHelloWorldAsString"> <soap:operation soapAction=""></soap:operation> <input> <soap:body use="literal" namespace="http://ws.mkyong.com/"></soap:body> </input> <output> <soap:body use="literal" namespace="http://ws.mkyong.com/"></soap:body> </output> </operation> </binding> <service name="HelloWorldImplService"> <port name="HelloWorldImplPort" binding="tns:HelloWorldImplPortBinding"> <soap:address location="http://localhost:9999/ws/hello"></soap:address> </port> </service> </definitions>
2. hello.getHelloWorldAsString()
A second call, client put method invoke request in SOAP envelope and send it to service endpoint. At the service endpoint, call the requested method and put the result in a SOAP envelope and send it back to client.
Client send request :
POST /ws/hello HTTP/1.1 SOAPAction: "" Accept: text/xml, multipart/related, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2 Content-Type: text/xml; charset=utf-8 User-Agent: Java/1.6.0_13 Host: localhost:9999 Connection: keep-alive Content-Length: 224 <?xml version="1.0" ?> <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> <S:Body> <ns2:getHelloWorldAsString xmlns:ns2="http://ws.mkyong.com/"> <arg0>mkyong</arg0> </ns2:getHelloWorldAsString> </S:Body> </S:Envelope>
Server send response :
HTTP/1.1 200 OK Transfer-encoding: chunked Content-type: text/xml; charset=utf-8 <?xml version="1.0" ?> <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> <S:Body> <ns2:getHelloWorldAsStringResponse xmlns:ns2="http://ws.mkyong.com/"> <return>Hello World JAX-WS mkyong</return> </ns2:getHelloWorldAsStringResponse> </S:Body> </S:Envelope>
Done, any comments are appreciated.






There is a small mistake in your downloaded files:
in the HelloWorldClient.java file the
URL url = new URL(“http://localhost:8888/ws/hello?wsdl”);
should be:
URL url = new URL(“http://localhost:9999/ws/hello?wsdl”);
it’s 9999 in the web page, but 8888 in the downloaded files. Any way, very good tutoral
thanks a lot.
Thank you for this blog. This helped me to understand soap webservices. Many posts in the internet are explaining it, but not that simple, sometimes it’s so confusing. I found all I need here.
I understand, i suffered the same also, that why wrote this simple guide :)
Thanks a lot!
This example is very simple and it can help everyone to write a java ws server or client in a fast way!
Good work!
My small application and the publisher works fine, thanks for the tutorial! What should I do to run the app in jboss instead of using the endpoint publisher?
Very good site. You really elaborate on the smallest details which can sometimes be very confusing and not documented well. Thank you for clearing things up in a very straight forward way.
Just thank you again )
very useful tutorial helped me to understand Web services clearly. Appreciate your efforts
Hi dude, you have made my life easier. thanks a lot
you simply rock :)
All of your tutorial are really good. you are simply the best.
Thanks for the efforts. Webservices seem easy after reading your examples! I had ignored this topic out of boredom. You made it lively!
Good to know it did help you in somewhere :)
nice tutorials. Everything worked fine in the first try itself..
THanks a loot.
But tcp monitor did not show the traffic… did everything as instructed,
Double confirm your port setting, make sure it’s configure properly.
show a demo where in method accepts object as a parameter and return object
Simple and Excellent tutorial, well organized, keep up the good work.
I like a lot the clear style of the examples but unfortunately I cannot make them work. Everything compiles and runs fine but it doesnt generate an wsdl file at all
Keep on getting message that resource is not found which seems logical since there is no wsdl file
I’m using Netbeans and glassfish. I create a web project but don’t see webservices in the project tree .
Would it be possible to give me some guidance ?
As You’ve already grasped I’m a newbee to this.
Tx in advance
Don’t depends on Netbean to create web services, the generated web services from Netbean are complex and hard to maintenance (at least to me). JAX-WS is easily to develop, try create it without the help of Netbean, and you will learn a lot.
In WS development, normally we use code to wsdl method, because it ‘s fast and easy. Often time, we just code and never worry about the wsdl file. After WS is deployed, the WS runtime will generate wsdl file for WS client to consume, automatically.
My advice is don’t use netbean, it add too much extra codes on your WS, JAX-WS is a standard, a simple “java” command will get it run and deployed correctly.
thanks for such a great effort on jax-ws
Valid services are: {http://Model/}
Why am I taking this exception ?
please post your error stacks
how can i deploy this to prod server…
Hi,
in the source code, the class HelloWorldClient.java is not correct. While it shows in this page the next line:
URL url = new URL(“http://localhost:9999/ws/hello?wsdl”);
In the source code zip, it shows like that:
URL url = new URL(“http://localhost:8888/ws/hello?wsdl”);
So unless you change the port back to 9999 it will not work.
Ok, I did not realize that it was like that because it was calling the monitor first…
It’s really good point. I encountered the same problem. So once again if one runs HelloWorldClient.java without TCP Monitor one should put
URL url = new URL(“http://localhost:9999/ws/hello?wsdl”); to make everything work.
If one runs TCP Monitor and fill in TCP monitor information exactly as here http://www.mkyong.com/webservices/jax-ws/how-to-trace-soap-message-in-eclipse-ide/ then one really needs the following:
URL url = new URL(“http://localhost:8888/ws/hello?wsdl”);
Could the author take this remark to consideration?
In general this is good article.
it works nice
I am new to web service. Got a dumb question for you all.
The client already have created stuf code out of the WSDL. Why is it asking for WSDL again in the first request? Why can it just send the second request alone?
Also, I notice sometimes the endpoint does not include “?wsdl”. what is the difference between including “?wsdl” and not including “?wsdl”?
Any comment is appreciated. Thanks,
1. It’s depends on how you write the code. If you sure your client WSDL will never change, then just get a copy and integrate with your application locally.
2. WSDL is for SOAP service. Appending “?wsdl” at the end of the web service means to get the wsdl file content. Without “?wsdl” is the WS URL. For restful web service, it does not contains any of “?wsdl” as well.
Hope help.
That helps. Thanks a lot. And I love your site. Easy to follow and very well organized. Better than any other tutorial site I have ever seen. Keep up the good work :-)
Thanks a lot!!!!
I was wondering such a nice WS example since very long time.
Now I am relaxed doing all these example.
I request to you,Pls give EJB 3.0 tutorial such a nice way.
Thanks in advance.
[...] JAX-WS hello world example – RPC Style Tutorial to show you how to create a rpc style web service endpoint by using JAX-WS, and web service client in Java, wsimport and Ruby. [...]
[...] commkyongwsServerInfoImplService.java Note For complete example, please visit this JAX-WS hello world example article, refer to the section “2. Java Web Service Client via wsimport [...]
[...] Tomcat to support SSL and deployed this simple hello world web service. And use following client connect to the deployed web service over SSL connection : package [...]
Hello – I preferably should suggest, impressed with your site. I had no trouble navigating through all the tabs and so guidance had been genuinely easy to access. I came across what I hoped for very quickly all the way. Plus extremely good. Would most likely appreciate it in the event you add forums or something, it becomes much easier a fantastic way for your consumers to work together. Fine job..
[...] you how to use JAX-WS to create a SOAP-based web service (document style) endpoint. Compare with RPC style, it need some extra efforts to get it [...]
[...] the traced messaged in the “TCP/IP Monitor” view if any. Note You can copy this JAX-WS web service example and do the testing yourself. For Netbean users In Netbean IDE, you can use TCP monitor to trace [...]