Coding a web service

There are two approaches to developing a web service, as follow:

  • The top-down approach, first develop the WSDL and use a utility, to generate the necessary glue code and stubs, then fill in the code for the business logic in the stub classes.
  • The bottom-up approach, you code the web service first and then you can package the web service and deploy it, the JBoss Web Services deployer will automatically generate the WSDL.

We use the last approach. Create the following Java file.

package org.app.ws;

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

@WebService
public class CellPhoneTask {
 private HashMap<String, String> task;

 public CellPhoneTask() {
 init();
 }

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

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

}

Finally, we need to create a web.xml file declaring the web service class as a servlet.

<?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.CellPhoneTask</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>

To deploy the web service; you copy the WAR file to the deploy directory. You can view web services deployed  to the application server by going to the URL http://localhost:8080/jbossws/services.

Click the URL identified by Endpoint Address to access the 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: