<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Kevin&#039;s Blog &#187; Web Service</title>
	<atom:link href="http://blog.lckymn.com/tag/web-service/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.lckymn.com</link>
	<description>IT, Java, Ubuntu, Linux</description>
	<lastBuildDate>Thu, 12 Jan 2012 05:48:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Web Service Development using Tomcat and OpenEJB</title>
		<link>http://blog.lckymn.com/2009/11/01/web-service-development-using-tomcat-and-openejb/</link>
		<comments>http://blog.lckymn.com/2009/11/01/web-service-development-using-tomcat-and-openejb/#comments</comments>
		<pubDate>Sun, 01 Nov 2009 05:42:30 +0000</pubDate>
		<dc:creator>Kevin</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Java Web Application Development]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[@WebService]]></category>
		<category><![CDATA[Apache Tomcat]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[Eclipse Ganymede]]></category>
		<category><![CDATA[Eclipse JEE]]></category>
		<category><![CDATA[EJB]]></category>
		<category><![CDATA[EJB3]]></category>
		<category><![CDATA[Java EE]]></category>
		<category><![CDATA[Java EE 5]]></category>
		<category><![CDATA[JPA]]></category>
		<category><![CDATA[OpenEJB]]></category>
		<category><![CDATA[OpenJPA]]></category>
		<category><![CDATA[Tomcat]]></category>
		<category><![CDATA[Web Service]]></category>
		<category><![CDATA[Web Service Client]]></category>
		<category><![CDATA[Web Service Development]]></category>

		<guid isPermaLink="false">http://blog.lckymn.com/?p=432</guid>
		<description><![CDATA[<p>In addition to the previous post, <a href="http://blog.lckymn.com/2009/10/17/java-ee-application-development-using-tomcat-openejb-and-hibernate/" target="_blank">Java EE Application Development using Tomcat, OpenEJB and Hibernate</a>, this post will demonstrate how to create a web service using Tomcat and OpenEJB. The web service in this blog entry uses the code from <a href="http://blog.lckymn.com/2009/10/17/java-ee-application-development-using-tomcat-openejb-and-hibernate/" target="_blank">the previous post</a>, so if you haven&#8217;t read <a href="http://blog.lckymn.com/2009/10/17/java-ee-application-development-using-tomcat-openejb-and-hibernate/" target="_blank">it</a> yet, better read <a href="http://blog.lckymn.com/2009/10/17/java-ee-application-development-using-tomcat-openejb-and-hibernate/" target="_blank">it</a> first.</p> <p>Firstly, create a web service interface then put the <a href="http://java.sun.com/javase/6/docs/api/javax/jws/WebService.html" target="_blank">@WebService</a> annotation.</p> package com.lckymn.kevin.test.openejb.webservice; import javax.jws.WebService; import <p style="border: 3px solid rgb(243, 197, 52); padding: 5px; background-color: rgb(254, 254, 184); width: 600px; text-align: center;">[...Continue reading <a href="http://blog.lckymn.com/2009/11/01/web-service-development-using-tomcat-and-openejb/">Web Service Development using Tomcat and OpenEJB</a>...]</p>]]></description>
			<content:encoded><![CDATA[<p>In addition to the previous post, <a href="http://blog.lckymn.com/2009/10/17/java-ee-application-development-using-tomcat-openejb-and-hibernate/" target="_blank">Java EE Application Development using Tomcat, OpenEJB and Hibernate</a>, this post will demonstrate how to create a web service using Tomcat and OpenEJB.  The web service in this blog entry uses the code from <a href="http://blog.lckymn.com/2009/10/17/java-ee-application-development-using-tomcat-openejb-and-hibernate/" target="_blank">the previous post</a>, so if you haven&#8217;t read <a href="http://blog.lckymn.com/2009/10/17/java-ee-application-development-using-tomcat-openejb-and-hibernate/" target="_blank">it</a> yet, better read <a href="http://blog.lckymn.com/2009/10/17/java-ee-application-development-using-tomcat-openejb-and-hibernate/" target="_blank">it</a> first.</p>
<p>Firstly, create a web service interface then put the <a href="http://java.sun.com/javase/6/docs/api/javax/jws/WebService.html" target="_blank">@WebService</a> annotation.</p>
<pre class="brush: java; tab-size: 4; title: ; toolbar: true; notranslate">
package com.lckymn.kevin.test.openejb.webservice;

import javax.jws.WebService;

import com.lckymn.kevin.test.openejb.domain.User;

@WebService
public interface TestWebService
{
	User getUser(Long id);
}
</pre>
<p>Secondly, create a class implmenting the web service interface with also the <a href="http://java.sun.com/javase/6/docs/api/javax/jws/WebService.html" target="_blank">@WebService</a> annotation then specify the <code>targetNamespace</code> and the <code>serviceName</code> as you wish. In my case, these are<br />
<code><br />
targetNamespace: "http://webservice.webhibernate.test"<br />
serviceName: "testWebService"<br />
</code><br />
To use the UserService EJB, injected by OpenEJB, make the web service class session EJB with the @Stateless annotation.</p>
<pre class="brush: java; tab-size: 4; title: ; toolbar: true; notranslate">
package com.lckymn.kevin.test.openejb.webservice;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.jws.WebService;

import com.lckymn.kevin.test.openejb.domain.User;
import com.lckymn.kevin.test.openejb.service.UserService;

@Local
@Stateless
@WebService(targetNamespace = &quot;http://webservice.webhibernate.test&quot;, serviceName = &quot;testWebService&quot;)
public class TestWebServiceImpl implements TestWebService
{
	@EJB
	private UserService userService;

	@Override
	public User getUser(Long id)
	{
		return userService.getUser(id);
	}
}
</pre>
<p>The class above simply returns a User entity object acquired from the <code>UserService</code> EJB, made in the previous post.</p>
<p>That&#8217;s it. You have just made your web service. When the server starts and the application is deployed, the EJB container (OpenEJB) registers the above EJB as a web service.</p>
<p>Now, let&#8217;s make a web service client. To make a very simple example client, I&#8217;m going to create a servlet in the same web application to which the web service belongs, yet it can of course be another web application, Java desktop application, Java console application and so on.</p>
<p>Here is a simple servlet which gets the user ID from the client-side then accesses the web service to get the User. After that it passes the User entity object through the <code>HttpSession</code> to the JSP page to display.</p>
<pre class="brush: java; tab-size: 4; title: ; toolbar: true; notranslate">
package com.lckymn.kevin.test.openejb.web;

import java.io.IOException;
import java.net.URL;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import com.lckymn.kevin.test.openejb.domain.User;
import com.lckymn.kevin.test.openejb.webservice.TestWebService;

public class TestWebServiceClientServlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		Service service = Service.create(new URL(&quot;http://localhost:8080/TestWebServiceImpl?wsdl&quot;), new QName(
				&quot;http://webservice.webhibernate.test&quot;, &quot;testWebService&quot;));
		TestWebService testWebService = service.getPort(TestWebService.class);

		String userIdParam = request.getParameter(&quot;userId&quot;);

		HttpSession session = request.getSession();
		if (null == userIdParam || 0 == userIdParam.length())
		{
			session.removeAttribute(&quot;userFound&quot;);
		}
		else
		{
			Long userId = Long.parseLong(userIdParam);
			User user = testWebService.getUser(userId);
			session.setAttribute(&quot;userFound&quot;, user);
		}
		getServletContext().getRequestDispatcher(&quot;/WEB-INF/jsp/webServiceClient.jsp&quot;)
				.forward(request, response);
	}
}
</pre>
<p>This is just a simple example thus I omitted validation (e.g. checking whether the userId is &#8216;long&#8217; type or not) and exception handling. </p>
<p>The WSDL location is the server URI + &#8220;/&#8221; + web service class name + &#8220;?wsdl&#8221;.<br />
<code>http://localhost:8080/TestWebServiceImpl?wsdl</code><br />
The parameters of the <a href="http://java.sun.com/javase/6/docs/api/javax/xml/namespace/QName.html" target="_blank">QName</a> constructor are <code>namespaceURI</code> and <code>localPart</code>, and these are defined in the web service. If you look at the web service code again, these can easily be found from the <a href="http://java.sun.com/javase/6/docs/api/javax/jws/WebService.html" target="_blank">@WebService</a> annotation.</p>
<pre class="brush: java; gutter: false; highlight: [1]; title: ; notranslate">
@WebService(targetNamespace = &quot;http://webservice.webhibernate.test&quot;, serviceName = &quot;testWebService&quot;)
public class TestWebServiceImpl implements TestWebService
{
	...
}
</pre>
<p>The value of the <code>targetNamespace</code> element is the <code>namespaceURI</code> of the <code>QName</code> constructor, and the value of the <code>serviceNmae</code> element is the <code>localPart</code>.</p>
<p>This is web.xml. The information of the new servlet, the web service client just created, is added to the web.xml, made in the previous post.</p>
<pre class="brush: xml; highlight: [14,15,16,17,18,19,20,21,22,23]; tab-size: 4; title: ; toolbar: true; notranslate">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;web-app xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns=&quot;http://java.sun.com/xml/ns/javaee&quot; xmlns:web=&quot;http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot; xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot; id=&quot;WebApp_ID&quot; version=&quot;2.5&quot;&gt;
  &lt;display-name&gt;test-web-hibernate&lt;/display-name&gt;
  &lt;servlet&gt;
    &lt;description&gt;&lt;/description&gt;
    &lt;display-name&gt;TestServlet&lt;/display-name&gt;
    &lt;servlet-name&gt;TestServlet&lt;/servlet-name&gt;
    &lt;servlet-class&gt;com.lckymn.kevin.test.openejb.web.TestServlet&lt;/servlet-class&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
    &lt;servlet-name&gt;TestServlet&lt;/servlet-name&gt;
    &lt;url-pattern&gt;/Test&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  &lt;servlet&gt;
    &lt;description&gt;&lt;/description&gt;
    &lt;display-name&gt;TestWebServiceClientServlet&lt;/display-name&gt;
    &lt;servlet-name&gt;TestWebServiceClientServlet&lt;/servlet-name&gt;
    &lt;servlet-class&gt;com.lckymn.kevin.test.openejb.web.TestWebServiceClientServlet&lt;/servlet-class&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
    &lt;servlet-name&gt;TestWebServiceClientServlet&lt;/servlet-name&gt;
    &lt;url-pattern&gt;/TestWebServiceClient&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  &lt;welcome-file-list&gt;
    &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
  &lt;/welcome-file-list&gt;
&lt;/web-app&gt;
</pre>
<p>Finally, add a JSP file to enter the userId to search a user and to display the user info.<br />
In my case, I added the file to the <code>application/WebContent/WEB-INF/jsp</code> directory which is the same location that I set in the <code>TestWebServiceClientServlet</code> (look at the TestWebServiceClientServlet code above).<br />
<code>test-web-hibernate/WebContent/WEB-INF/jsp/webServiceClient.jsp</code></p>
<pre class="brush: xml; tab-size: 4; title: ; toolbar: true; notranslate">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;
&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=UTF-8&quot;
    pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot; /&gt;
&lt;title&gt;Insert title here&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div&gt;
&lt;form name=&quot;userForm&quot; action=&quot;TestWebServiceClient&quot; method=&quot;post&quot; &gt;
	&lt;input type=&quot;text&quot; name=&quot;userId&quot; value=&quot;&quot; /&gt; &lt;input type=&quot;submit&quot; name=&quot;userIdSubmit&quot; value=&quot;Search&quot; /&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;div&gt;
	&lt;table&gt;
		&lt;tr&gt;
			&lt;td&gt;User ID: &lt;/td&gt;&lt;td&gt;${userFound.id }&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;Username: &lt;/td&gt;&lt;td&gt;${userFound.username }&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;Surname: &lt;/td&gt;&lt;td&gt;${userFound.surname }&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;Given name: &lt;/td&gt;&lt;td&gt;${userFound.givenName }&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;Email: &lt;/td&gt;&lt;td&gt;${userFound.email }&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>To test it, open the browser and enter the following URI.</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">

http://localhost:8080/test-web-hibernate/TestWebServiceClient
</pre>
<p>It displays the screen like below<br />
<div id="attachment_436" class="wp-caption alignnone" style="width: 712px"><a href="http://blog.lckymn.com/wp-content/uploads/2009/11/01_web_service_client_test01.jpg"><img src="http://blog.lckymn.com/wp-content/uploads/2009/11/01_web_service_client_test01.jpg" alt="Enter userId and click the &#039;Search&#039; button" title="Web Service Client Test 01" width="702" height="268" class="size-full wp-image-436" /></a><p class="wp-caption-text">Enter userId and click the 'Search' button</p></div><br />
-Enter a userId to search then click the &#8216;Search&#8217; button.</p>
<p>It displays the result yet there is one problem. It doesn&#8217;t display the userId.<br />
<div id="attachment_437" class="wp-caption alignnone" style="width: 712px"><a href="http://blog.lckymn.com/wp-content/uploads/2009/11/01_web_service_client_test02.jpg"><img src="http://blog.lckymn.com/wp-content/uploads/2009/11/01_web_service_client_test02.jpg" alt="The Search Result: userId is not displayed" title="Web Service Client Test 02" width="702" height="268" class="size-full wp-image-437" /></a><p class="wp-caption-text">The Search Result: userId is not displayed</p></div><br />
This is because the id field of the User entity class that I created in the previous blog entry does not have the mutator that is the setId() method so when the entity object is passed through the web service, the id field is not included.</p>
<p>There are two simple ways to solve this problem. Either way works so choose whichever you like.</p>
<p>1. Add the setter method. If there is setId() method, the id field is included when the object is passed through the web service. The reason why I did not write the setter method is that the id is supposed to be set by Hibernate and to avoid any problems caused by setting it manually, I did not write it. However, it is required in order to include the field when passing object through the web service. So adding setter can solve this problem. If you do not like this solution as you do not like to put the setter method due to the reason I explained, you can try the second solution.</p>
<pre class="brush: java; highlight: [19,20,21,22]; tab-size: 4; title: ; toolbar: true; notranslate">
@Entity
@Table(name = &quot;users&quot;)
public class User implements Serializable
{
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = &quot;user_id&quot;)
	private Long id;

	...

	public Long getId()
	{
		return id;
	}

	public void setId(Long id)
	{
		this.id = id;
	}

	...
}
</pre>
<p>OR</p>
<p>2. If you add the <a href="http://java.sun.com/javase/6/docs/api/javax/xml/bind/annotation/XmlElement.html" target="_blank">@XmlElement</a> annotation to the id field and set the value of the <code>required</code> element to <code>true</code>, the id field is included even without the setter method.</p>
<pre class="brush: java; highlight: [15]; tab-size: 4; title: ; toolbar: true; notranslate">
...

import javax.xml.bind.annotation.XmlElement;

...
@Entity
@Table(name = &quot;users&quot;)
public class User implements Serializable
{
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = &quot;user_id&quot;)
	@XmlElement(required = true)
	private Long id;

	...

	public Long getId()
	{
		return id;
	}

	// No setId() required

	...
}
</pre>
<p>Now, test if it works.<br />
<div id="attachment_436" class="wp-caption alignnone" style="width: 712px"><a href="http://blog.lckymn.com/wp-content/uploads/2009/11/01_web_service_client_test01.jpg"><img src="http://blog.lckymn.com/wp-content/uploads/2009/11/01_web_service_client_test01.jpg" alt="Enter userId and click the &#039;Search&#039; button" title="Web Service Client Test 01" width="702" height="268" class="size-full wp-image-436" /></a><p class="wp-caption-text">Enter userId and click the 'Search' button</p></div></p>
<p>It works!<br />
<div id="attachment_438" class="wp-caption alignnone" style="width: 712px"><a href="http://blog.lckymn.com/wp-content/uploads/2009/11/01_web_service_client_test03.jpg"><img src="http://blog.lckymn.com/wp-content/uploads/2009/11/01_web_service_client_test03.jpg" alt="The Search Result: userId is displayed correctly" title="Web Service Client Test 03" width="702" height="268" class="size-full wp-image-438" /></a><p class="wp-caption-text">The Search Result: userId is displayed correctly</p></div></p>
<p>There are also other ways and tools to create a web service in Java. I used to use <a href="http://ws.apache.org/axis/" target="_blank">Apache Axis</a> then later moved to <a href="http://xfire.codehaus.org/" target="_blank">XFire</a>. Now, I use <a href="http://cxf.apache.org/" target="_blank">Apache CXF</a> which is considered as XFire 2.0.  It is, as explained here, very easy to create a web service client, yet it can be even easier with <a href="http://ws.apache.org/axis/java/user-guide.html#WSDL2JavaBuildingStubsSkeletonsAndDataTypesFromWSDL" target="_blank">WSDL2Java</a> from <a href="http://ws.apache.org/axis/" target="_blank">Apache Axis</a> or <a href="http://ws.apache.org/axis2/1_3/userguide-creatingclients.html" target="_blank">WSDL2Java</a> from <a href="http://ws.apache.org/axis2/" target="_blank">Apache Axis2</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.lckymn.com/2009/11/01/web-service-development-using-tomcat-and-openejb/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

