JAX-WS: Webservices with Java EE 6

SOAP webservices were never so much fun and ease before Java EE 6! So without further ado, we’ll see how to get a JAX-WS service up and running in a matter of minutes! The source code is hosted on Google codes, which you can download and directly import into your IDE.

(I used JBoss Developer Studio 5 and JBoss Application Server 7 )

Download the Source Code

We’ll expose a PizzaService as a JAX-WS. Our PizzaService would be a very light-weight service which will have just two functions:-

  1. getPizza – This will buy/get you a Pizza object.
  2. returnPizza – This will help you return a stale/cold/substandard Pizza back!

Let’s define Pizza entity class:-

Pizza.java

package webservice.jaxws.resources;
import java.util.Date;

public class Pizza {
 private String size;
 private Date dateOfManufacture;
 private float cost;

/*Setters and Getters removed for brevity */

}

The PizzaService Interface
PizzaService.java


package webservice.jaxws.service;

import webservice.jaxws.resources.Pizza;

public interface PizzaService {
 public Pizza getPizza();
 public String returnPizza(Pizza pizza);
}

Writing the implementation of the above interface.

PizzaServiceImpl.java


package webservice.jaxws.service;

import java.util.Date;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

import webservice.jaxws.resources.Pizza;

@WebService(serviceName="PizzaService", portName="PizzaPort", targetNamespace="generated.jaxws.webservice")
public class PizzaServiceImpl implements PizzaService {

 @Override
 @WebMethod
 @WebResult(name="pizza")
 public Pizza getPizza() {
 //Creating a new Pizza
 Pizza pizza = new Pizza();
 pizza.setSize("Large");
 pizza.setCost(375.45f);
 pizza.setDateOfManufacture(new Date());

 return pizza;
 }

 @Override
 @WebMethod
 @WebResult(name="PizzaReturnStatus")
 public String returnPizza(@WebParam(name="pizza") Pizza pizza) {
 System.out.println("Pizza returned by the customer :(");
 System.out.println("Details of the returned pizza:-");
 System.out.println(pizza);

 return "Pizza successfully returned.";
 }

}

To expose a class as webservice all you need to do is to annotate it with @Webservice and you’re done!

The other annotations make the generated WSDL file pretty and more-meaningful to read.

  1. @WebMethod – It declares the method as an endpoint. Although the spec says it to be mandatory, but the code still runs once you annotate the service class with @WebService.
  2. @WebResult – It is used when the method is returning something, it gives the returning object a name, else your wsdl file would contain a parameter called “return” which might not seem so much readable once you have got a significant number of methods featuring in a particular webservice.
  3. @WebParam – This is also almost same as @WebResult with the only difference that this annotation is used to make the name of input argument of the endpoint more meaningful. In the absence of @WebParam, the wsdl file would read the input argument as arg0, arg1 etc.

It’s now time to put the code to test and see the output. Run the project and to see the wsdl open up your browser and type in the following url:-

http://localhost:8080/webservice-jaxws/PizzaService?wsdl

The anatomy of the above url is simple enough, http://localhost:8080/webservice-jaxws/ is the server-address and the project location and PizzaService is the service name we defined as an attribute of @WebService annotated at the class PizzaServiceImpl (scroll up to check!)

You can test the above webservice by either writing a client or using Soap-UI which is the easier and quicker way!

UPDATE

A client consuming this webservice can be seen and downloaded from this URL.

That’s all for this one.

9 thoughts on “JAX-WS: Webservices with Java EE 6”

  1. Hi Ankit

    when i tried this and getting bean from ClassPathApplicationContext, via this code

    ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
    “applicationContext.xml”);
    CPizzaServiceImpl bean =(CPizzaServiceImpl)applicationContext.getBean(“pizzaProxy”);
    Pizza pizza = bean.getPizza();

    but I am getting response as null 😦

    I have tried each and every thing. My applicationContext.xml looks like this

    Please suggest It would be really a great help

    Like

Leave a comment