Developing a web service with the top-down strategy

Starting with the WSDL file, run wsconsume to generate the class stubs from the WSDL and provide the business logic for the web methods, as follows:

wsconsume -o stubs -k wsgen/CellPhoneTaskService.wsdl
  • The –o option causes the generated files to be placed in a directory named stubs.
  • The –k option indicates that the generated Java source files are to be kept.
  • The file called CellPhoneTask.java contains an interface that defines the web service.

Following, the Java file CellPhoneTaskImpl, that contains the CellPhoneTask’ implementation.

package org.app.ws;

import java.util.HashMap;
import javax.jws.*;

@WebService(
		// 1
		endpointInterface = "org.app.ws.CellPhoneTask",
		// 2
		portName = "CellPhoneTaskPort",
		// 3
		wsdlLocation = "WEB-INF/wsdl/CellPhoneTaskService.wsdl")
// 4
public class CellPhoneTaskImpl implements CellPhoneTask {
	private HashMap task;

	public CellPhoneTaskImpl() {
		init();
	}

	public void init() {
		task = new HashMap();
		task.put("Diana", "98123-123");
		task.put("Diego", "99456-452");
		task.put("Jesy", "99456-456");
	}

	public String getCellPhone(String name) {
		String cellPhone = task.get(name);
		if (cellPhone == null)
			cellPhone = "00000-000";
		return cellPhone;
	}

}
  1. Identifies the interface that defines the web service.
  2. Identifies the port name.
  3. Identifies the location of the WSDL file.
  4. The class must implement the interface

We also have to make one change to the web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <display-name>CellPhoneWebService</display-name>
 <servlet>
 <servlet-name>CellPhoneTask</servlet-name>
 <servlet-class>org.app.ws.CellPhoneTaskImpl</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>CellPhoneTask</servlet-name>
 <url-pattern>/task</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
 <welcome-file>index.html</welcome-file>
 <welcome-file>index.htm</welcome-file>
 <welcome-file>index.jsp</welcome-file>
 <welcome-file>default.html</welcome-file>
 <welcome-file>default.htm</welcome-file>
 <welcome-file>default.jsp</welcome-file>
 </welcome-file-list>
</web-app>

Package all class files along with the WSDL and web.xml files in a WAR file with the following structure:

+---CellPhoneWebService
¦   +---build
¦   ¦   +---classes
¦   ¦       ¦
¦   ¦       +---org
¦   ¦           +---app
¦   ¦               +---ws
¦   ¦                       CellPhoneTask.class
¦   ¦                       CellPhoneTaskImpl.class
¦   ¦                       GetCellPhone.class
¦   ¦                       GetCellPhoneResponse.class
¦   ¦                       package-info.class
¦   ¦
¦   +---src
¦   ¦   +---org
¦   ¦       +---app
¦   ¦           +---ws
¦   ¦                   CellPhoneTask.java
¦   ¦                   CellPhoneTaskImpl.java
¦   ¦                   GetCellPhone.java
¦   ¦                   GetCellPhoneResponse.java
¦   ¦                   package-info.java
¦   ¦
¦   +---WebContent
¦       +---META-INF
¦       ¦       MANIFEST.MF
¦       ¦
¦       +---WEB-INF
¦           ¦   web.xml
¦           ¦
¦           +---lib
¦           +---wsdl
¦                   CellPhoneTaskService.wsdl

Etiquetas:

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s


A %d blogueros les gusta esto: