Thursday, September 19, 2013

Simple JAX-WS Webservice - Calculator Implementation

First we will create a Simple JAX-WS Web Service. if you do not know how to create such web service, you can refer my previous article that describes how to develop simple JAX-WS Service.

Click Here to See How To Create Simple JAX-WS Service

i assumed that you have created a simple web service and it is up and running. we will modify that web service to provide the calculator based web service operations. in this calculator web service, we support for three operations.

1. Addition
2. Multiplication
3. Subtraction

you can see the SEI (Service Endpoint Interface) and SIB (Service Implementation Bean) as follows.


SEI - CalculatorService.java

package com.chathurangaonline.sample.jaxws;

import javax.jws.WebMethod;
import javax.jws.WebService;

/**
 * <p>
 *     Service Endpoint Interface for the calculator service.
 * </p>
 */
@WebService
public interface CalculatorService {

    @WebMethod
    long add(long number1, long number2);

    @WebMethod
    long subtract(long number1, long number2);

    @WebMethod
    long multiply(long number1, long number2);
}


SIB - CalculatorServiceImpl.java

package com.chathurangaonline.sample.jaxws;

import javax.jws.WebService;

/**
 * <p>
 *     Service Implementation Bean (SIB) for CalculatorService endpoint interface
 * </p>
 */
@WebService
public class CalculatorServiceImpl implements CalculatorService{


    @Override
    public long add(long number1, long number2) {
        return number1+number2;
    }

    @Override
    public long subtract(long number1, long number2) {
        return number1+number2;
    }

    @Override
    public long multiply(long number1, long number2) {
        return number1*number2;
    }
}


Service Publisher (Only for Development Environment)

package com.chathurangaonline.sample.jaxws;

import javax.xml.ws.Endpoint;

/**
 * <p>
 *     simple web service publisher for testing the web service in the development environment.
 *     remove this publisher class in the production environment and deploy the web service with
 *     j2ee compliant web server.
 * </p>
 */
public class CalculatorServicePublisher {

    public static void main(String[] args) {

        Endpoint.publish("http://localhost:6655/jaxWsCalcService",new CalculatorServiceImpl());
    }
}


once everything is added correctly, our project will looks like as follows.



now we have a calculator web service that is ready for the deployment. now we will publish our web service using the Endpoint.publish() method and check whether it is working. since we are uisng this in the test environment, we do not need to deploy this is a J2EE application server at the moment. but in our next article we will look at how to deploy the web service in tomcat server.

once the web service is published you can access the web service in the following url.
http://localhost:6655/jaxWsCalcService

once the web service is published, we need to write/generate a web service client to comsume the web service. it will be discussed in following blog post.


How to generate JAX-WS service client and integrate with Java Application

you can download the source code from following gitHub repo.

https://github.com/chathurangat/jaxws-calculator-service

Hope this will be helpful for you!

Cheers
Chathuranga Tennakoon
www.chathurangaonline.com







No comments:

Post a Comment