Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a webservice work

I am new to webservices and i want to implement webservices by using java in my eclipse project.

So can anyone tel me how to implement and creating a project please

Thanks

like image 840
user314344 Avatar asked Dec 28 '10 06:12

user314344


1 Answers

As defined by W3C web service is a software system to support interoperable machine-to-machine interaction over a network. More elaborately a system consumes service from other software system.

Web services has two major classes:

  • REST compliant
  • arbitrary web service

To implement web service one need to choose one category on the basis of his/her requirement. Java has bunch APIS to implement web services on both category.

Requirements before implementing web service is :

  • XML
  • WSDL (web service description language)
  • SOAP protocol etc

REST based is bit easy to implement compare to other category. So it's better to start with REST complaint web services.

How web service works :

WS works as request-response paradigm , there is an entity which will request for some service to it's specific counterpart namely service provider entity. Upon request, service provider will respond with a response message. So there are two message involved hear one Request message (XML)and one Response message (XML). There are bunch of ways to achieve these. Detail can be found at web service architecture

Beginner can start with JERSEY jsr311 standard reference implementation to build RESTful web services.

Example (jersey specific):

Step One : Creating root resources

// The Java class will be hosted at the URI path "/helloworld"
   @Path("/helloworld")
   public class HelloWorldResource {

       @GET 
       @Produces("text/plain")
      public String getClichedMessage() {
          return "Hello World";
      }
  }

Step Two : Deploying

public class Main {

  private static URI getBaseURI() {
      return UriBuilder.fromUri("http://localhost/").port(8080).build();
  }

  public static final URI BASE_URI = getBaseURI();

  protected static HttpServer startServer() throws IOException {
      System.out.println("Starting ...");
      ResourceConfig resourceConfig = new PackagesResourceConfig("com.sun.jersey.samples.helloworld.resources");
      return GrizzlyServerFactory.createHttpServer(BASE_URI, resourceConfig);
  }

  public static void main(String[] args) throws IOException {
      HttpServer httpServer = startServer();
      System.out.println(String.format("Jersey app started with WADL available at "
              + "%sapplication.wadl\nTry out %shelloworld\nHit enter to stop it...",
              BASE_URI, BASE_URI));
      System.in.read();
      httpServer.stop();
  }    

}

REST REFERENCE - by Roy T . Fielding

like image 80
Asraful Avatar answered Sep 19 '22 19:09

Asraful