Hello World Web Service Example

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Hello, here we will see a hello world web service example using SOAP. Instead of using any servlet container or application server, we will simply use core java to publish this hello world web service.

Hello World Web Service:

Right now we are not going deeper into web services or their approaches. Our objective is to just write a web service and consume it. We will see its detailed explanation in next article.

Simply create a new java project in eclipse using File > New > Java Project. 

  • Create HelloWorldServer interface which has sayHello() method which takes name parameter.
  • Create HelloWorldServerImpl which implements HelloWorldServer  and its method sayHello(String name).
  • Create HelloWorldServerPublisher which will publish web service.
  • Create HelloWorldClient to consume hello world web service.

HelloWorldServer:

Create following interface which is Service Endpoint Interface for hello world web service

HelloWorldServerImpl

Create following class implementing HelloWorldServer. This is Service Implementation Bean

 HelloWorldServerPublisher.

Now that we’ve created hello world web service, we can publish it using code below.

Run above program and hello world web service will be published. visit url http://127.0.0.1:9876/hw?wsdl and you can the wsdl generated below.

This is this wsdl for hello world web service. Let not delve into it deeper right now. We will see how to consume this using a java client.

HelloWorldClient

Now run this program and you will see the output as below.

 

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

13 thoughts on “Hello World Web Service Example

  • Pingback:web service hello world example explained - theJavaGeek

  • Pingback:using wsimport command to generate web service client - theJavaGeek

  • March 18, 2015 at 5:47 pm
    Permalink

    Hello Prasad,

    I changed the return message in the “HelloWorldServerImpl” class and tried to publish the HelloworldService by running HelloWorldServerPublisher. I have got the following exception:

    xception in thread “main” com.sun.xml.internal.ws.server.ServerRtException: Server Runtime Error: java.net.BindException: Address already in use: bind
    at com.sun.xml.internal.ws.transport.http.server.ServerMgr.createContext(ServerMgr.java:100)
    at com.sun.xml.internal.ws.transport.http.server.HttpEndpoint.publish(HttpEndpoint.java:67)
    at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.publish(EndpointImpl.java:144)
    at com.sun.xml.internal.ws.spi.ProviderImpl.createAndPublishEndpoint(ProviderImpl.java:102)
    at javax.xml.ws.Endpoint.publish(Endpoint.java:170)
    at webService.HelloWorldServerPublisher.main(HelloWorldServerPublisher.java:9)
    Caused by: java.net.BindException: Address already in use: bind
    at sun.nio.ch.Net.bind(Native Method)
    at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:119)
    at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:59)
    at sun.net.httpserver.ServerImpl.(ServerImpl.java:70)
    at sun.net.httpserver.HttpServerImpl.(HttpServerImpl.java:32)
    at sun.net.httpserver.DefaultHttpServerProvider.createHttpServer(DefaultHttpServerProvider.java:17)
    at com.sun.net.httpserver.HttpServer.create(HttpServer.java:111)
    at com.sun.xml.internal.ws.transport.http.server.ServerMgr.createContext(ServerMgr.java:82)
    … 5 more

    If I specify another port number (say 9877) in the URL, it is being published successfully. My question is, Isn’t it possible to publish to the same url without creating another? If it is possible, how to get rid of this exception?

    Reply
    • May 1, 2015 at 2:11 am
      Permalink

      The only way I found to solve this problem was to kill process running publish.
      First you have to find the process (ps -A|grep java) and then, kill it (kill PID#). If there are more than one, probably is the last (biggest PID number or the lowest time).
      In my case I had two – the first was running eclipse and the second was running publish.Ex:
      PID TTY TIME CMD
      29028 ? 00:00:27 java
      29132 ? 00:00:00 java

      Reply
  • May 4, 2016 at 9:33 pm
    Permalink

    Excellent, thank you. Why can’t more tutorials be a simple as this. I had followed another tutorial a few days ago which had far more complicated steps and resulted in installing lots of additional software.

    Reply
  • May 26, 2016 at 4:38 pm
    Permalink

    showing some probleam
    javax.xml.ws.WebServiceException: {http://webser.com/}HelloWorldServerImplService is not a valid service. Valid services are: {http://webser/}HelloWorldServerImplService
    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.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 webser.HelloWorldClient.main(HelloWorldClient.java:20)

    Reply
    • November 10, 2016 at 8:12 am
      Permalink

      I hope you have solved your issue. Anyways, the solution is right there: change http://webser.com to http://webser/ in your client class. Your package name must be webser, if you want to use webser.com as your nameSpaceURI value in QName constructor, change the package name to com.webser.
      I am new to all this too, just sharing what I realized while doing some research.

      Reply
  • September 16, 2016 at 3:09 pm
    Permalink

    how can we send SOAP Request over http to this web serrvice..

    Reply
  • November 16, 2016 at 3:01 pm
    Permalink

    @Tejas look at the package name which u have given in client class

    Reply
  • January 24, 2017 at 5:10 pm
    Permalink

    Can u guide me how to set the /hw path

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *