Peter Arockiaraj

Can you hear me now?…Good!

Developing Web Services by Using Metro Webservices Framework


Introduction

In this article we are going to develop a web service by using Metro Web Services framework. This article provides steps (step by step ) to create &  deploy web services by using Metro. Please go through below to find sample web services.

Software Requirements

  1. EClipse (Java IDE)- Optional
  2. Metro Web services framework (Download from https://metro.dev.java.net/)

The Code

In this example, we are going to create a Arithmetic service, to add two numbers. In this example, we are going to use a code-first approach for this service using JAX-WS annotations.

Creating Server Application

Step 1:  Download Following Jar files.

activation.jar

FastInfoset.jar

http.jar

jaxb-api.jar

jaxb-impl.jar

jaxb-xjc.jar

jaxws-api.jar

jsr181-api.jar

jsr250-api.jar

saaj-api.jar

saaj-impl.jar

jsr173_api.jar

woodstox.jar

resolver.jar

stax-ex.jar

streambuffer.jar

jaxws-rt.jar

mimepull.jar

webservices-api.jar

webservices-extra-api.jar

webservices-extra.jar

webservices-rt.jar

webservices-tools.jar

activation.jar

FastInfoset.jar

http.jar

jaxb-api.jar

jaxb-impl.jar

jaxb-xjc.jar

jaxws-api.jar

jsr181-api.jar

jsr250-api.jar

saaj-api.jar

saaj-impl.jar

jsr173_api.jar

woodstox.jar

resolver.jar

stax-ex.jar

streambuffer.jar

jaxws-rt.jar

mimepull.jar

webservices-api.jar

webservices-extra-api.jar

webservices-extra.jar

webservices-rt.jar

webservices-tools.jar

Step 2: Create New Java project in eclipse (MetroWebServices).

Step 3: Create WEB-INF,ant folder inside project folder.

Step 4: Create classes folder inside WEB-INF folder.

Step 5: Create lib folder inside WEB-INF folder.

Step 6: Copy all the jar file into lib folder.

Step 7: Add all jar files into classpath (In Eclipse set java build path->Libraries). Add set Default output folder into MetroWebServices/WEB-INF/classes

Step 8: Create ArithmeticException.java for user defined exception.

package com.sungard.sample.metro.webservices;

public class ArithmeticException extends Exception {

String detail;

public ArithmeticException (String message, String detail) {

super (message);

this.detail = detail;

}

public String getDetail () {

return detail;

}

}

Step 9: Create Web services class ArithmeticImpl.java

package com.sungard.sample.metro.webservices;

import javax.jws.WebService;

import javax.jws.WebMethod;

@WebService

public class ArithmeticImpl {

/**

* @param number1

* @param number2

* @return The sum

* @throws ArithmeticException

*             if any of the numbers to be added is negative.

*/

@WebMethod

public int addTwoNumbers(int number1, int number2) throws ArithmeticException {

if (number1 < 0 || number2 < 0) {

throw new ArithmeticException(“Negative number cant be added!”,

“Numbers: ” + number1 + “, ” + number2);

}

return number1 + number2;

}

}

Step 10: Inside Web-Inf folder create following xml to define endpoint class.

sun-jaxws.xml

<?xml version=“1.0” encoding=“UTF-8”?>

<endpoints xmlns=http://java.sun.com/xml/ns/jax-ws/ri/runtime&#8217; version=‘2.0’>

<endpoint

name=‘sungardServices’

implementation=‘com.sungard.sample.metro.webservices.ArithmeticImpl’

url-pattern=‘/addTwoNumbers’/>

</endpoints>

Step 11: Create Web.xml file inside web-inf folder as follows.

web.xml

<?xml version=“1.0” encoding=“UTF-8”?>

<web-app version=“2.4” xmlns=http://java.sun.com/xml/ns/j2ee&#8221;

xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance&#8221;

xsi:schemaLocation=http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd&#8221;>

<description>sungardServices</description>

<display-name>sungardServices</display-name>

<listener>

<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>

</listener>

<servlet>

<description>JAX-WS endpoint – sungardServices</description>

<display-name>sungardServices</display-name>

<servlet-name>sungardServices</servlet-name>

<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>sungardServices</servlet-name>

<url-pattern>/addTwoNumbers</url-pattern>

</servlet-mapping>

<session-config>

<session-timeout>60</session-timeout>

</session-config>

</web-app>

Step 12: Create build.xml file inside ant folder.

build.xml

<?xml version=”1.0″ encoding=”UTF-8″?>

<project name=”ws” basedir=”../” default=”archive”>

<target name=”archive”>

<jar destfile=”metroservices.war”>

<fileset dir=”${basedir}”>

<include name=”**/*.class” />

</fileset>

<fileset dir=”${basedir}”>

<include name=”**/*.jar” />

</fileset>

<fileset dir=”${basedir}”>

<include name=”**/*.xml” />

<exclude name=”**/*build*” />

</fileset>

</jar>

</target>

</project>

Step 13: Run ant build

Step 14: Deploy metroservices.war file in tomcat server (or in some web server).

Step 15: To verify application deployed successfully or not use following url.
http://localhost:8080/metroservices/addTwoNumbers?wsdl

Step 16: Browser will show wsdl file our web service.

Creating Client Application

Step 1: Create customizationfiles folder inside project folder.

Step 2: Crate following xml file inside customizationfiles folder.

custom-client.xml

<?xml version=“1.0” encoding=“UTF-8” standalone=“yes”?>

<bindings

xmlns:xsd=http://www.w3.org/2001/XMLSchema&#8221;

xmlns:wsdl=http://schemas.xmlsoap.org/wsdl/&#8221;

wsdlLocation=http://localhost:8080/metroservices/addTwoNumbers?wsdl&#8221;

xmlns=http://java.sun.com/xml/ns/jaxws&#8221;>

<bindings node=“wsdl:definitions”>

<package name=“com.sungard.sample.metro.webservices.client”/>

</bindings>

</bindings>

Step 3: Crate following xml file inside customizationfiles folder.

custom-schema.xml

<?xml version=“1.0” encoding=“UTF-8” standalone=“yes”?>

<bindings

xmlns:xsd=http://www.w3.org/2001/XMLSchema&#8221;

xmlns=http://java.sun.com/xml/ns/jaxb&#8221;

version=“1.0”>

<bindings schemaLocation=http://localhost:8080/metroservices/addTwoNumbers?xsd=1&#8221; node=“/xsd:schema”>

<schemaBindings>

<package name=“com.sungard.sample.metro.webservices.client”/>

</schemaBindings>

</bindings>

</bindings>

Step 4: Create following build xml for client application inside ant folder.

buildclient.xml

<?xml version=”1.0″ encoding=”UTF-8″?>

<project name=”ws” basedir=”../” default=”generate-client”>

<path id=”wsit.classpath”>

<pathelement location=”${java.home}/../lib/tools.jar” />

<fileset dir=”${basedir}/WEB-INF/lib/”>

<include name=”jaxb-xjc.jar” />

<include name=”jaxb-api.jar” />

<include name=”jaxb-impl.jar” />

<include name=”webservices-api.jar” />

<include name=”webservices-rt.jar” />

<include name=”webservices-tools.jar” />

<include name=”webservices-extra.jar” />

<include name=”webservices-extra-api.jar” />

<include name=”javaee.jar” />

<include name=”activation.jar” />

</fileset>

</path>

<taskdef name=”wsimport” classname=”com.sun.tools.ws.ant.WsImport”>

<classpath refid=”wsit.classpath” />

</taskdef>

<target name=”generate-client”>

<wsimport debug=”false” verbose=”false” keep=”true”

extension=”${extension}” destdir=”${basedir}/src”

wsdl=”http://localhost:8080/metroservices/addTwoNumbers?wsdl”&gt;

<binding dir=”${basedir}/customizationfiles” includes=”custom-client.xml, custom-schema.xml” />

</wsimport>

</target>

</project>

Step 5: Run buildclient.xml file using ant.

Step 6: Create Client.java file to invoke webservices.

package com.sungard.sample.metro.webservices.client;

public class Client {

public static void main(String[] args) {

try {

ArithmeticImpl impl = new ArithmeticImplService()

.getArithmeticImplPort();

int number1 = 10;

int number2 = 20;

System.out

.printf(“Invoking addNumbers(%d, %d)\n”, number1, number2);

int result;

result = impl.addTwoNumbers(number1, number2);

System.out.printf(“The result of adding %d and %d is %d.\n\n”,

number1, number2, result);

number1 = -10;

System.out

.printf(“Invoking addNumbers(%d, %d)\n”, number1, number2);

result = impl.addTwoNumbers(number1, number2);

System.out.printf(“The result of adding %d and %d is %d.\n”,

number1, number2, result);

} catch (ArithmeticException_Exception e) {

e.printStackTrace();

}

}

}

Step 7: run Client.java file in your eclipse. You will be getting following output.

Invoking addNumbers(10, 20)

The result of adding 10 and 20 is 30.

Invoking addNumbers(-10, 20)

com.sungard.sample.metro.webservices.client.ArithmeticException_Exception: Negative number cant be added!

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)

at java.lang.reflect.Constructor.newInstance(Constructor.java:494)

at com.sun.xml.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:141)

at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:119)

at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:89)

at com.sun.xml.ws.client.sei.SEIStub.invoke(SEIStub.java:118)

at $Proxy38.addTwoNumbers(Unknown Source)

at com.sungard.sample.metro.webservices.client.Client.main(Client.java:24)

Caused by: com.sungard.sample.metro.webservices.ArithmeticException: Negative number cant be added!

at com.sungard.sample.metro.webservices.ArithmeticImpl.addTwoNumbers(ArithmeticImpl.java:19)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:585)

at com.sun.xml.ws.api.server.InstanceResolver$1.invoke(InstanceResolver.java:246)

at com.sun.xml.ws.server.InvokerTube$2.invoke(InvokerTube.java:146)

at com.sun.xml.ws.server.sei.EndpointMethodHandler.invoke(EndpointMethodHandler.java:257)

at com.sun.xml.ws.server.sei.SEIInvokerTube.processRequest(SEIInvokerTube.java:93)

at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:598)

at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:557)

at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:542)

at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:439)

at com.sun.xml.ws.server.WSEndpointImpl$2.process(WSEndpointImpl.java:243)

at com.sun.xml.ws.transport.http.HttpAdapter$HttpToolkit.handle(HttpAdapter.java:471)

at com.sun.xml.ws.transport.http.HttpAdapter.handle(HttpAdapter.java:244)

at com.sun.xml.ws.transport.http.servlet.ServletAdapter.handle(ServletAdapter.java:135)

at com.sun.xml.ws.transport.http.servlet.WSServletDelegate.doGet(WSServletDelegate.java:129)

at com.sun.xml.ws.transport.http.servlet.WSServletDelegate.doPost(WSServletDelegate.java:160)

at com.sun.xml.ws.transport.http.servlet.WSServlet.doPost(WSServlet.java:75)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)

at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:875)

at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)

at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)

at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)

at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)

at java.lang.Thread.run(Thread.java:595)

October 6, 2009 Posted by | Web Services | Leave a comment