Create a simple Web Service


This code example shows how to create a simple web service. We use the annotation @WebService to declare the class as a such.
The annotation @WebMethod is provided at method level to declare it as an operation for the web service.
The operation getTime of the JavadbWebService simply returns the current time.


package com.javadb.ws.example;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.jws.WebMethod;
import javax.jws.WebService;

/**
 *
 * @author www.javadb.com
 */


@WebService()
public class JavadbWebService {
    
    @WebMethod
    public String getTime() {
        
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        return (sdf.format(calendar.getTime()));
        
    }

}


The result of a call to the operation getTime produces something like this:


14:15


This is the SOAP request and response to the web service:


<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Header/>
    <S:Body>
        <ns2:getTime xmlns:ns2="http://example.ws.javadb.com/"/>
    </S:Body>
</S:Envelope>



<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
        <ns2:getTimeResponse xmlns:ns2="http://example.ws.javadb.com/">
            <return>14:15</return>
        </ns2:getTimeResponse>
    </S:Body>
</S:Envelope>



Do you know your Java?
Take a Ten-Question-Java-Quiz!

Bookmark and Share




Need help with your Java code? It's secure and confidential.
This is how it works:
Send a detailed description of what you need help with, the more details the better. Also provide a deadline for when it has to be finished. More time means better chance of putting your request into the schedule.

If the request is serious you will shortly receive an email with the price, to which you have to respond if you accept.

Once you have accepted, the work will begin on developing your code by an experienced Java developer. When the code is finished a link to a secure payment will be sent to you.

The source code is then sent to you once the payment is completed.

IMPORTANT! The request needs to be very detailed, else it may be ignored.


Write your detailed request here:

E-mail address: