Java web server and client

Java Client Server XML Web Services (JAX-WS) Tutorial

The application which we are going to build consists of two parts:

To start, let’s take a brief look at a Java technology which is built for web services.

1. Understand Java API for XML Web Services (JAX-WS)

There are several technologies that make up the Java web services technology which is a part of Java EE platform, and JAX-WS is the main technology that integrates other ones like JAXB (Java Architecture for XML Binding) and SAAJ (SOAP with Attachments API for Java). In other words, when working with web services in Java, we work with JAX-WS directly, which is in turn, works with dependent technologies like JAXB and SAAJ.

For Java SE, JAX-WS has been included since Java SE 6. That means we can write web service-based console applications from Java SE 6, without downloading JAX-WS implementation package.

    • Home page: Java EE Web Services Technologies
    • Latest version: JAX-WS 2.2 (as of Nov 2012)
    • Download reference implementation: http://jax-ws.java.net
    • API documentation: http://jax-ws.java.net/nonav/jaxws-api/2.2/index.html
      • javax.xml.ws : the core package of JAX-WS.
      • javax.jws : contains annotations to simplify writing code for web services, such as @WebService , @WebMethod , @WebParam …

      In addition, Java SE comes with some command lines tools for simplifying generation of web services code: wgen , wsimport , schemagen and xjc . We will use the wsimport tool for generating some code for client part of the application.

      2. Code the Java web service class

      Now let’s create our web service class. The web service method returns a MD5-hahsed value of an input string. Using the annotations @WebService for the class and @WebMethod for the service method, create a class named MD5WebService.java as follows:

      package net.codejava.webservices.server; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import javax.jws.WebMethod; import javax.jws.WebService; @WebService public class MD5WebService < @WebMethod public String hashString(String input) < try < MessageDigest msgDigest = MessageDigest.getInstance("MD5"); byte[] inputBytes = input.getBytes(); byte[] hashedBytes = msgDigest.digest(inputBytes); StringBuffer sb = new StringBuffer(); for (int i = 0; i < hashedBytes.length; i++) < sb.append(Integer.toString((hashedBytes[i] & 0xff) + 0x100, 16) .substring(1)); >return sb.toString(); > catch (NoSuchAlgorithmException ex) < ex.printStackTrace(); return ""; >> >

      Type the following command to compile the web service class (suppose the current directory is parent of the directory structure for the package: net.codejava.webservices.server ):

      javac net\codejava\webservices\server\MD5WebService.java

      3. Code the server program for Java web service

      To create a simple, lightweight server for deploying the web service, we use the method publish() of the javax.xml.ws.Endpoint class:

      publish(String address, Object implementor)

Оцените статью