<?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>Wet Feet - Online Marketing and Technology Blog &#187; Spring Framework</title>
	<atom:link href="http://www.wetfeetblog.com/tag/spring/feed" rel="self" type="application/rss+xml" />
	<link>http://www.wetfeetblog.com</link>
	<description></description>
	<lastBuildDate>Tue, 04 Oct 2011 17:21:49 +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>Spring JDBC Template &#8211; A leaner alternative to fat Hibernate</title>
		<link>http://www.wetfeetblog.com/spring-jdbc-template-leaner-alternative-fat-hibernate/328</link>
		<comments>http://www.wetfeetblog.com/spring-jdbc-template-leaner-alternative-fat-hibernate/328#comments</comments>
		<pubDate>Tue, 23 Feb 2010 14:00:12 +0000</pubDate>
		<dc:creator>Tomas Mazukna</dc:creator>
				<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[jdbc]]></category>
		<category><![CDATA[JDO]]></category>

		<guid isPermaLink="false">http://www.wetfeetblog.com/?p=328</guid>
		<description><![CDATA[Objective This post will show you how to use Spring JDBC Template to perform real world CRUD operations. I hope I can show you that you do not need overweight Hibernate to interact with database of your choice. The Problem Domain Almost all application I have coded needed to store and get data of some [...]<p><a href="http://www.wetfeetblog.com/spring-jdbc-template-leaner-alternative-fat-hibernate/328">Spring JDBC Template &#8211; A leaner alternative to fat Hibernate</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.wetfeetblog.com/wp-content/uploads/2010/02/network_server_2.jpg" alt="" title="network_server_2" width="300" height="225" class="float-right" /></p>
<h3>Objective</h3>
<p>This post will show you how to use Spring JDBC Template to perform real world CRUD operations. I hope I can show you that you do not need overweight Hibernate to interact with database of your choice.</p>
<h3>The Problem Domain</h3>
<p>Almost all application I have coded needed to store and get data of some sort. In most cases it was relational database. With hibernate being all the rage these days I have used it in multiple projects with varying degree of problems and success. In <strong>my personal</strong> opinion hibernate tries to solve a problem that really is not there. If you can not wire SQL you should not be using Hibernate or be doing any software development.<br />
<span id="more-328"></span><br />
I advise the use of Spring JDBC templates. In essence Spring JDBC Template will offload all the mundane steps of database access from you shoulders only requiring you to write SQL and move data from result set into your model objects. Here is how Spring documentation describes responsibilities:</p>
<table summary="Spring JDBC - who does what?" style="border-collapse: collapse;border-top: 0.5pt solid ; border-bottom: 0.5pt solid ; border-left: 0.5pt solid ; border-right: 0.5pt solid ; " width="80%">
<colgroup>
<col>
<col>
<col></colgroup>
<thead>
<tr>
<th style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="center">Action</th>
<th style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="center">Spring</th>
<th style="border-bottom: 0.5pt solid ; " align="center">You</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="left">Define connection parameters.</td>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="center"><span class="bold"><strong></strong></span></td>
<td style="border-bottom: 0.5pt solid ; " align="center">X</td>
</tr>
<tr>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="left">Open the connection.</td>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="center">X</td>
<td style="border-bottom: 0.5pt solid ; " align="center">&nbsp;</td>
</tr>
<tr>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="left">Specify the SQL statement.</td>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="center">&nbsp;</td>
<td style="border-bottom: 0.5pt solid ; " align="center">X</td>
</tr>
<tr>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="left">Declare parameters and provide parameter values</td>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="center">&nbsp;</td>
<td style="border-bottom: 0.5pt solid ; " align="center">X</td>
</tr>
<tr>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="left">Prepare and execute the statement.</td>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="center">X</td>
<td style="border-bottom: 0.5pt solid ; " align="center">&nbsp;</td>
</tr>
<tr>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="left">Set up the loop to iterate through the results (if any).</td>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="center">X</td>
<td style="border-bottom: 0.5pt solid ; " align="center">&nbsp;</td>
</tr>
<tr>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="left">Do the work for each iteration.</td>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="center">&nbsp;</td>
<td style="border-bottom: 0.5pt solid ; " align="center">X</td>
</tr>
<tr>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="left">Process any exception.</td>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="center">X</td>
<td style="border-bottom: 0.5pt solid ; " align="center">&nbsp;</td>
</tr>
<tr>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="left">Handle transactions.</td>
<td style="border-right: 0.5pt solid ; border-bottom: 0.5pt solid ; " align="center">X</td>
<td style="border-bottom: 0.5pt solid ; " align="center">&nbsp;</td>
</tr>
<tr>
<td style="border-right: 0.5pt solid ; " align="left">Close the connection, statement and resultset.</td>
<td style="border-right: 0.5pt solid ; " align="center">X</td>
<td style="" align="center">&nbsp;</td>
</tr>
</tbody>
</table>
<p><br/></p>
<h3>CRUD Example with Spring JDBC Template</h3>
<p>For this example you will need spring-core, spring-tx and spring-aop components. We will use spring-tx to manage the transaction so we can get back the auto generated IDs of the persisted objects and aop to set up the transaction management. Here is meat for spring configuration file:</p>
<pre>
<div class="codesnip-container" >
<div class="xml codesnip" style="font-family:monospace;"><span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">id</span>=<span class="st0">&quot;propertyPlaceholder&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp;<span class="re0">class</span>=<span class="st0">&quot;org.springframework.beans.factory.config.PropertyPlaceholderConfigurer&quot;</span> <span class="re2">&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;location&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;/WEB-INF/config.properties&quot;</span> <span class="re2">/&gt;</span></span>
<span class="sc3"><span class="re1">&lt;/bean<span class="re2">&gt;</span></span></span>

<span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">id</span>=<span class="st0">&quot;mysqlDataSource&quot;</span> <span class="re0">class</span>=<span class="st0">&quot;com.mchange.v2.c3p0.ComboPooledDataSource&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp;<span class="re0">destroy-method</span>=<span class="st0">&quot;close&quot;</span><span class="re2">&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;driverClass&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;com.mysql.jdbc.Driver&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;jdbcUrl&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;${database.url}&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;user&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;${database.user}&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;password&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;${database.password}&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;initialPoolSize&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;${database.connections.start}&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;maxPoolSize&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;${database.connections.max}&quot;</span><span class="re2">/&gt;</span></span> &nbsp;
&nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;idleConnectionTestPeriod&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;270&quot;</span> <span class="re2">/&gt;</span></span>
<span class="sc3"><span class="re1">&lt;/bean<span class="re2">&gt;</span></span></span>

<span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">id</span>=<span class="st0">&quot;txManager&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp;<span class="re0">class</span>=<span class="st0">&quot;org.springframework.jdbc.datasource.DataSourceTransactionManager&quot;</span> <span class="re2">&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;dataSource&quot;</span> <span class="re0">ref</span>=<span class="st0">&quot;mysqlDataSource&quot;</span> <span class="re2">/&gt;</span></span>
<span class="sc3"><span class="re1">&lt;/bean<span class="re2">&gt;</span></span></span>

<span class="sc3"><span class="re1">&lt;tx:advice</span> <span class="re0">id</span>=<span class="st0">&quot;txAdvice&quot;</span> <span class="re0">transaction-manager</span>=<span class="st0">&quot;txManager&quot;</span><span class="re2">&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;tx:attributes<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; <span class="sc-1">&lt;!-- all methods starting with 'get' are read-only --&gt;</span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;tx:method</span> <span class="re0">name</span>=<span class="st0">&quot;get*&quot;</span> <span class="re0">read-only</span>=<span class="st0">&quot;true&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;tx:method</span> <span class="re0">name</span>=<span class="st0">&quot;list*&quot;</span> <span class="re0">read-only</span>=<span class="st0">&quot;true&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc-1">&lt;!-- other methods use the default transaction settings (see below) --&gt;</span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;tx:method</span> <span class="re0">name</span>=<span class="st0">&quot;*&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/tx:attributes<span class="re2">&gt;</span></span></span>
<span class="sc3"><span class="re1">&lt;/tx:advice<span class="re2">&gt;</span></span></span>

<span class="sc3"><span class="re1">&lt;aop:config<span class="re2">&gt;</span></span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;aop:pointcut</span> <span class="re0">id</span>=<span class="st0">&quot;uOwnerDataSvcOperation&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp;<span class="re0">expression</span>=<span class="st0">&quot;execution(* com.es.rto.dao.UOwnerDataServiceImpl.*(..))&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;aop:advisor</span> <span class="re0">advice-ref</span>=<span class="st0">&quot;txAdvice&quot;</span> <span class="re0">pointcut-ref</span>=<span class="st0">&quot;uOwnerDataSvcOperation&quot;</span><span class="re2">/&gt;</span></span>
<span class="sc3"><span class="re1">&lt;/aop:config<span class="re2">&gt;</span></span></span>

<span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">id</span>=<span class="st0">&quot;namedTemplate&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp;<span class="re0">class</span>=<span class="st0">&quot;org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate&quot;</span><span class="re2">&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;constructor-arg</span> <span class="re0">index</span>=<span class="st0">&quot;0&quot;</span> <span class="re0">ref</span>=<span class="st0">&quot;mysqlDataSource&quot;</span><span class="re2">/&gt;</span></span> &nbsp; 
<span class="sc3"><span class="re1">&lt;/bean<span class="re2">&gt;</span></span></span>

<span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">id</span>=<span class="st0">&quot;qandADao&quot;</span> <span class="re0">class</span>=<span class="st0">&quot;com.es.rto.dao.QandADao&quot;</span> <span class="re2">&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;jdbcTemplate&quot;</span> <span class="re0">ref</span>=<span class="st0">&quot;namedTemplate&quot;</span> <span class="re2">/&gt;</span></span>
<span class="sc3"><span class="re1">&lt;/bean<span class="re2">&gt;</span></span></span>

<span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">id</span>=<span class="st0">&quot;uOwnerDataSvc&quot;</span> <span class="re0">class</span>=<span class="st0">&quot;com.es.rto.dao.UOwnerDataServiceImpl&quot;</span> <span class="re2">&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;qandADao&quot;</span> <span class="re0">ref</span>=<span class="st0">&quot;qandADao&quot;</span> <span class="re2">/&gt;</span></span>
<span class="sc3"><span class="re1">&lt;/bean<span class="re2">&gt;</span></span></span></div>
</div>
</pre>
<p>First thing we do we load our config.properties file where we have database configuration defined. Second we define a pooled data source to connect to mysql database. Third bean is a transaction manager that is watching our data source from the step two. Next we set up our transaction manager. We instruct transaction manager that methods starting with &#8220;get&#8221; and &#8220;list&#8221; are read only. This means that they will not be subject to transaction management. Then we define named jdbc template and pass in the data source as first constructor argument and then pass in the template into our own DAO bean.</p>
<h3>Why do we need Transaction Management?</h3>
<p>When we create a new row in a mysql table with autoincrement id value we need a way to get that auto generated id. To do so we need to query the database using same connection. When we use jdbcTemplate.update() or jdbcTemplate.query() it will check out a connection form the pool execute the query, process the results and return this connection to the pool. So if we do update() and then get() to query &#8220;SELECT LAST_INSERT_ID()&#8221; we will get another connection, which is a problem since we need to do this on the same connection to get the right id. This is where the transaction comes in. Instead of releasing connection back to the pool spring will hold on to it until we are done with it.</p>
<h3>Java code side of the Spring JDBC template</h3>
<p> We will use 3 beans to illustrate the scenario: question, answer and user.</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">public</span> <span class="kw1">class</span> Question <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">long</span> questionId<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">long</span> categoryId<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">long</span> userId<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">long</span> areaId<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> question<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> verifyKey<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adate+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Date</span></a> created<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> User user<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> List<span class="sy0">&lt;</span>Answer<span class="sy0">&gt;</span> answers<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">long</span> answerCount<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> name<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// getters and setters omited...</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">public</span> <span class="kw1">class</span> Answer <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">long</span> answerId<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">long</span> questionId<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">long</span> userId<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> answer<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> verifyKey<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> url<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adate+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Date</span></a> created<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> User user<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> name<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// getters and setters omited...</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">public</span> <span class="kw1">class</span> User <span class="kw1">implements</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aserializable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Serializable</span></a> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">long</span> id<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> email<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> retypeE<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> name<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> password<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> password2<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> verifyKey<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">boolean</span> buyer<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">boolean</span> seller<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">boolean</span> active<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">boolean</span> privacy<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">int</span> pageSize <span class="sy0">=</span> <span class="nu0">20</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> next<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">boolean</span> administrator<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// getters and setters omited...</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>Here are the 2 classes that do all the work:</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">public</span> <span class="kw1">class</span> QandADao <span class="br0">&#123;</span>

&nbsp; <span class="kw1">private</span> <span class="kw1">final</span> Logger logger <span class="sy0">=</span> Logger.<span class="me1">getLogger</span><span class="br0">&#40;</span>QandADao.<span class="kw1">class</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; <span class="kw1">protected</span> NamedParameterJdbcTemplate jdbcTemplate <span class="sy0">=</span> <span class="kw2">null</span><span class="sy0">;</span>
&nbsp; 

&nbsp; @SuppressWarnings<span class="br0">&#40;</span><span class="st0">&quot;unchecked&quot;</span><span class="br0">&#41;</span>
&nbsp; <span class="kw1">public</span> List<span class="sy0">&lt;</span>Question<span class="sy0">&gt;</span> getQuestions<span class="br0">&#40;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> search, <span class="kw4">int</span> page, <span class="kw4">int</span> step <span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; <span class="kw1">try</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; MapSqlParameterSource params <span class="sy0">=</span> <span class="kw1">new</span> MapSqlParameterSource<span class="br0">&#40;</span> <span class="st0">&quot;search&quot;</span>, <span class="st0">&quot;%&quot;</span><span class="sy0">+</span>search<span class="sy0">+</span><span class="st0">&quot;%&quot;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> sql <span class="sy0">=</span> <span class="st0">&quot;SELECT q.*, count( a.answer_id ) as answers, u.name, u.seller &nbsp;FROM users u, questions q LEFT JOIN answers a &quot;</span><span class="sy0">+</span>
&nbsp; &nbsp; &nbsp; <span class="st0">&quot;ON &nbsp;a.question_id = q.question_id WHERE a.answer LIKE :search AND q.user_id = u.id &quot;</span><span class="sy0">+</span>
&nbsp; &nbsp; &nbsp; <span class="st0">&quot;GROUP BY q.question_id, q.category_id, q.user_id, q.question, q.created, q.area_id, u.name, u.seller &quot;</span><span class="sy0">+</span> &nbsp;
&nbsp; &nbsp; &nbsp; <span class="st0">&quot;UNION &quot;</span><span class="sy0">+</span>
&nbsp; &nbsp; &nbsp; <span class="st0">&quot;SELECT q.*, count( a.answer_id ) as answers, u.name, u.seller &nbsp;FROM users u, questions q LEFT JOIN answers a &quot;</span><span class="sy0">+</span> 
&nbsp; &nbsp; &nbsp; <span class="st0">&quot;ON &nbsp;a.question_id = q.question_id WHERE q.question LIKE :search AND q.user_id = u.id &quot;</span><span class="sy0">+</span>
&nbsp; &nbsp; &nbsp; <span class="st0">&quot;GROUP BY q.question_id, q.category_id, q.user_id, q.question, q.created, q.area_id, u.name, u.seller &quot;</span><span class="sy0">+</span>
&nbsp; &nbsp; &nbsp; <span class="st0">&quot;LIMIT &quot;</span><span class="sy0">+</span><span class="br0">&#40;</span>step<span class="sy0">*</span><span class="br0">&#40;</span>page<span class="sy0">-</span><span class="nu0">1</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">+</span><span class="st0">&quot;, &quot;</span><span class="sy0">+</span>step<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; logger.<span class="me1">debug</span><span class="br0">&#40;</span> sql <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; <span class="kw1">return</span> jdbcTemplate.<span class="me1">query</span><span class="br0">&#40;</span>sql, params, <span class="kw1">new</span> RowMappers.<span class="me1">QuestionsMapper</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">catch</span> <span class="br0">&#40;</span> DataAccessException exc <span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; logger.<span class="me1">error</span><span class="br0">&#40;</span><span class="st0">&quot;FAILED to get Question List&quot;</span>, exc<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw1">new</span> ArrayList<span class="sy0">&lt;</span>Question<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; <span class="br0">&#125;</span>

&nbsp; <span class="kw1">public</span> Question saveQuestion<span class="br0">&#40;</span> Question question <span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; <span class="kw1">try</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; BeanPropertySqlParameterSource namedParameters <span class="sy0">=</span> <span class="kw1">new</span> BeanPropertySqlParameterSource<span class="br0">&#40;</span>question<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> sql<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span> question.<span class="me1">getQuestionId</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">==</span> <span class="nu0">0</span> <span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; sql <span class="sy0">=</span> <span class="st0">&quot;INSERT INTO questions ( category_id, user_id, question, area_id, uname ) VALUES ( :categoryId, :userId, :question, :areaId, :name )&quot;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; <span class="kw1">else</span>
&nbsp; &nbsp; &nbsp; &nbsp; sql <span class="sy0">=</span> <span class="st0">&quot;UPDATE questions SET question=:question, category_id=:categoryId, area_id=:areaId WHERE question_id=:questionId&quot;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; logger.<span class="me1">debug</span><span class="br0">&#40;</span> sql <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; jdbcTemplate.<span class="me1">update</span><span class="br0">&#40;</span>sql, namedParameters<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span> question.<span class="me1">getQuestionId</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">==</span> 0 <span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; question.<span class="me1">setQuestionId</span><span class="br0">&#40;</span> <span class="kw1">this</span>.<span class="me1">getInsertedID</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">catch</span> <span class="br0">&#40;</span> DataAccessException exc <span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; logger.<span class="me1">error</span><span class="br0">&#40;</span><span class="st0">&quot;SAVE answer FAILED!&quot;</span>, exc<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; <span class="kw1">throw</span> exc<span class="sy0">;</span>
&nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; <span class="kw1">return</span> question<span class="sy0">;</span>
&nbsp; <span class="br0">&#125;</span>

&nbsp; <span class="kw1">protected</span> <span class="kw4">long</span> getInsertedID<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; <span class="kw1">return</span> jdbcTemplate.<span class="me1">queryForLong</span><span class="br0">&#40;</span> <span class="st0">&quot;SELECT LAST_INSERT_ID()&quot;</span> , <span class="kw1">new</span> MapSqlParameterSource<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; <span class="br0">&#125;</span>
&nbsp; 
<span class="br0">&#125;</span></div>
</div>
</pre>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">public</span> <span class="kw1">class</span> RowMappers <span class="br0">&#123;</span>

&nbsp; <span class="kw1">public</span> <span class="kw1">static</span> <span class="kw1">final</span> <span class="kw1">class</span> QuestionMapper <span class="kw1">implements</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Arowmapper+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">RowMapper</span></a> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Object</span></a> mapRow<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aresultset+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">ResultSet</span></a> rs, <span class="kw4">int</span> rowNum<span class="br0">&#41;</span> <span class="kw1">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asqlexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">SQLException</span></a> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; Question q <span class="sy0">=</span> <span class="kw1">new</span> Question<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; q.<span class="me1">setQuestionId</span><span class="br0">&#40;</span>rs.<span class="me1">getLong</span><span class="br0">&#40;</span> <span class="st0">&quot;question_id&quot;</span> <span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; q.<span class="me1">setCategoryId</span><span class="br0">&#40;</span>rs.<span class="me1">getLong</span><span class="br0">&#40;</span> <span class="st0">&quot;category_id&quot;</span> <span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; q.<span class="me1">setUserId</span><span class="br0">&#40;</span>rs.<span class="me1">getLong</span><span class="br0">&#40;</span> <span class="st0">&quot;user_id&quot;</span> <span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; q.<span class="me1">setAreaId</span><span class="br0">&#40;</span> rs.<span class="me1">getLong</span><span class="br0">&#40;</span><span class="st0">&quot;area_id&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; q.<span class="me1">setQuestion</span><span class="br0">&#40;</span> rs.<span class="me1">getString</span><span class="br0">&#40;</span><span class="st0">&quot;question&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; q.<span class="me1">setCreated</span><span class="br0">&#40;</span> rs.<span class="me1">getTimestamp</span><span class="br0">&#40;</span> <span class="st0">&quot;created&quot;</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; q.<span class="me1">setName</span><span class="br0">&#40;</span> rs.<span class="me1">getString</span><span class="br0">&#40;</span><span class="st0">&quot;uname&quot;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; User u <span class="sy0">=</span> <span class="kw1">new</span> User<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; u.<span class="me1">setName</span><span class="br0">&#40;</span> rs.<span class="me1">getString</span><span class="br0">&#40;</span><span class="st0">&quot;name&quot;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; u.<span class="me1">setSeller</span><span class="br0">&#40;</span> rs.<span class="me1">getBoolean</span><span class="br0">&#40;</span><span class="st0">&quot;seller&quot;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; u.<span class="me1">setId</span><span class="br0">&#40;</span>q.<span class="me1">getUserId</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; q.<span class="me1">setUser</span><span class="br0">&#40;</span>u<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; <span class="kw1">return</span> q<span class="sy0">;</span>
&nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; <span class="br0">&#125;</span> 
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>So what is going on here? Lets look at the <strong>QuestionMapper</strong>. It implements one method &#8211; mapRow. Its hob is to process a single row of the result set. As you see here we create a Question bean and a User bean which is included in the Questions bean. It can not be more strait forward then this.</p>
<p><strong>QandADao</strong> is the class where all the magic happens. First lets examine the getQuestions() method. First we define query parameter(s) using MapSqlParameterSource class. We simply put the values in the map and jdbc template will pull them out by the key. Next we write our sql query. In this case iti is pretty complex to pull out any questions where either question or the answer have the thing we are searching for. We use union to be able to pull out  the questions without the answers. Lastly we call query method on the jdbc template and we pass in our query, parameters and the row mapper instance. Spring takes care of all the plumbing. What we get out of this call is a list of Question objects.</p>
<p>saveQuestion() method is not much different. In this case we use BeanPropertySqlParameterSource which maps bean properties to sql parameters. If you define a sql parameter &#8220;name&#8221; your bean should have &#8220;getName()&#8221; method. Instead of calling query() we call update() on the jdbc template which only takes sql and the parameters.</p>
<h3>Conclusion</h3>
<p>You may have noticed that the effort to use the Spring approach to JDCB is very simple, flexible and powerful. You have all the control in all the right places! You write the sql with all the performance hints and tweaks, you write code how to transfer results to the domain objects the way you need it or like it. I only mentioned about one tenth of the options you have with spring. <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/jdbc.html">Read The Fine Manual (RTFM)</a> to explore the full potential.</p>
<p><a href="http://www.wetfeetblog.com/spring-jdbc-template-leaner-alternative-fat-hibernate/328">Spring JDBC Template &#8211; A leaner alternative to fat Hibernate</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.wetfeetblog.com/spring-jdbc-template-leaner-alternative-fat-hibernate/328/feed</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Google App Engine + JAVA + JDO + Inheritance + One-To-Many Relationships</title>
		<link>http://www.wetfeetblog.com/google-app-engine-java-jdo-inheritance-one-to-many-relationships/242</link>
		<comments>http://www.wetfeetblog.com/google-app-engine-java-jdo-inheritance-one-to-many-relationships/242#comments</comments>
		<pubDate>Wed, 13 Jan 2010 15:30:12 +0000</pubDate>
		<dc:creator>Tomas Mazukna</dc:creator>
				<category><![CDATA[Google App Engine]]></category>
		<category><![CDATA[Spring Framework]]></category>

		<guid isPermaLink="false">http://www.wetfeetblog.com/?p=242</guid>
		<description><![CDATA[Objective This article will show you how to persist real world data in Google App Engine. You will learn how to handle inheritance and one-to-many owned relationships in your model objects and store them correctly in the big table implementation of JDO. The Problem Domain In my application I need to segregate data by the [...]<p><a href="http://www.wetfeetblog.com/google-app-engine-java-jdo-inheritance-one-to-many-relationships/242">Google App Engine + JAVA + JDO + Inheritance + One-To-Many Relationships</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p><img class="float-right" title="tin_can" src="http://www.wetfeetblog.com/wp-content/uploads/2010/01/tin_can.jpg" alt="" width="152" height="180" /></p>
<h3>Objective</h3>
<p>This article will show you how to persist real world data in Google App Engine. You will learn how to handle inheritance and one-to-many owned relationships in your model objects and store them correctly in the big table implementation of JDO.</p>
<h3>The Problem Domain</h3>
<p>In my application I need to segregate data by the customer this data belongs to. We will use inheritance in model objects, so all of them will have customer id defined in the base class. We will make the JDO save base class properties in the sub class &#8220;table&#8221;. We will also learn how to instruct Google App Engine JDO to handle customers with multiple addresses and phone numbers for us. If we define the relationship between model objects correctly JDO will manage storing/retrieving/deleting of these lined objects without our involvement.</p>
<p><span id="more-242"></span></p>
<h3>Inheritance in Model Classes</h3>
<p>First let&#8217;s define a superclass for all our model classes:</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">package</span> <span class="co2">com.yourcorp.here</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">java.io.Serializable</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.IdGeneratorStrategy</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.IdentityType</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.Inheritance</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.InheritanceStrategy</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.PersistenceCapable</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.Persistent</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.PrimaryKey</span><span class="sy0">;</span>

@PersistenceCapable<span class="br0">&#40;</span>identityType <span class="sy0">=</span> IdentityType.<span class="me1">APPLICATION</span>, detachable<span class="sy0">=</span><span class="st0">&quot;true&quot;</span><span class="br0">&#41;</span>
@Inheritance<span class="br0">&#40;</span>strategy<span class="sy0">=</span>InheritanceStrategy.<span class="me1">SUBCLASS_TABLE</span><span class="br0">&#41;</span>
<span class="kw1">public</span> <span class="kw1">class</span> ModelBase <span class="kw1">implements</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aserializable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Serializable</span></a><span class="br0">&#123;</span>

&nbsp; &nbsp; @PrimaryKey
&nbsp; &nbsp; @Persistent<span class="br0">&#40;</span>valueStrategy <span class="sy0">=</span> IdGeneratorStrategy.<span class="me1">IDENTITY</span><span class="br0">&#41;</span>
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Along+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Long</span></a> id<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">protected</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Along+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Long</span></a> customerId<span class="sy0">;</span>

&nbsp; &nbsp; <span class="kw1">private</span> <span class="kw1">static</span> <span class="kw1">final</span> <span class="kw4">long</span> serialVersionUID <span class="sy0">=</span> 3039288063578312662L<span class="sy0">;</span>

&nbsp; &nbsp; <span class="co1">// getters &amp;amp; setters go here</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>What we done here is defined  Model Base class that has two properties &#8211; ID and customerID and marked them as persistent. We also annotated that our class is persistence capable and defined Inheritance strategy. In this case we chose to use Subclass Table. What this means is that ID and customerID properties will be saved in the table of the class that inherits this one. You can read more about <a href="http://www.jpox.org/docs/1_2/jdo_orm/inheritance.html">inheritance strategies here</a>.</p>
<p>Now lets define our User class:</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">package</span> <span class="co2">com.yourcorp.here</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">java.io.Serializable</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">java.util.ArrayList</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">java.util.List</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.IdentityType</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.PersistenceCapable</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.Persistent</span><span class="sy0">;</span>

@PersistenceCapable<span class="br0">&#40;</span>identityType <span class="sy0">=</span> IdentityType.<span class="me1">APPLICATION</span>, detachable<span class="sy0">=</span><span class="st0">&quot;true&quot;</span><span class="br0">&#41;</span>
<span class="kw1">public</span> <span class="kw1">class</span> User <span class="kw1">extends</span> ModelBase <span class="kw1">implements</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aserializable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Serializable</span></a> <span class="br0">&#123;</span>

&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> email<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> name<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> password<span class="sy0">;</span>

&nbsp; &nbsp; <span class="kw1">private</span> <span class="kw1">static</span> <span class="kw1">final</span> <span class="kw4">long</span> serialVersionUID <span class="sy0">=</span> 3039288063578312662L<span class="sy0">;</span>

&nbsp; &nbsp; <span class="co1">// getters &amp;amp; setters go here</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>When you persist an instance of this User class in the big table it will have id, customerId, email, name and password properties in the single user &#8220;table&#8221;.</p>
<h3>One-to-many relationships in Google App Engine JDO</h3>
<p>For this example lets define a customer class that has a list on phone and address objects:</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">package</span> <span class="co2">com.yourcorp.here</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">java.util.ArrayList</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">java.util.List</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.IdentityType</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.PersistenceCapable</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.Persistent</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.Element</span><span class="sy0">;</span>

@PersistenceCapable<span class="br0">&#40;</span>identityType <span class="sy0">=</span> IdentityType.<span class="me1">APPLICATION</span>, detachable<span class="sy0">=</span><span class="st0">&quot;true&quot;</span><span class="br0">&#41;</span>
<span class="kw1">public</span> <span class="kw1">class</span> Customer <span class="kw1">extends</span> ModelBase <span class="br0">&#123;</span>

&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> name<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> contactName<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> comments<span class="sy0">;</span>

&nbsp; &nbsp; @Persistent<span class="br0">&#40;</span>mappedBy <span class="sy0">=</span> <span class="st0">&quot;customer&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; @<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aelement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Element</span></a><span class="br0">&#40;</span>dependent <span class="sy0">=</span> <span class="st0">&quot;true&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; <span class="kw1">private</span> List<span class="sy0">&lt;</span>Address<span class="sy0">&gt;</span> adresses <span class="sy0">=</span> <span class="kw1">new</span> ArrayList<span class="sy0">&lt;</span>Address<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>

&nbsp; &nbsp; @Persistent<span class="br0">&#40;</span>mappedBy <span class="sy0">=</span> <span class="st0">&quot;customer&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; @<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aelement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Element</span></a><span class="br0">&#40;</span>dependent <span class="sy0">=</span> <span class="st0">&quot;true&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alist+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">List</span></a> phones <span class="sy0">=</span> <span class="kw1">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aarraylist+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">ArrayList</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>

&nbsp; &nbsp; <span class="kw1">private</span> <span class="kw1">static</span> <span class="kw1">final</span> <span class="kw4">long</span> serialVersionUID <span class="sy0">=</span> 3039288063578312662L<span class="sy0">;</span>

&nbsp; &nbsp; <span class="co1">// getters &amp;amp; setters go here</span>
<span class="br0">&#125;</span></div>
</div>
</address>
</pre>
<p>As you can tell we have defined two lists and annotated them to be persistent. <strong>(mappedBy = &#8220;customer&#8221;)</strong> defines this relationship as &#8220;owned&#8221; &#8211; meaning the customer property of the Address and Phone object will hold the reference tho this customer object.<br />
And here is an example of the address object:</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">package</span> <span class="co2">com.yourcorp.here</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.IdentityType</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.PersistenceCapable</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.Persistent</span><span class="sy0">;</span>

@PersistenceCapable<span class="br0">&#40;</span>identityType <span class="sy0">=</span> IdentityType.<span class="me1">APPLICATION</span>, detachable<span class="sy0">=</span><span class="st0">&quot;true&quot;</span><span class="br0">&#41;</span>
<span class="kw1">public</span> <span class="kw1">class</span> Address <span class="kw1">extends</span> EntityBase <span class="br0">&#123;</span>

&nbsp; &nbsp; @PrimaryKey
&nbsp; &nbsp; @Persistent<span class="br0">&#40;</span>valueStrategy <span class="sy0">=</span> IdGeneratorStrategy.<span class="me1">IDENTITY</span><span class="br0">&#41;</span>
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Akey+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Key</span></a> id<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> type<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> name<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> line1<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> line2<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> city<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> state<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> zip<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> Customer customer<span class="sy0">;</span>

&nbsp; &nbsp; <span class="kw1">private</span> <span class="kw1">static</span> <span class="kw1">final</span> <span class="kw4">long</span> serialVersionUID <span class="sy0">=</span> 3039288063578312662L<span class="sy0">;</span>

&nbsp; &nbsp; <span class="co1">// getters &amp;amp; setters go here</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>You may also notice <strong>@Element(dependent = &#8220;true&#8221;)</strong> which means that when the customer object is deleted JDO will delete these child objects for you automatically.</p>
<h3>Conclusion</h3>
<p>JDO simplifies your life when you need to store and retrieve data if you know how to define all these relationships correctly. Compare the effort required to store and retrieve the same object from your favorite relational database, without using the monster called hibernate&#8230;&#8230;
<div class="codesnip-container" ></div>
<p><a href="http://www.wetfeetblog.com/google-app-engine-java-jdo-inheritance-one-to-many-relationships/242">Google App Engine + JAVA + JDO + Inheritance + One-To-Many Relationships</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.wetfeetblog.com/google-app-engine-java-jdo-inheritance-one-to-many-relationships/242/feed</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Google App Engine + JAVA + Spring + REST + Mapping Exceptions to Errors for JSON</title>
		<link>http://www.wetfeetblog.com/google-app-engine-java-spring-rest-mapping-exceptions-to-errors-for-json/212</link>
		<comments>http://www.wetfeetblog.com/google-app-engine-java-spring-rest-mapping-exceptions-to-errors-for-json/212#comments</comments>
		<pubDate>Wed, 16 Dec 2009 13:55:25 +0000</pubDate>
		<dc:creator>Tomas Mazukna</dc:creator>
				<category><![CDATA[Google App Engine]]></category>
		<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[json]]></category>

		<guid isPermaLink="false">http://www.wetfeetblog.com/?p=212</guid>
		<description><![CDATA[Objective We are talking REST+JSON from browser to the server and back, so we need to report errors nicely. Exception thrown on the server have to be converted to error messages and transmitted to the client in JSON format. Luckily there is a very elegant solution for this in Spring. HandlerExceptionResolver interface Spring provides a [...]<p><a href="http://www.wetfeetblog.com/google-app-engine-java-spring-rest-mapping-exceptions-to-errors-for-json/212">Google App Engine + JAVA + Spring + REST + Mapping Exceptions to Errors for JSON</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.wetfeetblog.com/wp-content/uploads/2009/12/error_screenshot.jpg" alt="error_screenshot" title="error_screenshot" class="float-right" /></p>
<h3>Objective</h3>
<p>We are talking REST+JSON from browser to the server and back, so we need to report errors nicely. Exception thrown on the server have to be converted to error messages and transmitted to the client in JSON format. Luckily there is a very elegant solution for this in Spring.</p>
<h3>HandlerExceptionResolver interface</h3>
<p>Spring provides a very handy <strong>HandlerExceptionResolver</strong> interface for mapping exceptions to views. If you check the documentation (and you should) you will notice that where are couple exception handlers already implemented, but we will create our own. Just because we can.</p>
<p><span id="more-212"></span></p>
<h3>Implementing JsonExceptionResolver</h3>
<p>Our implementation is very basic, but can be enhanced to return more data about where the exception occurred, etc. To implement HandlerExceptionResolver interface we need to provide one method:</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">package</span> <span class="co2">com.lureto.rjf</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">javax.servlet.http.HttpServletRequest</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.servlet.http.HttpServletResponse</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">org.springframework.web.servlet.HandlerExceptionResolver</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">org.springframework.web.servlet.ModelAndView</span><span class="sy0">;</span>

<span class="kw1">public</span> <span class="kw1">class</span> JsonExceptionResolver <span class="kw1">implements</span> HandlerExceptionResolver <span class="br0">&#123;</span>

&nbsp; &nbsp; &nbsp; &nbsp; @Override
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> ModelAndView resolveException<span class="br0">&#40;</span>HttpServletRequest request, HttpServletResponse response,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Object</span></a> handler, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Exception</span></a> exception <span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ModelAndView mav <span class="sy0">=</span> <span class="kw1">new</span> ModelAndView<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mav.<span class="me1">setViewName</span><span class="br0">&#40;</span><span class="st0">&quot;MappingJacksonJsonView&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mav.<span class="me1">addObject</span><span class="br0">&#40;</span><span class="st0">&quot;error&quot;</span>, exception.<span class="me1">getMessage</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> mav<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

<span class="br0">&#125;</span></div>
</div>
</pre>
<p><strong>resolveException</strong> method returns ModelAndView, to which we add a model object &#8220;error&#8221; holding the message from the exception, and set the view name. The view in this case is MappingJacksonJsonView. Setting error to the exception message comes in handy when we need to return an error from lets say &#8220;saveUser&#8221; action &#8211; we simply throw exception from the controller method like this: <strong>throw new Exception(&#8220;USER_EMAIL_NOT_UNIQUE&#8221;);</strong></p>
<p>Next we need to hook in our exception resolver into Spring context like this:</p>
<pre>
<div class="codesnip-container" >
<div class="xml codesnip" style="font-family:monospace;"><span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">class</span>=<span class="st0">&quot;org.springframework.web.servlet.view.ContentNegotiatingViewResolver&quot;</span><span class="re2">&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;mediaTypes&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;map<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;entry</span> <span class="re0">key</span>=<span class="st0">&quot;json&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;application/json&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/map<span class="re2">&gt;</span></span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;/property<span class="re2">&gt;</span></span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;defaultContentType&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;application/json&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;defaultViews&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;list<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">name</span>=<span class="st0">&quot;MappingJacksonJsonView&quot;</span></span>
<span class="sc3"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">class</span>=<span class="st0">&quot;org.springframework.web.servlet.view.json.MappingJacksonJsonView&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/list<span class="re2">&gt;</span></span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;/property<span class="re2">&gt;</span></span></span>
<span class="sc3"><span class="re1">&lt;/bean<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp;
<span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">id</span>=<span class="st0">&quot;exceptionResolver&quot;</span> <span class="re0">class</span>=<span class="st0">&quot;com.lureto.rjf.JsonExceptionResolver&quot;</span> <span class="re2">/&gt;</span></span></div>
</div>
</pre>
<p>We simply define the bean and name call it exceptionResolver. Spring detects implemented interface and automagicaly routes exceptions to this resolver. We needed to modify our view definition &#8211; we gave it name, so we can reference it in the code &#8211; &#8220;MappingJacksonJsonView&#8221;.</p>
<h3>Conclusion</h3>
<p>Trapping exceptions on the server and converting them to JSON error messages can be done with 4 lines of conde and 1 line of xml. You can use this approach to map all sorts of exceptions to custom error jsp pages, JSON errors or any other view type. Your application users do not have to see an ugly Tomcat 500 error page.</p>
<p><a href="http://www.wetfeetblog.com/google-app-engine-java-spring-rest-mapping-exceptions-to-errors-for-json/212">Google App Engine + JAVA + Spring + REST + Mapping Exceptions to Errors for JSON</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.wetfeetblog.com/google-app-engine-java-spring-rest-mapping-exceptions-to-errors-for-json/212/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Goolge app engine + java + spring + REST + JSON + Flex – Part 3</title>
		<link>http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json-flex-part-3/185</link>
		<comments>http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json-flex-part-3/185#comments</comments>
		<pubDate>Wed, 18 Nov 2009 21:25:56 +0000</pubDate>
		<dc:creator>Tomas Mazukna</dc:creator>
				<category><![CDATA[Google App Engine]]></category>
		<category><![CDATA[Spring Framework]]></category>

		<guid isPermaLink="false">http://www.wetfeetblog.com/?p=185</guid>
		<description><![CDATA[Objective After reading this post you will be able to implement robust user authentication and authorization scheme without replicating code to check if user is logged in and if the are authorized to perform a certain action. The Problem Domain You, like me, face the same problem with each web application &#8211; user authentication and [...]<p><a href="http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json-flex-part-3/185">Goolge app engine + java + spring + REST + JSON + Flex – Part 3</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.wetfeetblog.com/wp-content/uploads/2009/11/the_lock.jpg" alt="the_lock" title="the_lock" class="float-right" /></p>
<h3>Objective</h3>
<p>After reading this post you will be able to implement robust user authentication and authorization scheme without replicating code to check if user is logged in and if the are authorized to perform a certain action. </p>
<h3>The Problem Domain</h3>
<p>You, like me, face the same problem with each web application &#8211; user authentication and authorization. Who is logged in? Can they delete this entity? Can they see this report? How to solve this problem without copy pasting same piece of code to every servlet/action/controller/whatever? If you are using spring MVC there is a very elegant and powerful solution &#8211; <b>Interceptors</b>.</p>
<p>You can have as many interceptors as you like, I usually have one for security and maybe one more for some other task. Interceptor fires before your controller/action gets invoked. If it returns <b>true</b> the flow can continue, if it returns <b>false</b> the execution stops right there &#8211; request has been handled by the interceptor.<br />
<span id="more-185"></span></p>
<h3>Step One &#8211; Adding an interceptor to our <a href="http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json-flex-part-1/87"><u>application form part 1</u></a></h3>
<p>First lets code our interceptor. I named it SessionInerceptor.</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">package</span> <span class="co2">com.lureto.rjf</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">javax.servlet.http.HttpServletRequest</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.servlet.http.HttpServletResponse</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">org.springframework.web.servlet.handler.HandlerInterceptorAdapter</span><span class="sy0">;</span>

<span class="kw1">public</span> <span class="kw1">class</span> SessionInterceptor <span class="kw1">extends</span> HandlerInterceptorAdapter <span class="br0">&#123;</span>

&nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">boolean</span> preHandle<span class="br0">&#40;</span> HttpServletRequest request, HttpServletResponse response, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Object</span></a> handler<span class="br0">&#41;</span> 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Exception</span></a> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; User user <span class="sy0">=</span> <span class="br0">&#40;</span>User<span class="br0">&#41;</span>request.<span class="me1">getSession</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">getAttribute</span><span class="br0">&#40;</span> Constants.<span class="me1">SESSION_USER</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> uri <span class="sy0">=</span> request.<span class="me1">getRequestURI</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> base <span class="sy0">=</span> request.<span class="me1">getContextPath</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; uri <span class="sy0">=</span> uri.<span class="me1">substring</span><span class="br0">&#40;</span> base.<span class="me1">length</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span> user <span class="sy0">==</span> <span class="kw2">null</span> <span class="sy0">&amp;&amp;</span> <span class="sy0">!</span>uri.<span class="me1">startsWith</span><span class="br0">&#40;</span><span class="st0">&quot;/api/login&quot;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.<span class="me1">getSession</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">invalidate</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span class="me1">setContentType</span><span class="br0">&#40;</span><span class="st0">&quot;application/json&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span class="me1">getWriter</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">write</span><span class="br0">&#40;</span><span class="st0">&quot;{<span class="es0">\&quot;</span>error<span class="es0">\&quot;</span>:<span class="es0">\&quot;</span>SESSION-EXPIRED<span class="es0">\&quot;</span>}&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">true</span><span class="sy0">;</span>
&nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; 
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>As you see we are extending HandlerInterceptorAdapter class by implementing preHandle() method. If you need you can use postHandle() to perfor some action after the main handler has finished work. My implementation for this example is pretty simple &#8211; check only if the user is present in the session. If no user is present and user is not trying to log in, send back an error. In pure web application you probably would want to send a redirect to the login page.</p>
<p>Now lets add this interceptor to the Spring MVC stack by adding these 5 lines to our rest-json-flex-servlet.xml file.</p>
<pre>
<div class="codesnip-container" >
<div class="xml codesnip" style="font-family:monospace;"><span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">class</span>=<span class="st0">&quot;org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;interceptors&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">class</span>=<span class="st0">&quot;com.lureto.rjf.SessionInterceptor&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/property<span class="re2">&gt;</span></span></span>
<span class="sc3"><span class="re1">&lt;/bean<span class="re2">&gt;</span></span></span></div>
</div>
</pre>
<p>One more thing &#8211; we need to enable session in google app engine. Add this line to your appengine-web.xml</p>
<pre>
<div class="codesnip-container" >
<div class="xml codesnip" style="font-family:monospace;"><span class="sc3"><span class="re1">&lt;sessions-enabled<span class="re2">&gt;</span></span></span>true<span class="sc3"><span class="re1">&lt;/sessions-enabled<span class="re2">&gt;</span></span></span></div>
</div>
</pre>
<h3>Step 2 &#8211; Implement Login Controller</h3>
<p>Now we have our SessionInterceptor stopping us from accessing the services, we need to ad the way to log in. My inplementation of LoginController:</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">package</span> <span class="co2">com.lureto.rjf</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">java.io.IOException</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">java.util.List</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">javax.jdo.PersistenceManager</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.Query</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.servlet.http.HttpServletRequest</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">org.apache.log4j.Logger</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">org.springframework.beans.factory.annotation.Autowired</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">org.springframework.stereotype.Controller</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">org.springframework.web.bind.annotation.ModelAttribute</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">org.springframework.web.bind.annotation.RequestBody</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">org.springframework.web.bind.annotation.RequestMapping</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">org.springframework.web.bind.annotation.RequestMethod</span><span class="sy0">;</span>

@Controller
@RequestMapping<span class="br0">&#40;</span><span class="st0">&quot;/login&quot;</span><span class="br0">&#41;</span>
<span class="kw1">public</span> <span class="kw1">class</span> LoginController <span class="br0">&#123;</span>

&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw1">static</span> Logger logger <span class="sy0">=</span> Logger.<span class="me1">getLogger</span><span class="br0">&#40;</span> LoginController.<span class="kw1">class</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; @Autowired
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> PMF pmf<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; @SuppressWarnings<span class="br0">&#40;</span><span class="st0">&quot;unchecked&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; @ModelAttribute<span class="br0">&#40;</span><span class="st0">&quot;user&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; @RequestMapping<span class="br0">&#40;</span>value <span class="sy0">=</span> <span class="st0">&quot;/&quot;</span>, method <span class="sy0">=</span> RequestMethod.<span class="me1">POST</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> User login<span class="br0">&#40;</span> @RequestBody User user, HttpServletRequest request <span class="br0">&#41;</span> <span class="kw1">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">IOException</span></a> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; logger.<span class="me1">debug</span><span class="br0">&#40;</span>user<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Hardcoded for demo website</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span> user.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">equalsIgnoreCase</span><span class="br0">&#40;</span><span class="st0">&quot;user1&quot;</span><span class="br0">&#41;</span> <span class="sy0">&amp;&amp;</span> user.<span class="me1">getPassword</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">equals</span><span class="br0">&#40;</span><span class="st0">&quot;password1&quot;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; user.<span class="me1">setId</span><span class="br0">&#40;</span>1001<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; user.<span class="me1">setEmail</span><span class="br0">&#40;</span><span class="st0">&quot;fake@email.com&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.<span class="me1">getSession</span><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span>.<span class="me1">setAttribute</span><span class="br0">&#40;</span> Constants.<span class="me1">SESSION_USER</span> , user<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> user<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; PersistenceManager pm <span class="sy0">=</span> pmf.<span class="me1">getManager</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Query query <span class="sy0">=</span> pm.<span class="me1">newQuery</span><span class="br0">&#40;</span><span class="st0">&quot;select from com.lureto.rjf.User &quot;</span> <span class="sy0">+</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;where password == passwordParam &amp;&amp; name == nameParam &quot;</span> <span class="sy0">+</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;parameters String passwordParam, String nameParam&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<span class="sy0">&lt;</span>User<span class="sy0">&gt;</span> users<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">try</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; users <span class="sy0">=</span> <span class="br0">&#40;</span>List<span class="sy0">&lt;</span>User<span class="sy0">&gt;</span><span class="br0">&#41;</span> query.<span class="me1">execute</span><span class="br0">&#40;</span> user.<span class="me1">getPassword</span><span class="br0">&#40;</span><span class="br0">&#41;</span>, user.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span> users.<span class="me1">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">&gt;</span> 0 <span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; User luser <span class="sy0">=</span> users.<span class="me1">get</span><span class="br0">&#40;</span>0<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.<span class="me1">getSession</span><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span>.<span class="me1">setAttribute</span><span class="br0">&#40;</span> Constants.<span class="me1">SESSION_USER</span> , luser<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> luser<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">finally</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; query.<span class="me1">closeAll</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">null</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> PMF getPmf<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> pmf<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> setPmf<span class="br0">&#40;</span>PMF pmf<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">pmf</span> <span class="sy0">=</span> pmf<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; 
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>What happens here is very strait forward: we construct a query to the big table to find a user matching supplied name and password. If we find one, we create a session and put our user object there. Our SessionInterceptor expects to find this user there once we try access /api/user/ endpoints.<br />
Since Spring is scanning this package and we are using annotation, we do not need to add this controller to the xml file.</p>
<h3>Step 4 &#8211; Enhance existing User bean to support permissions</h3>
<p>Here is my User.java</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;">@PersistenceCapable<span class="br0">&#40;</span>identityType <span class="sy0">=</span> IdentityType.<span class="me1">APPLICATION</span>, detachable<span class="sy0">=</span><span class="st0">&quot;true&quot;</span><span class="br0">&#41;</span>
<span class="kw1">public</span> <span class="kw1">class</span> User <span class="br0">&#123;</span>

&nbsp; &nbsp; @PrimaryKey
&nbsp; &nbsp; @Persistent<span class="br0">&#40;</span>valueStrategy <span class="sy0">=</span> IdGeneratorStrategy.<span class="me1">IDENTITY</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Along+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Long</span></a> id<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> email<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> name<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> password<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> List<span class="sy0">&lt;</span>String<span class="sy0">&gt;</span> permissions<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; <span class="kw1">public</span> User<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; permissions <span class="sy0">=</span> <span class="kw1">new</span> ArrayList<span class="sy0">&lt;</span>String<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; 
&nbsp; &nbsp; <span class="co1">// getters and setters here</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<h3>Step 5 &#8211; Add Login Screen to Flex application</h3>
<p><img src="http://www.wetfeetblog.com/wp-content/uploads/2009/11/login_screen.png" alt="login_screen" title="login_screen" width="457" height="282" class="alignnone size-full wp-image-199" /></p>
<p>This is how the finished screen looks like, once you log in, you end up in this screen.</p>
<p><img src="http://www.wetfeetblog.com/wp-content/uploads/2009/11/user-edit-screen.png" alt="user-edit-screen" title="user-edit-screen" width="631" height="535" class="alignnone size-full wp-image-200" /></p>
<p>Full source code for <a href="http://www.wetfeetblog.com/wp-content/uploads/2009/11/JsonRestClient.mxml"><u>JsonRestClient.mxml</u></a>.</p>
<h3>Step 6 &#8211; User Authorization Implementation</h3>
<p>As you see from all the work we have done to this point we are not checking for user permissions. I intentional left it out. The best solution will depend on your particular application. In this case, we need to come up with the permission convention first. I would probably use &#8220;action-uri&#8221; convention. So to be able to get the user list you need to have &#8220;GET-user&#8221; permission, to update it would be &#8220;POST-user&#8221;, etc. You may come up with something better for your situation.</p>
<h3>Conclusion</h3>
<p>Interceptors in Spring is the perfect way of moving user authentication and authorization code form controllers into centralized place. This makes your controllers cleaner. You already know that the right user with right permissions is performing this action.</p>
<p><a href="http://www.wetfeetblog.com/wp-content/uploads/2009/11/rest-json-flex-part3.zip"><u>Source Code for Java part can be found Here.</u></a></p>
<p><a href="http://rest-json-flex.appspot.com/"><u>Demo application can be found here</u></a>. (check the LoginController in Step 2 to figure out the user + password to log in )</p>
<p>Leave me a comment what permission checking convention you would use.</p>
<p>Best of Luck!<br />
Tomas</p>
<p><a href="http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json-flex-part-3/185">Goolge app engine + java + spring + REST + JSON + Flex – Part 3</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json-flex-part-3/185/feed</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Goolge app engine + java + spring + REST + JSON + Flex – Part 2</title>
		<link>http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json-flex-part-2/147</link>
		<comments>http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json-flex-part-2/147#comments</comments>
		<pubDate>Thu, 29 Oct 2009 12:42:27 +0000</pubDate>
		<dc:creator>Tomas Mazukna</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[Google App Engine]]></category>
		<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://www.wetfeetblog.com/?p=147</guid>
		<description><![CDATA[Objective Once you are finished with this article, you will be able to store application data in Google App Engine Data store and consume REST service using flex client. Common Problems and Solutions In my personal opinion flash player security model is the biggest piece of bullshit I ever encountered. If you sit down and [...]<p><a href="http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json-flex-part-2/147">Goolge app engine + java + spring + REST + JSON + Flex – Part 2</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.wetfeetblog.com/wp-content/uploads/2009/10/www_1.jpg" alt="www_1" title="www_1" class="float-right" /></p>
<h3>Objective</h3>
<p>Once you are finished with this article, you will be able to store application data in Google App Engine Data store and consume REST service using flex client.</p>
<h3>Common Problems and Solutions</h3>
<p>In my personal opinion flash player security model is the biggest piece of bullshit I ever encountered. If you sit down and thing just for 10 seconds about how it is implemented you will have to agree that it provides NO security at all. It just makes impossible so do certain things, that&#8217;s all. Adobe needs to drop it now.</p>
<p>Here is the problem that we have with REST and Google app engine: Adobe&#8217;s implementation of HTTPService only supports GET and POST, to consume REST we need at least 3 methods: GET, POST and DELETE. We can live without PUT &#8211; if id is null JDO will create a new persisted object, if it is set JDO will update it. But we still do not have support for DELETE. <span id="more-147"></span></p>
<p>If you are implementing Air Application there are good news: <a href="http://code.google.com/p/as3httpclientlib/">as3httpclientlib</a> &#8211; a socket implementation of http client that can do all that we need: HEAD, GET, POST, PUT, DELETE. It depends on <a href="http://code.google.com/p/as3corelib/">as3corelib</a> and <a href="http://code.google.com/p/as3crypto/">as3crypto</a>, so grab those too.</p>
<p>If you are implementing a web application in google app engine, you are out of luck with as3httpclientlib. The reason is that adobe has this perverted understanding about security: since as3httpclientlib utilizes the Socket to implement http client adobe flash player tries to &#8220;protect&#8221; the server that socket is trying to connect by sending<br />
&#8220;&#060;policy-file-request/&#062;&#8221; to the 843 port to get a scoket policy file, it that fails it tries the port the socket is trying to connect to. Since this is not a http request, google app engine ignores it, and you can not create sockets in google app engine &#8211; they are not supported, so you can not write a simple class to send back what flash player wants. The conclusion: Adobe Flex has not yet made it to Web 2.0, it can not talk REST to the server. But there are workarounds and here is mine:</p>
<h3>Step one &#8211; Add persistence management code to the project we created in <a href="http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json-flex-part-1/87">part 1</a></h3>
<p>First we need to modify our <strong>User.java</strong> class to support persistence:</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">import</span> <span class="co2">javax.jdo.annotations.IdGeneratorStrategy</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.IdentityType</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.PersistenceCapable</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.Persistent</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.annotations.PrimaryKey</span><span class="sy0">;</span>

@PersistenceCapable<span class="br0">&#40;</span>identityType <span class="sy0">=</span> IdentityType.<span class="me1">APPLICATION</span>, detachable<span class="sy0">=</span><span class="st0">&quot;true&quot;</span><span class="br0">&#41;</span>
<span class="kw1">public</span> <span class="kw1">class</span> User <span class="br0">&#123;</span>

&nbsp; &nbsp; @PrimaryKey
&nbsp; &nbsp; @Persistent<span class="br0">&#40;</span>valueStrategy <span class="sy0">=</span> IdGeneratorStrategy.<span class="me1">IDENTITY</span><span class="br0">&#41;</span>
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Along+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Long</span></a> id<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> email<span class="sy0">;</span>
&nbsp; &nbsp; @Persistent
&nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> name<span class="sy0">;</span>

&nbsp; &nbsp; <span class="co1">// Getter and setters go here......</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>As you can see we have instructed JDO that we will store User objects at application level, added a primary key and its generation strategy and annotated members to be persisted.</p>
<p>Next we need to get hold of a persistence manager factory, since it is a very very costly operation, we wan to do it only once in the lifetime of our application. For this purpose lets create a singleton which will hold this instance for us:</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">package</span> <span class="co2">com.lureto.rjf</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">javax.jdo.JDOHelper</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.PersistenceManager</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.PersistenceManagerFactory</span><span class="sy0">;</span>

<span class="kw1">public</span> <span class="kw1">class</span> PMF <span class="br0">&#123;</span>

&nbsp; &nbsp; <span class="kw1">private</span> <span class="kw1">static</span> <span class="kw1">final</span> PersistenceManagerFactory pmfInstance <span class="sy0">=</span> 
JDOHelper.<span class="me1">getPersistenceManagerFactory</span><span class="br0">&#40;</span><span class="st0">&quot;transactions-optional&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>

&nbsp; &nbsp; <span class="kw1">private</span> PMF<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>

&nbsp; &nbsp; <span class="kw1">public</span> <span class="kw1">static</span> PersistenceManager getManager<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> pmfInstance.<span class="me1">getPersistenceManager</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; <span class="kw1">public</span> <span class="kw1">static</span> PersistenceManagerFactory get<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> pmfInstance<span class="sy0">;</span>
&nbsp; &nbsp; <span class="br0">&#125;</span>

<span class="br0">&#125;</span></div>
</div>
</pre>
<p>The last step is to modify our <strong>UserContrller.java</strong> to actually store and retrieve the User objects from the data store.</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">package</span> <span class="co2">com.lureto.rjf</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">java.io.IOException</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">java.util.List</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">javax.jdo.PersistenceManager</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.jdo.Query</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">org.apache.log4j.Logger</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">org.springframework.stereotype.Controller</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">org.springframework.web.bind.annotation.ModelAttribute</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">org.springframework.web.bind.annotation.PathVariable</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">org.springframework.web.bind.annotation.RequestBody</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">org.springframework.web.bind.annotation.RequestMapping</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">org.springframework.web.bind.annotation.RequestMethod</span><span class="sy0">;</span>

@Controller
@RequestMapping<span class="br0">&#40;</span><span class="st0">&quot;/user&quot;</span><span class="br0">&#41;</span>
<span class="kw1">public</span> <span class="kw1">class</span> UserController <span class="br0">&#123;</span>

&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw1">static</span> Logger logger <span class="sy0">=</span> Logger.<span class="me1">getLogger</span><span class="br0">&#40;</span> UserController.<span class="kw1">class</span> <span class="br0">&#41;</span><span class="sy0">;</span>

&nbsp; &nbsp; &nbsp; &nbsp; @SuppressWarnings<span class="br0">&#40;</span><span class="st0">&quot;unchecked&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; @ModelAttribute<span class="br0">&#40;</span><span class="st0">&quot;users&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; @RequestMapping<span class="br0">&#40;</span>value <span class="sy0">=</span> <span class="st0">&quot;/&quot;</span>, method <span class="sy0">=</span> RequestMethod.<span class="me1">GET</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alist+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">List</span></a> listUsers<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw1">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">IOException</span></a> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PersistenceManager pm <span class="sy0">=</span> PMF.<span class="me1">getManager</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Query query <span class="sy0">=</span> pm.<span class="me1">newQuery</span><span class="br0">&#40;</span>User.<span class="kw1">class</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alist+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">List</span></a> users<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">try</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; users <span class="sy0">=</span> <span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alist+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">List</span></a><span class="br0">&#41;</span> query.<span class="me1">execute</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">finally</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; query.<span class="me1">closeAll</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> users<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; @ModelAttribute<span class="br0">&#40;</span><span class="st0">&quot;user&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; @RequestMapping<span class="br0">&#40;</span>value <span class="sy0">=</span> <span class="st0">&quot;/&quot;</span>, method <span class="sy0">=</span> RequestMethod.<span class="me1">POST</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> User saveUser<span class="br0">&#40;</span> @RequestBody User user <span class="br0">&#41;</span> <span class="kw1">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">IOException</span></a> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; logger.<span class="me1">debug</span><span class="br0">&#40;</span>user<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PersistenceManager pm <span class="sy0">=</span> PMF.<span class="me1">getManager</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">try</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pm.<span class="me1">makePersistent</span><span class="br0">&#40;</span>user<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">finally</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pm.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> user<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; @RequestMapping<span class="br0">&#40;</span>value <span class="sy0">=</span> <span class="st0">&quot;/{userId}&quot;</span>, method <span class="sy0">=</span> RequestMethod.<span class="me1">DELETE</span> <span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> deleteUser<span class="br0">&#40;</span> @PathVariable <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> userId <span class="br0">&#41;</span> <span class="kw1">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">IOException</span></a> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; deleteUserIntenral<span class="br0">&#40;</span>userId<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; @RequestMapping<span class="br0">&#40;</span>value <span class="sy0">=</span> <span class="st0">&quot;/{userId}/delete&quot;</span>, method <span class="sy0">=</span> RequestMethod.<span class="me1">POST</span> <span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> deleteUser2<span class="br0">&#40;</span> @PathVariable <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> userId <span class="br0">&#41;</span> <span class="kw1">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">IOException</span></a> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; deleteUserIntenral<span class="br0">&#40;</span>userId<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">void</span> deleteUserIntenral<span class="br0">&#40;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> userId <span class="br0">&#41;</span> <span class="kw1">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">IOException</span></a> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; logger.<span class="me1">debug</span><span class="br0">&#40;</span><span class="st0">&quot;Deleting user id &gt; &quot;</span><span class="sy0">+</span>userId<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; PersistenceManager pm <span class="sy0">=</span> PMF.<span class="me1">getManager</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">try</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Along+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Long</span></a> id <span class="sy0">=</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Along+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Long</span></a>.<span class="me1">parseLong</span><span class="br0">&#40;</span>userId<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; User user <span class="sy0">=</span> pm.<span class="me1">getObjectById</span><span class="br0">&#40;</span> User.<span class="kw1">class</span>, id <span class="br0">&#41;</span><span class="sy0">;</span> 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pm.<span class="me1">deletePersistent</span><span class="br0">&#40;</span>user<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">finally</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pm.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>As you can see from the code above, saving and getting a list of Users is pretty trivial coding task. You may also notice that we added <strong>deleteUser(@PathVariable String userId)</strong> method to delete the user when a delete request is made with user id added to the end of the path. To delete the user object first we need to fetch it from the data store and only then ask persistence manager to delete it. We hav two ways to delte the user &#8211; sending DELETE requests and sending POST to &#8220;/api/user/{userId}/delete&#8221;. The post method is there to get around the shortcomings of Actions Scrip 3.</p>
<p>And one last change we need to make to support not so mature flex &#8211; to add <strong>crossdomain.xml</strong> file to /war of our project so flash player security implementation  stops throwing security exceptions. Like I said before this adds nothing to the security, but annoys the hell out of the developers. Here is the contents:</p>
<pre>
<div class="codesnip-container" >
<div class="xml codesnip" style="font-family:monospace;"><span class="sc3"><span class="re1">&lt;?xml</span> <span class="re0">version</span>=<span class="st0">&quot;1.0&quot;</span><span class="re2">?&gt;</span></span><span class="sc0">&lt;!DOCTYPE cross-domain-policySYSTEM </span>
<span class="sc0"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd&quot;&gt;</span>
<span class="sc3"><span class="re1">&lt;cross-domain-policy<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;allow-access-from</span> <span class="re0">domain</span>=<span class="st0">&quot;*&quot;</span> <span class="re0">to-ports</span>=<span class="st0">&quot;*&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;allow-http-request-headers-from</span> <span class="re0">domain</span>=<span class="st0">&quot;*&quot;</span> <span class="re0">headers</span>=<span class="st0">&quot;*&quot;</span><span class="re2">/&gt;</span></span>
<span class="sc3"><span class="re1">&lt;/cross-domain-policy<span class="re2">&gt;</span></span></span></div>
</div>
</pre>
<h3>Step two- Create Flex project to consume the service we just upgraded</h3>
<p>First create a Flex Web application project:<br />
<img class="alignnone size-full wp-image-157" title="project_step_1" src="http://www.wetfeetblog.com/wp-content/uploads/2009/10/project_step_11.png" alt="project_step_1" width="404" height="460" /></p>
<p>and then layout the UI to look like this:<br />
<img class="alignnone size-full wp-image-160" title="project_step_2" src="http://www.wetfeetblog.com/wp-content/uploads/2009/10/project_step_21.png" alt="project_step_2" width="589" height="365" /></p>
<p>And if you lazy like and want it all right now, here is the complete sudo code mxml file:</p>
<pre>
<div class="codesnip-container" >
<div class="xml codesnip" style="font-family:monospace;"><span class="sc3"><span class="re1">&lt;?xml</span> <span class="re0">version</span>=<span class="st0">&quot;1.0&quot;</span> <span class="re0">encoding</span>=<span class="st0">&quot;utf-8&quot;</span><span class="re2">?&gt;</span></span>
<span class="sc3"><span class="re1">&lt;mx:Application</span> <span class="re0">xmlns:mx</span>=<span class="st0">&quot;http://www.adobe.com/2006/mxml&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp;<span class="re0">layout</span>=<span class="st0">&quot;absolute&quot;</span> <span class="re0">creationComplete</span>=<span class="st0">&quot;init()&quot;</span><span class="re2">&gt;</span></span>
&nbsp; 
&nbsp; <span class="sc3"><span class="re1">&lt;mx:Script<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; <span class="sc2">&lt;![CDATA[</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp;import mx.rpc.events.ResultEvent;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp;import mx.rpc.events.FaultEvent;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp;import mx.rpc.http.HTTPService;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp;import mx.managers.PopUpManager;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp;import json.JParser;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp;import mx.controls.Alert;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp;import mx.controls.List;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp;[Bindable]</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp;private var users:Array;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp;private var uriBase: String = &quot;http://localhost:8080/api/user/&quot;;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp;private var popupLabel:Label;</span>

<span class="sc2"> &nbsp; &nbsp; &nbsp;private function init() : void {</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;var myUrl : String = Application.application.url;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;if( myUrl.indexOf( &quot;file://&quot; ) != 0 ) </span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;uriBase = myUrl.substring( 0, myUrl.lastIndexOf( &quot;/&quot; ) ) + &quot;/api/user/&quot; ;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;refreshUsers() &nbsp; &nbsp; &nbsp;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp;}</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp;private function addNew() : void {</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;this.user_id.text = &quot;&quot;;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;this.user_email.text = &quot;&quot;;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;this.user_name.text = &quot;&quot;;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp;}</span>

<span class="sc2"> &nbsp; &nbsp; &nbsp;private function updateEdit() : void {</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;this.user_id.text = &nbsp;this.usersList.selectedItem.id;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;this.user_name.text = &nbsp;this.usersList.selectedItem.name;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;this.user_email.text = &nbsp;this.usersList.selectedItem.email;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp;}</span>

<span class="sc2"> &nbsp; &nbsp; &nbsp;private function deleteUser() : void {</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;if( usersList.selectedItem == null )</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;showPopupWait();</span>

<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;var service: HTTPService = new HTTPService();</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;service.contentType = &quot;application/json&quot;;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;service.method = &quot;POST&quot;;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;service.resultFormat = &quot;text&quot;;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;service.url = uriBase + usersList.selectedItem.id + &quot;/delete&quot; ;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;service.useProxy = false;</span>

<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;service.addEventListener( FaultEvent.FAULT , function(event:FaultEvent):void {</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;removePopup();</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Alert.show( event.message.toString() );</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} ) &nbsp; &nbsp; &nbsp;</span>

<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;service.addEventListener( ResultEvent.RESULT , function(event:ResultEvent):void {</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;refreshUsers(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} ) &nbsp; &nbsp; &nbsp;</span>

<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;service.send();</span>

<span class="sc2"> &nbsp; &nbsp; &nbsp;}</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp;private function saveUser() : void {</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;showPopupWait();</span>

<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;var service: HTTPService = new HTTPService();</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;service.contentType = &quot;application/json&quot;;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;service.method = &quot;POST&quot;;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;service.resultFormat = &quot;text&quot;;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;service.url = uriBase;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;service.useProxy = false;</span>

<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;service.addEventListener( FaultEvent.FAULT , function(event:FaultEvent):void {</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;removePopup();</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Alert.show( event.message.toString() );</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} ) &nbsp; &nbsp; &nbsp;</span>

<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;service.addEventListener( ResultEvent.RESULT , function(event:ResultEvent):void {</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var response: Object = JParser.decode( event.message.body.toString() );</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;user_id.text = response.user.id;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;refreshUsers(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} ) &nbsp; &nbsp; &nbsp;</span>


<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;var user:Object = new Object();</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;if( this.user_id.text.length &gt; 0 )</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;user.id = this.user_id.text</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;user.name = this.user_name.text;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;user.email = this.user_email.text;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;var json:String = JParser.encode( user );</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;var jsonData:ByteArray = new ByteArray();</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;jsonData.writeUTFBytes(json);</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;jsonData.position = 0;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;var contentType:String = &quot;application/json&quot;;</span>

<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;//client.post(uri, jsonData, contentType);</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;service.send( jsonData );</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp;}</span>

<span class="sc2"> &nbsp; &nbsp; &nbsp;private function refreshUsers() : void {</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;showPopupWait();</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;var service: HTTPService = new HTTPService();</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;service.contentType = &quot;application/json&quot;;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;service.method = &quot;GET&quot;;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;service.resultFormat = &quot;text&quot;;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;// add random number to make browser grab new list.... sux, but works.</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;service.url = uriBase+&quot;?&quot;+ (new Date).getTime();</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;service.useProxy = false;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;service.addEventListener( FaultEvent.FAULT , function(event:FaultEvent):void {</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;removePopup();</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Alert.show( event.message.toString() );</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} ) &nbsp; &nbsp; &nbsp;</span>

<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;service.addEventListener( ResultEvent.RESULT , function(event:ResultEvent):void {</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var userList: Object = JParser.decode( event.message.body.toString() );</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;users = userList.users as Array ;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;removePopup();</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} ) &nbsp; &nbsp; &nbsp;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;service.send(); &nbsp; &nbsp; &nbsp; &nbsp;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp;}</span>

<span class="sc2"> &nbsp; &nbsp; &nbsp;public function showPopup( message:String ) : void {</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;if( this.popupLabel == null ) {</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.popupLabel = new Label();</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.popupLabel.text = &nbsp;message ;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.popupLabel.setStyle( &quot;fontSize&quot; , &quot;22&quot; );</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.popupLabel.setStyle( &quot;fontWeight&quot; , &quot;bold&quot; );</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PopUpManager.addPopUp( this.popupLabel, this, true );</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PopUpManager.centerPopUp( this.popupLabel );</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;}</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp;}</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp;public function showPopupWait() : void {</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;this.showPopup( &quot;Please wait.....&quot;);</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp;}</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp;public function removePopup() : void {</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;if( this.popupLabel != null )</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PopUpManager.removePopUp( this.popupLabel );</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp; &nbsp;this.popupLabel = null;</span>
<span class="sc2"> &nbsp; &nbsp; &nbsp;}</span>

<span class="sc2"> &nbsp; &nbsp;]]&gt;</span>
&nbsp; <span class="sc3"><span class="re1">&lt;/mx:Script<span class="re2">&gt;</span></span></span>
&nbsp; 
&nbsp; <span class="sc3"><span class="re1">&lt;mx:DataGrid</span> <span class="re0">x</span>=<span class="st0">&quot;10&quot;</span> <span class="re0">y</span>=<span class="st0">&quot;35&quot;</span> <span class="re0">id</span>=<span class="st0">&quot;usersList&quot;</span> <span class="re0">dataProvider</span>=<span class="st0">&quot;{users}&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp; <span class="re0">width</span>=<span class="st0">&quot;469&quot;</span> <span class="re0">height</span>=<span class="st0">&quot;159&quot;</span> <span class="re0">itemClick</span>=<span class="st0">&quot;updateEdit()&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;mx:columns<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;mx:DataGridColumn</span> <span class="re0">headerText</span>=<span class="st0">&quot;ID&quot;</span> <span class="re0">dataField</span>=<span class="st0">&quot;id&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;mx:DataGridColumn</span> <span class="re0">headerText</span>=<span class="st0">&quot;Email&quot;</span> <span class="re0">dataField</span>=<span class="st0">&quot;email&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;mx:DataGridColumn</span> <span class="re0">headerText</span>=<span class="st0">&quot;Name&quot;</span> <span class="re0">dataField</span>=<span class="st0">&quot;name&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/mx:columns<span class="re2">&gt;</span></span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;/mx:DataGrid<span class="re2">&gt;</span></span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;mx:Label</span> <span class="re0">x</span>=<span class="st0">&quot;10&quot;</span> <span class="re0">y</span>=<span class="st0">&quot;10&quot;</span> <span class="re0">text</span>=<span class="st0">&quot;Users&quot;</span> <span class="re0">fontSize</span>=<span class="st0">&quot;14&quot;</span> <span class="re0">fontWeight</span>=<span class="st0">&quot;bold&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;mx:Button</span> <span class="re0">x</span>=<span class="st0">&quot;487&quot;</span> <span class="re0">y</span>=<span class="st0">&quot;59&quot;</span> <span class="re0">label</span>=<span class="st0">&quot;Add New&quot;</span> <span class="re0">click</span>=<span class="st0">&quot;addNew()&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;mx:Button</span> <span class="re0">x</span>=<span class="st0">&quot;487&quot;</span> <span class="re0">y</span>=<span class="st0">&quot;89&quot;</span> <span class="re0">label</span>=<span class="st0">&quot;Delete&quot;</span> <span class="re0">width</span>=<span class="st0">&quot;76&quot;</span> <span class="re0">click</span>=<span class="st0">&quot;deleteUser()&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;mx:Button</span> <span class="re0">x</span>=<span class="st0">&quot;487&quot;</span> <span class="re0">y</span>=<span class="st0">&quot;119&quot;</span> <span class="re0">label</span>=<span class="st0">&quot;Refresh&quot;</span> <span class="re0">width</span>=<span class="st0">&quot;76&quot;</span> <span class="re0">click</span>=<span class="st0">&quot;refreshUsers()&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;mx:Form</span> <span class="re0">x</span>=<span class="st0">&quot;10&quot;</span> <span class="re0">y</span>=<span class="st0">&quot;202&quot;</span> <span class="re0">width</span>=<span class="st0">&quot;469&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;mx:FormItem</span> <span class="re0">label</span>=<span class="st0">&quot;User ID&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;mx:TextInput</span> <span class="re0">id</span>=<span class="st0">&quot;user_id&quot;</span> <span class="re0">width</span>=<span class="st0">&quot;353&quot;</span> <span class="re0">editable</span>=<span class="st0">&quot;false&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/mx:FormItem<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;mx:FormItem</span> <span class="re0">label</span>=<span class="st0">&quot;User Name&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;mx:TextInput</span> <span class="re0">id</span>=<span class="st0">&quot;user_name&quot;</span> <span class="re0">width</span>=<span class="st0">&quot;353&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/mx:FormItem<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;mx:FormItem</span> <span class="re0">label</span>=<span class="st0">&quot;User Email&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;mx:TextInput</span> <span class="re0">id</span>=<span class="st0">&quot;user_email&quot;</span> <span class="re0">width</span>=<span class="st0">&quot;353&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/mx:FormItem<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;mx:FormItem<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;mx:Button</span> <span class="re0">label</span>=<span class="st0">&quot;Save&quot;</span> <span class="re0">width</span>=<span class="st0">&quot;96&quot;</span> <span class="re0">click</span>=<span class="st0">&quot;saveUser()&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/mx:FormItem<span class="re2">&gt;</span></span></span>
&nbsp; <span class="sc3"><span class="re1">&lt;/mx:Form<span class="re2">&gt;</span></span></span>
&nbsp; 
<span class="sc3"><span class="re1">&lt;/mx:Application<span class="re2">&gt;</span></span></span></div>
</div>
</pre>
<p>To serialize objects to and from the server we need a JSON library. I chose <a href="http://www.waynemike.pwp.blueyonder.co.uk/jswoof/">JSwoof.swc</a> &#8211; JSON serializer and parser. I found it to be a little better qauality then the one in as3corelib, but you can use either one.</p>
<p>Once you get the project compiling, start your google app engine project locally, mine is configured to listen on port 8080, and then you can run the flex project. For the lazy (like me) here is the <a href="http://www.wetfeetblog.com/wp-content/uploads/2009/10/json-rest-client.zip">full project</a> all ready to go.</p>
<h3>Conclusion</h3>
<p>Adding persistence support in google app engine project is not hard, you just need to read documentation carefully.</p>
<p>The harder part is to get flex talking to these services. Like any closed source solution you are at mercy of company making it, like we see in this particular case. Unfortunately there are no better alternative to FLex for building nice looking af functional web application, we just need to deal with its shortcomings.</p>
<p>You can check out the working application at <a href="http://rest-json-flex.appspot.com/">rest-json-flex.appspot.com</a></p>
<p>In <a href="http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json-flex-part-3/185">step 3 we will add session management</a>, user authentication and authorization to our google app engine REST application.</p>
<p><a href="http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json-flex-part-2/147">Goolge app engine + java + spring + REST + JSON + Flex – Part 2</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json-flex-part-2/147/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Goolge app engine + java + spring + REST + JSON + Flex &#8211; Part 1</title>
		<link>http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json-flex-part-1/87</link>
		<comments>http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json-flex-part-1/87#comments</comments>
		<pubDate>Mon, 26 Oct 2009 14:26:42 +0000</pubDate>
		<dc:creator>Tomas Mazukna</dc:creator>
				<category><![CDATA[Google App Engine]]></category>
		<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[json]]></category>

		<guid isPermaLink="false">http://www.wetfeetblog.com/?p=87</guid>
		<description><![CDATA[Objective Once you are finished with this article, you will be able to implement REST services in Google Apps Engine using JAVA, Spring 3.0 and JSON. In the next part you will learn how to add flex application to consume the services. Step one &#8211; Getting all required libraries: Google App Engine SDK, Spring and [...]<p><a href="http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json-flex-part-1/87">Goolge app engine + java + spring + REST + JSON + Flex &#8211; Part 1</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<h3>Objective</h3>
<p>Once you are finished with this article, you will be able to implement REST services in Google Apps Engine using JAVA, Spring 3.0 and JSON. In the next part you will learn how to add flex application to consume the services.</p>
<h3>Step one &#8211; Getting all required libraries: Google App Engine SDK, Spring and JSON serializer</h3>
<p>If you are using Eclipse (my favorite IDE), you can<a href="http://code.google.com/eclipse/docs/download.html" target="_blank"> install google plugin from here</a>. If you choose to go another route, you need to download the sdk, create the project and join us back in the next section. Once you install the plugin, restart eclipse and you can see these buttons</p>
<p><img class="size-full wp-image-103" title="Google App Engine Project Button" src="http://www.wetfeetblog.com/wp-content/uploads/2009/10/new_project_button.png" alt="Google App Engine Project Button" width="382" height="122" /></p>
<p>you can proceed to the next section.</p>
<h3>Step Two &#8211; Create new project</h3>
<p>Go to new project wizzard<span id="more-87"></span></p>
<p><img class="alignnone size-full wp-image-106" title="new project wizzard" src="http://www.wetfeetblog.com/wp-content/uploads/2009/10/new_project_wizzard.png" alt="new project wizzard" width="283" height="299" /></p>
<p>and select Web application project under google. you should get to the project settings screen</p>
<p><img class="alignnone size-full wp-image-107" title="project step 1" src="http://www.wetfeetblog.com/wp-content/uploads/2009/10/project_step_1.png" alt="project step 1" width="438" height="565" /></p>
<p>choose your project name (can be anything, not necessarily you google application name) and you base package, click finish and you should see the following project structure:</p>
<p><img class="alignnone size-full wp-image-108" title="project step 2" src="http://www.wetfeetblog.com/wp-content/uploads/2009/10/project_step_2.png" alt="project step 2" width="383" height="295" /></p>
<h3>Step Three &#8211; Add Spring Support</h3>
<p>Go to <a href="http://www.springsource.org">springsource.org</a> and download latest 3.0 release, unzip it into a folder where you keep your java libs. Copy the following Jars into war\WEB-INF\llib directory:</p>
<ol>
<li>org.springframework.asm-3.0.x.jar</li>
<li>org.springframework.beans-3.0.x.jar</li>
<li>org.springframework.context-3.0.x.jar</li>
<li>org.springframework.core-3.0.x.jar</li>
<li>org.springframework.expression-3.0.x.jar</li>
<li>org.springframework.oxm-3.0.x.jar</li>
<li>org.springframework.web-3.0.x.jar</li>
<li>org.springframework.web.servlet-3.0.x.jar</li>
</ol>
<p>Also grab your favorite version of <a href="http://logging.apache.org/log4j/1.2/index.html">log4j.jar</a> and <a href="http://commons.apache.org/downloads/download_logging.cgi">commons-logging-1.1.1.jar</a>. The trick with the commons-logging is to rename it to something like commons-fix-logging-1.1.1.jar, google app engine replaces this jar with its own version with crippled packages, by providing different name we keep both versions and make spring happy. Once you copied the jars, open project preferences and add those jars to the build library path.</p>
<p><img class="alignnone size-full wp-image-119" title="project step 3" src="http://www.wetfeetblog.com/wp-content/uploads/2009/10/project_step_3.png" alt="project step 3" width="392" height="334" /></p>
<p>Now lets get to the fun part &#8211; configuring spring.</p>
<p>Lets get rid of the generated servlet &#8211; just delete it. And open web.xml. My generated one looks like this:</p>
<pre>
<div class="codesnip-container" >
<div class="xml codesnip" style="font-family:monospace;"><span class="sc3"><span class="re1">&lt;?xml</span> <span class="re0">version</span>=<span class="st0">&quot;1.0&quot;</span> <span class="re0">encoding</span>=<span class="st0">&quot;utf-8&quot;</span><span class="re2">?&gt;</span></span>
<span class="sc3"><span class="re1">&lt;web-app</span> <span class="re0">xmlns:xsi</span>=<span class="st0">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span class="sc3"><span class="re0">xmlns</span>=<span class="st0">&quot;http://java.sun.com/xml/ns/javaee&quot;</span></span>
<span class="sc3"><span class="re0">xmlns:web</span>=<span class="st0">&quot;http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot;</span></span>
<span class="sc3"><span class="re0">xsi:schemaLocation</span>=<span class="st0">&quot;http://java.sun.com/xml/ns/javaee</span>
<span class="sc3">http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot;</span> <span class="re0">version</span>=<span class="st0">&quot;2.5&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;servlet<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;servlet-name<span class="re2">&gt;</span></span></span>Rest_json_flex<span class="sc3"><span class="re1">&lt;/servlet-name<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;servlet-class<span class="re2">&gt;</span></span></span>com.lureto.rjf.Rest_json_flexServlet<span class="sc3"><span class="re1">&lt;/servlet-class<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/servlet<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;servlet-mapping<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;servlet-name<span class="re2">&gt;</span></span></span>Rest_json_flex<span class="sc3"><span class="re1">&lt;/servlet-name<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;url-pattern<span class="re2">&gt;</span></span></span>/rest_json_flex<span class="sc3"><span class="re1">&lt;/url-pattern<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/servlet-mapping<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;welcome-file-list<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;welcome-file<span class="re2">&gt;</span></span></span>index.html<span class="sc3"><span class="re1">&lt;/welcome-file<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/welcome-file-list<span class="re2">&gt;</span></span></span>
<span class="sc3"><span class="re1">&lt;/web-app<span class="re2">&gt;</span></span></span></div>
</div>
</pre>
<p>The final version should look like this:</p>
<pre>
<div class="codesnip-container" >
<div class="xml codesnip" style="font-family:monospace;"><span class="sc3"><span class="re1">&lt;?xml</span> <span class="re0">version</span>=<span class="st0">&quot;1.0&quot;</span> <span class="re0">encoding</span>=<span class="st0">&quot;utf-8&quot;</span><span class="re2">?&gt;</span></span>
<span class="sc3"><span class="re1">&lt;web-app</span> <span class="re0">xmlns:xsi</span>=<span class="st0">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span class="sc3"><span class="re0">xmlns</span>=<span class="st0">&quot;http://java.sun.com/xml/ns/javaee&quot;</span></span>
<span class="sc3"><span class="re0">xmlns:web</span>=<span class="st0">&quot;http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot;</span></span>
<span class="sc3"><span class="re0">xsi:schemaLocation</span>=<span class="st0">&quot;http://java.sun.com/xml/ns/javaee</span>
<span class="sc3">http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot;</span> <span class="re0">version</span>=<span class="st0">&quot;2.5&quot;</span><span class="re2">&gt;</span></span>

&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;context-param<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;param-name<span class="re2">&gt;</span></span></span>log4jConfigLocation<span class="sc3"><span class="re1">&lt;/param-name<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;param-value<span class="re2">&gt;</span></span></span>/WEB-INF/log4j.properties<span class="sc3"><span class="re1">&lt;/param-value<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/context-param<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;listener<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;listener-class<span class="re2">&gt;</span></span></span>org.springframework.web.util.Log4jConfigListener<span class="sc3"><span class="re1">&lt;/listener-class<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/listener<span class="re2">&gt;</span></span></span>

&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;servlet<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;servlet-name<span class="re2">&gt;</span></span></span>rest-json-flex<span class="sc3"><span class="re1">&lt;/servlet-name<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;servlet-class<span class="re2">&gt;</span></span></span>org.springframework.web.servlet.DispatcherServlet<span class="sc3"><span class="re1">&lt;/servlet-class<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;load-on-startup<span class="re2">&gt;</span></span></span>1<span class="sc3"><span class="re1">&lt;/load-on-startup<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/servlet<span class="re2">&gt;</span></span></span>

&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;servlet-mapping<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;servlet-name<span class="re2">&gt;</span></span></span>rest-json-flex<span class="sc3"><span class="re1">&lt;/servlet-name<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;url-pattern<span class="re2">&gt;</span></span></span>/api/*<span class="sc3"><span class="re1">&lt;/url-pattern<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/servlet-mapping<span class="re2">&gt;</span></span></span>

&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;welcome-file-list<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;welcome-file<span class="re2">&gt;</span></span></span>index.html<span class="sc3"><span class="re1">&lt;/welcome-file<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/welcome-file-list<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; 
<span class="sc3"><span class="re1">&lt;/web-app<span class="re2">&gt;</span></span></span></div>
</div>
</pre>
<p>As you can see I have moved log4j.properties from src to WEB-INF location. I like all my config files in one place, you can leave it in src or move it somewhere else, just adjust the path accordingly.  Second section defines Spring dispatcher servlet, then we map this servlet to /api/* path. All requests with this pattern will get routed to spring dispathcer servlet.</p>
<h3>Step Four &#8211; Add JSON support</h3>
<p>I looked around for good java JSON library and found couple good candidates. Since Spring uses Jackson JSON I decided to go the same route. <a href="http://jackson.codehaus.org/">Grab the library from here</a> and put jackson-core-1.2.1.jar and jackson-mapper-1.2.1.jar file into projects WEB-INF/lib directory. Choose your favorite license when you downloading the jars.</p>
<h3>Step Five &#8211; Configure Spring</h3>
<p>To configure Spring servlet we need to create servletname-servlet.xml file for spring bean configuration. Our file has to be named <b>rest-json-flex-servlet.xml</b>.</p>
<pre>
<div class="codesnip-container" >
<div class="xml codesnip" style="font-family:monospace;"><span class="sc3"><span class="re1">&lt;?xml</span> <span class="re0">version</span>=<span class="st0">&quot;1.0&quot;</span> <span class="re0">encoding</span>=<span class="st0">&quot;UTF-8&quot;</span><span class="re2">?&gt;</span></span>
<span class="sc3"><span class="re1">&lt;beans</span> <span class="re0">xmlns</span>=<span class="st0">&quot;http://www.springframework.org/schema/beans&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp;<span class="re0">xmlns:xsi</span>=<span class="st0">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span class="sc3"> &nbsp; &nbsp;<span class="re0">xmlns:p</span>=<span class="st0">&quot;http://www.springframework.org/schema/p&quot;</span> </span>
<span class="sc3"> &nbsp; &nbsp;<span class="re0">xmlns:context</span>=<span class="st0">&quot;http://www.springframework.org/schema/context&quot;</span></span>
<span class="sc3"> &nbsp; &nbsp;<span class="re0">xsi:schemaLocation</span>=<span class="st0">&quot;</span>
<span class="sc3"> &nbsp; &nbsp; &nbsp; &nbsp;http://www.springframework.org/schema/beans </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp; &nbsp;http://www.springframework.org/schema/beans/spring-beans-3.0.xsd</span>
<span class="sc3"> &nbsp; &nbsp; &nbsp; &nbsp;http://www.springframework.org/schema/context </span>
<span class="sc3"> &nbsp; &nbsp; &nbsp; &nbsp;http://www.springframework.org/schema/context/spring-context-3.0.xsd&quot;</span><span class="re2">&gt;</span></span>

&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;context:component-scan</span> <span class="re0">base-package</span>=<span class="st0">&quot;com.lureto.rjf&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; &nbsp; 
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">class</span>=<span class="st0">&quot;org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;messageConverters&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;list<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;ref</span> <span class="re0">bean</span>=<span class="st0">&quot;jsonHttpMessageConverter&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/list<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/property<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/bean<span class="re2">&gt;</span></span></span>

&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">id</span>=<span class="st0">&quot;jsonHttpMessageConverter&quot;</span> &nbsp;<span class="re0">class</span>=<span class="st0">&quot;org.springframework.http.converter.json.MappingJacksonHttpMessageConverter&quot;</span> <span class="re2">/&gt;</span></span>

&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">class</span>=<span class="st0">&quot;com.lureto.rjf.spring.MyContentNegotiatingViewResolver&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;mediaTypes&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;map<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;entry</span> <span class="re0">key</span>=<span class="st0">&quot;json&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;application/json&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/map<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/property<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;defaultContentType&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;application/json&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;defaultViews&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;list<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">class</span>=<span class="st0">&quot;com.lureto.rjf.spring.JsonView&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/list<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/property<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/bean<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; 
<span class="sc3"><span class="re1">&lt;/beans<span class="re2">&gt;</span></span></span></div>
</div>
</pre>
<p>First we tell String to scan &#8220;com.lureto.rjf&#8221; package for any bean annotations.</p>
<pre>
<div class="codesnip-container" >
<div class="xml codesnip" style="font-family:monospace;"><span class="sc3"><span class="re1">&lt;context:component-scan</span> <span class="re0">base-package</span>=<span class="st0">&quot;com.lureto.rjf&quot;</span><span class="re2">/&gt;</span></span></div>
</div>
</pre>
<p>Next we define message converter to convert messages sent to the server into beans using MappingJacksonHttpMessageConverter</p>
<pre>
<div class="codesnip-container" >
<div class="xml codesnip" style="font-family:monospace;"><span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">class</span>=<span class="st0">&quot;org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;messageConverters&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;list</span> <span class="re0">id</span>=<span class="st0">&quot;beanList&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;ref</span> <span class="re0">bean</span>=<span class="st0">&quot;jsonHttpMessageConverter&quot;</span><span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/list<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/property<span class="re2">&gt;</span></span></span>
<span class="sc3"><span class="re1">&lt;/bean<span class="re2">&gt;</span></span></span>
<span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">id</span>=<span class="st0">&quot;jsonHttpMessageConverter&quot;</span> &nbsp;<span class="re0">class</span>=<span class="st0">&quot;org.springframework.http.converter.json.MappingJacksonHttpMessageConverter&quot;</span> <span class="re2">/&gt;</span></span></div>
</div>
</pre>
<p>The last part tells spring to render messages sent from the server to the client using JsonView. Here we need to do some bug fixing, as you may have noticed the classes used are from &#8220;com.lureto.rjf.spring&#8221; and not from &#8220;org.springframework.web&#8221;.</p>
<pre>
<div class="codesnip-container" >
<div class="xml codesnip" style="font-family:monospace;"><span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">class</span>=<span class="st0">&quot;com.lureto.rjf.spring.MyContentNegotiatingViewResolver&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;mediaTypes&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;map<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;entry</span> <span class="re0">key</span>=<span class="st0">&quot;json&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;application/json&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/map<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/property<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;defaultContentType&quot;</span> <span class="re0">value</span>=<span class="st0">&quot;application/json&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;property</span> <span class="re0">name</span>=<span class="st0">&quot;defaultViews&quot;</span><span class="re2">&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;list<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;bean</span> <span class="re0">class</span>=<span class="st0">&quot;com.lureto.rjf.spring.JsonView&quot;</span> <span class="re2">/&gt;</span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/list<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/property<span class="re2">&gt;</span></span></span>
<span class="sc3"><span class="re1">&lt;/bean<span class="re2">&gt;</span></span></span></div>
</div>
</pre>
<p><b>MyContentNegotiatingViewResolver.java</b> &#8211; at the moment of writing this the 3.0.0.RC1 version of ContentNegotiatingViewResolver has <a href="http://jira.springframework.org/browse/SPR-6163">bug with the list being singleton</a>.</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">package</span> <span class="co2">com.lureto.rjf.spring</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">java.util.Arrays</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">java.util.List</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">javax.servlet.http.HttpServletRequest</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">org.springframework.http.MediaType</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">org.springframework.web.servlet.view.ContentNegotiatingViewResolver</span><span class="sy0">;</span>

<span class="kw1">public</span> <span class="kw1">class</span> MyContentNegotiatingViewResolver <span class="kw1">extends</span> ContentNegotiatingViewResolver <span class="br0">&#123;</span>

&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">protected</span> List<span class="sy0">&lt;</span>MediaType<span class="sy0">&gt;</span> getMediaTypes<span class="br0">&#40;</span>HttpServletRequest request<span class="br0">&#41;</span> <span class="br0">&#123;</span> 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<span class="sy0">&lt;</span>MediaType<span class="sy0">&gt;</span> result <span class="sy0">=</span> <span class="kw1">super</span>.<span class="me1">getMediaTypes</span><span class="br0">&#40;</span>request<span class="br0">&#41;</span><span class="sy0">;</span> 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>result.<span class="me1">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">==</span> 1<span class="br0">&#41;</span> 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result <span class="sy0">=</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aarrays+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Arrays</span></a>.<span class="me1">asList</span><span class="br0">&#40;</span>result.<span class="me1">get</span><span class="br0">&#40;</span>0<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span> 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> result<span class="sy0">;</span> 
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; 
<span class="br0">&#125;</span></div>
</div>
</pre>
<p><b>JsonView.java</b> &#8211; is a copy org.springframework.web.servlet.view.json.MappingJacksonJsonView with one change:</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;">@Override
<span class="kw1">protected</span> <span class="kw4">void</span> renderMergedOutputModel<span class="br0">&#40;</span>Map<span class="sy0">&lt;</span>String, Object<span class="sy0">&gt;</span> model,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpServletRequest request,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpServletResponse response<span class="br0">&#41;</span> <span class="kw1">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Exception</span></a> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; model <span class="sy0">=</span> filterModel<span class="br0">&#40;</span>model<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; JsonGenerator generator <span class="sy0">=</span> objectMapper.<span class="me1">getJsonFactory</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">createJsonGenerator</span><span class="br0">&#40;</span>response.<span class="me1">getWriter</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>prefixJson<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; generator.<span class="me1">writeRaw</span><span class="br0">&#40;</span><span class="st0">&quot;{} &amp;&amp; &quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; objectMapper.<span class="me1">writeValue</span><span class="br0">&#40;</span>generator, model<span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>I had to change response.getOutputStream() to response.getWriter(), since jetty&#8217;s implementation of setting content type and encoding uses writer, if you try grabbing a stream after that, you will get an exception.</p>
<h3>Step Six &#8211; Lets write a little bit of code</h3>
<p>First lets define a model object that we will send across the wire. Here is my User.java class:</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">public</span> <span class="kw1">class</span> User <span class="br0">&#123;</span>

&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">long</span> id<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> email<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> name<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">long</span> getId<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> id<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> setId<span class="br0">&#40;</span><span class="kw4">long</span> id<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">id</span> <span class="sy0">=</span> id<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> getEmail<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> email<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> setEmail<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> email<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">email</span> <span class="sy0">=</span> email<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> getName<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> name<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">void</span> setName<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> name<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">name</span> <span class="sy0">=</span> name<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>And implement the controller to send and receive data. Here is my UserController.java:</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">package</span> <span class="co2">com.lureto.rjf</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">java.io.IOException</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">java.util.ArrayList</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">java.util.List</span><span class="sy0">;</span>

<span class="kw1">import</span> <span class="co2">org.apache.log4j.Logger</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">org.springframework.stereotype.Controller</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">org.springframework.web.bind.annotation.ModelAttribute</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">org.springframework.web.bind.annotation.RequestBody</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">org.springframework.web.bind.annotation.RequestMapping</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">org.springframework.web.bind.annotation.RequestMethod</span><span class="sy0">;</span>

@Controller
@RequestMapping<span class="br0">&#40;</span><span class="st0">&quot;/user&quot;</span><span class="br0">&#41;</span>
<span class="kw1">public</span> <span class="kw1">class</span> UserController <span class="br0">&#123;</span>

&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">private</span> <span class="kw1">static</span> Logger logger <span class="sy0">=</span> Logger.<span class="me1">getLogger</span><span class="br0">&#40;</span> UserController.<span class="kw1">class</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; @ModelAttribute<span class="br0">&#40;</span><span class="st0">&quot;users&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; @RequestMapping<span class="br0">&#40;</span>value <span class="sy0">=</span> <span class="st0">&quot;/&quot;</span>, method <span class="sy0">=</span> RequestMethod.<span class="me1">GET</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> List<span class="sy0">&lt;</span>User<span class="sy0">&gt;</span> listUsers<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw1">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">IOException</span></a> <span class="br0">&#123;</span> &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<span class="sy0">&lt;</span>User<span class="sy0">&gt;</span> users <span class="sy0">=</span> <span class="kw1">new</span> ArrayList<span class="sy0">&lt;</span>User<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; User user <span class="sy0">=</span> <span class="kw1">new</span> User<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; user.<span class="me1">setId</span><span class="br0">&#40;</span>10001<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; user.<span class="me1">setEmail</span><span class="br0">&#40;</span><span class="st0">&quot;user.one@gmail.com&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; user.<span class="me1">setName</span><span class="br0">&#40;</span><span class="st0">&quot;User UNO&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; users.<span class="me1">add</span><span class="br0">&#40;</span>user<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> users<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; @ModelAttribute<span class="br0">&#40;</span><span class="st0">&quot;user&quot;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; @RequestMapping<span class="br0">&#40;</span>value <span class="sy0">=</span> <span class="st0">&quot;/&quot;</span>, method <span class="sy0">=</span> RequestMethod.<span class="me1">POST</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">public</span> User saveUser<span class="br0">&#40;</span> @RequestBody User user <span class="br0">&#41;</span> <span class="kw1">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">IOException</span></a> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; logger.<span class="me1">debug</span><span class="br0">&#40;</span>user<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> user<span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>Lest go step my step through this and look at what all these little thing do.<br />
We have marked our class as <b>@Controller</b> which indicates to sprign framework that this is a controller class. <b>@RequestMapping(&#8220;/user&#8221;)</b> indicates that this controller will handle requests that start with /user, /api/user to be precise, since spring servlet is configured to handle /api/* mappings.</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;">@ModelAttribute<span class="br0">&#40;</span><span class="st0">&quot;users&quot;</span><span class="br0">&#41;</span>
@RequestMapping<span class="br0">&#40;</span>value <span class="sy0">=</span> <span class="st0">&quot;/&quot;</span>, method <span class="sy0">=</span> RequestMethod.<span class="me1">GET</span><span class="br0">&#41;</span>
<span class="kw1">public</span> List<span class="sy0">&lt;</span>User<span class="sy0">&gt;</span> listUsers<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw1">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">IOException</span></a> <span class="br0">&#123;</span></div>
</div>
</pre>
<p>Here we define a methow which will be invoked when /api/user/ path gets called with GET method. If you running standard config <a href="http://localhost:8080/api/user/">http://localhost:8080/api/user/</a> would be the url to hit in the browser. <b>@ModelAttribute(&#8220;users&#8221;)</b> tells spring to put the object returned by this method into users model attribute for the view. Since we have wired everything in xml configuration, if we return the object it will be rendered by default view which serializes beans into JSON strings.<br />
Our method for receiving json objects looks like this:</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;">@ModelAttribute<span class="br0">&#40;</span><span class="st0">&quot;user&quot;</span><span class="br0">&#41;</span>
@RequestMapping<span class="br0">&#40;</span>value <span class="sy0">=</span> <span class="st0">&quot;/&quot;</span>, method <span class="sy0">=</span> RequestMethod.<span class="me1">POST</span><span class="br0">&#41;</span>
<span class="kw1">public</span> User saveUser<span class="br0">&#40;</span> @RequestBody User user <span class="br0">&#41;</span> <span class="kw1">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">IOException</span></a> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; logger.<span class="me1">debug</span><span class="br0">&#40;</span>user<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> user<span class="sy0">;</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p><b>@RequestBody</b> annotation tells Spring framework to take request body and convert it into a bean using message converter. We have a single message converter defined in xml configuration file to be MappingJacksonHttpMessageConverter. </p>
<h3> Conclusion </h3>
<p>As you can see there is little code to write. In next Spring release the 2 bugs we fixed ourselves will probably be fixed, so this project will have only 2 source files, 3 xml files and 2 properties files. All the work is done by Spring framework, we just need to worry about the business logic and model.<br />
<a href="http://www.wetfeetblog.com/wp-content/uploads/2009/10/rest-json-flex-part1.zip">Full project can be downloaded from here</a>.<br />
In the <a href="http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json-flex-part-2/147">next part we will add flex project</a>, so we can read the json object returned and post user object to the server.</p>
<h3> UPDATE &#8211; October 27, 2009 </h3>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;">Uncaught exception from servlet
java.<span class="me1">lang</span>.<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Anullpointerexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">NullPointerException</span></a>
&nbsp; &nbsp; &nbsp; &nbsp; at com.<span class="me1">google</span>.<span class="me1">apphosting</span>.<span class="me1">runtime</span>.<span class="me1">security</span>.<span class="me1">shared</span>.<span class="me1">RuntimeVerifier</span>.<span class="me1">isInspectable</span><span class="br0">&#40;</span>RuntimeVerifier.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">302</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; at com.<span class="me1">google</span>.<span class="me1">apphosting</span>.<span class="me1">runtime</span>.<span class="me1">security</span>.<span class="me1">shared</span>.<span class="me1">intercept</span>.<span class="me1">java</span>.<span class="me1">lang</span>.<span class="me1">Class_</span>.<span class="me1">getEnclosingMethod</span><span class="br0">&#40;</span>Class_.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">237</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; at org.<span class="me1">codehaus</span>.<span class="me1">jackson</span>.<span class="me1">map</span>.<span class="me1">util</span>.<span class="me1">ClassUtil</span>.<span class="me1">isLocalType</span><span class="br0">&#40;</span>ClassUtil.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">88</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; at org.<span class="me1">codehaus</span>.<span class="me1">jackson</span>.<span class="me1">map</span>.<span class="me1">deser</span>.<span class="me1">BeanDeserializerFactory</span>.<span class="me1">isPotentialBeanType</span><span class="br0">&#40;</span>BeanDeserializerFactory.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">613</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; at org.<span class="me1">codehaus</span>.<span class="me1">jackson</span>.<span class="me1">map</span>.<span class="me1">deser</span>.<span class="me1">BeanDeserializerFactory</span>.<span class="me1">createBeanDeserializer</span><span class="br0">&#40;</span>BeanDeserializerFactory.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">61</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; at org.<span class="me1">codehaus</span>.<span class="me1">jackson</span>.<span class="me1">map</span>.<span class="me1">deser</span>.<span class="me1">StdDeserializerProvider</span>._createDeserializer<span class="br0">&#40;</span>StdDeserializerProvider.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">248</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; at org.<span class="me1">codehaus</span>.<span class="me1">jackson</span>.<span class="me1">map</span>.<span class="me1">deser</span>.<span class="me1">StdDeserializerProvider</span>._createAndCacheValueDeserializer<span class="br0">&#40;</span>StdDeserializerProvider.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">181</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; at org.<span class="me1">codehaus</span>.<span class="me1">jackson</span>.<span class="me1">map</span>.<span class="me1">deser</span>.<span class="me1">StdDeserializerProvider</span>.<span class="me1">findValueDeserializer</span><span class="br0">&#40;</span>StdDeserializerProvider.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">100</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; at org.<span class="me1">codehaus</span>.<span class="me1">jackson</span>.<span class="me1">map</span>.<span class="me1">ObjectMapper</span>._findRootDeserializer<span class="br0">&#40;</span>ObjectMapper.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">1069</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; at org.<span class="me1">codehaus</span>.<span class="me1">jackson</span>.<span class="me1">map</span>.<span class="me1">ObjectMapper</span>._readMapAndClose<span class="br0">&#40;</span>ObjectMapper.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">1002</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; at org.<span class="me1">codehaus</span>.<span class="me1">jackson</span>.<span class="me1">map</span>.<span class="me1">ObjectMapper</span>.<span class="me1">readValue</span><span class="br0">&#40;</span>ObjectMapper.<span class="me1">java</span><span class="sy0">:</span><span class="nu0">818</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; ...... <span class="me1">SNIP</span> ......</div>
</div>
</pre>
<p>After deploying to app engine I found out that jackson json library is using some unsupported introspection calls and had to make a fix inside jackson library to make it run inside google app engine. I have updated the jars inside the project to catch the exception and let parser to continue on its way. Here is the fix:</p>
<pre>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">public</span> <span class="kw1">static</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> isLocalType<span class="br0">&#40;</span>Class<span class="sy0">&lt;?&gt;</span> type<span class="br0">&#41;</span>
<span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">try</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// one more: method locals, anonymous, are not good:</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>type.<span class="me1">getEnclosingMethod</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">!=</span> <span class="kw2">null</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="st0">&quot;local/anonymous&quot;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/* But how about non-static inner classes? Can't construct
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* easily (theoretically, we could try to check if parent
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* happens to be enclosing... but that gets convoluted)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>type.<span class="me1">getEnclosingClass</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">!=</span> <span class="kw2">null</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="sy0">!</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amodifier+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Modifier</span></a>.<span class="me1">isStatic</span><span class="br0">&#40;</span>type.<span class="me1">getModifiers</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="st0">&quot;non-static member class&quot;</span><span class="sy0">;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">catch</span> <span class="br0">&#40;</span> &nbsp;<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Exception</span></a> exc <span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">null</span><span class="sy0">;</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p><a href="http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json-flex-part-1/87">Goolge app engine + java + spring + REST + JSON + Flex &#8211; Part 1</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json-flex-part-1/87/feed</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>Goolge app engine + java + spring + REST + JSON</title>
		<link>http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json/85</link>
		<comments>http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json/85#comments</comments>
		<pubDate>Wed, 07 Oct 2009 12:20:50 +0000</pubDate>
		<dc:creator>Tomas Mazukna</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[Google App Engine]]></category>
		<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[json]]></category>

		<guid isPermaLink="false">http://www.wetfeetblog.com/?p=85</guid>
		<description><![CDATA[I have been inching to try out google app engine and its capabilities, but did not wan to give up the spring. So to day I am starting on my prototype involving Goolge app engine + java + spring + REST + JSON. The end product will be spring mvc app running in google app [...]<p><a href="http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json/85">Goolge app engine + java + spring + REST + JSON</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.wetfeetblog.com/wp-content/uploads/2009/10/binary_code_3.jpg" alt="binary_code_3" title="binary_code_3" class="float-right" /></p>
<p>I have been inching to try out google app engine and its capabilities, but did not wan to give up the spring. So to day I am starting on my prototype involving Goolge app engine + java + spring + REST + JSON.</p>
<p>The end product will be spring mvc app running in google app engine handling GET, POST, PUT and DELETE http requests with JSON objects in and out utilizing JDO and seesions. This will give me a platform to write clients in Objective C for Ipod/Iphone (aka Coco Touch), Android, Flex, Ajax and maybe couple more platforms.</p>
<p>I am writing this while I am downloading Spring 3.0RC1. Will try to give daily updates if I can. If my prototype works as expected (all the research points to yes), I will have sample project available for download. There will be some trouble getting Flex to talk REST, since it only supports GET and POST and like most Adobe development stuff is not of the greatest maturity&#8230;&#8230;. (opening new tab to check if we have new version of flex&#8230; still beta). Anyhow, Flex REST part will be interesting, I have already done some hacking in that area and it can be done &#8211; I have to write http client by hand, but who does not like an interesting challenge?</p>
<p>So here you have it. Next update when I have Spring 3 mvc inside google app engine up and running.</p>
<p><a href="http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json-flex-part-1">The part 1 is now online.</a></p>
<p><a href="http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json-flex-part-2/147">The part 2 is now online.</a></p>
<p><a href="http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json-flex-part-3/185">The part 3 is now online</a></p>
<p><a href="http://www.wetfeetblog.com/google-app-engine-java-spring-rest-mapping-exceptions-to-errors-for-json/212">The part 4 is now online</a></p>
<p><a href="http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json/85">Goolge app engine + java + spring + REST + JSON</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.wetfeetblog.com/goolge-app-engine-java-spring-rest-json/85/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Using Microsoft Ad to Create and Authenticate Users</title>
		<link>http://www.wetfeetblog.com/spring-ldap-microsoft-ad/60</link>
		<comments>http://www.wetfeetblog.com/spring-ldap-microsoft-ad/60#comments</comments>
		<pubDate>Fri, 30 Jan 2009 15:38:38 +0000</pubDate>
		<dc:creator>Tomas Mazukna</dc:creator>
				<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[ldap]]></category>
		<category><![CDATA[microsoft ad]]></category>

		<guid isPermaLink="false">http://www.wetfeetblog.com/?p=60</guid>
		<description><![CDATA[I spent pas couple days digin in microsoft AD and spring LDAP. The job at hand was not pleasant, but after lots of pocking and test I managed to get the dag on thing to work. I can add/edit/delete/change password for a User stored in AD.  Here is the source code for the class: package [...]<p><a href="http://www.wetfeetblog.com/spring-ldap-microsoft-ad/60">Using Microsoft Ad to Create and Authenticate Users</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p><strong>I spent pas couple days digin in microsoft AD and spring LDAP. The job at hand was not pleasant, but after lots of pocking and test I managed to get the dag on thing to work. I can add/edit/delete/change password for a User stored in AD.  Here is the source code for the class:</strong></p>
<p><span id="more-60"></span></p>
<pre>package com.trx.wfm.model.dao;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.ModificationItem;

import org.apache.log4j.Logger;
import org.springframework.ldap.InvalidAttributeValueException;
import org.springframework.ldap.NameAlreadyBoundException;
import org.springframework.ldap.NameNotFoundException;
import org.springframework.ldap.OperationNotSupportedException;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.LdapTemplate;

import com.trx.wfm.exception.DuplicateUserException;
import com.trx.wfm.exception.LdapSaveException;
import com.trx.wfm.exception.PasswordStrengthException;
import com.trx.wfm.model.User;

public class LdapUserDAO {

    private LdapTemplate ldapTemplate;
    private boolean ldapReadonly;
    private String ldapBase;

	protected static final Logger logger = Logger.getLogger(LdapUserDAO.class);

    public LdapUserDAO() {
    }

    @SuppressWarnings("unchecked")
	public List getAllUsers() {
        return ldapTemplate.search("", "(objectclass=user)",new UserAttributeMapper() );
    }

    @SuppressWarnings("unchecked")
	public User findUser( String email ) {
        List lusers = ldapTemplate.search("", "(&amp;(objectClass=user)(userPrincipalName="+email+"))",new UserAttributeMapper() );
        if( lusers.size() &gt; 0 )
        	return lusers.get(0);
        return null;
    }

    public String getLdapBase() {
		return ldapBase;
	}

	public void setLdapBase(String ldapBase) {
		this.ldapBase = ldapBase;
	}

	public void setLdapTemplate(LdapTemplate ldapTemplate) {
        this.ldapTemplate = ldapTemplate;
    }

	public boolean isLdapReadonly() {
		return ldapReadonly;
	}

	public void setLdapReadonly(boolean ldapReadonly) {
		this.ldapReadonly = ldapReadonly;
	}

	public void deleteUser(User luzer) throws LdapSaveException {
		try {
		    DistinguishedName userDN = userToDistinguishedName( luzer );
		    ldapTemplate.unbind(userDN);
		} catch ( Exception exc ) {
			logger.error( "deleteUser()", exc);
			throw new LdapSaveException( exc.getMessage() );
		}
	}

	public void createUser(User luzer) throws DuplicateUserException, PasswordStrengthException, LdapSaveException {
		try {
		    Attributes personAttributes = new BasicAttributes();

		    personAttributes.put( "objectclass", "person" );
		    personAttributes.put( "objectclass", "user" );
		    personAttributes.put( "givenName", luzer.getFirstName() );
		    personAttributes.put( "userPrincipalName", luzer.getEmailAddress() );
		    personAttributes.put( "sn", luzer.getLastName());
		    personAttributes.put( "description", "Created via WFM 5.0 Flex app" );
		    personAttributes.put( "sAMAccountName", luzer.getFirstName().toUpperCase()+ "." + luzer.getLastName().toUpperCase() );
		    personAttributes.put( "userAccountControl", "512" ); /// 512 = normal luser

		    // PASSWORD stuff.....
		    personAttributes.put("unicodepwd", encodePassword( luzer.getPassword() ) );

		    // Set up user distinguished name and clreate it.
		    DistinguishedName newUserDN = userToDistinguishedName( luzer );
		    ldapTemplate.bind(newUserDN, null, personAttributes);

		} catch ( InvalidAttributeValueException exc ) {
			logger.error( "createUser()", exc);
			throw new LdapSaveException( exc.getMessage() );
		} catch ( NameAlreadyBoundException exc ) {   /// USER EXISTS....
			logger.error( "createUser()", exc);
			throw new DuplicateUserException(  "User ["+ luzer.getEmailAddress() + "] allready exists in AD." );
		} catch ( NameNotFoundException exc ) {
			logger.error( "createUser()", exc);
			throw new LdapSaveException( exc.getMessage() );
		} catch ( OperationNotSupportedException exc ) {  // CAN NOT ADD USER
			logger.error( "createUser()", exc);
			throw new PasswordStrengthException( exc.getMessage() );
		} catch ( Exception exc ) {
			logger.error( "createUser()", exc);
			throw new LdapSaveException( exc.getMessage() );
		}
	}

	public void changePassword( User luzer ) throws PasswordStrengthException {
		try {
		    ModificationItem repitem = new ModificationItem( DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("unicodepwd", encodePassword( luzer.getPassword() )) );
		    DistinguishedName userDN = userToDistinguishedName( luzer );
		    ldapTemplate.modifyAttributes( userDN, new ModificationItem[] { repitem } );
		} catch ( Exception exc ) {
			logger.error( "changePassword()", exc);
			throw new PasswordStrengthException( exc.getMessage() );
		}
	}

	public void updateUser( User luzer ) throws LdapSaveException {
		try {
		    ModificationItem repitem1 = new ModificationItem( DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("givenName", luzer.getFirstName()) );
		    ModificationItem repitem2 = new ModificationItem( DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("sn", luzer.getLastName()) );
		    ModificationItem repitem3 = new ModificationItem( DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("userPrincipalName", luzer.getEmailAddress()) );
		    DistinguishedName userDN = userToDistinguishedName( luzer );
		    ldapTemplate.modifyAttributes( userDN, new ModificationItem[] { repitem1, repitem2, repitem3 } );
		} catch ( Exception exc ) {
			logger.error( "updateUser()", exc);
			throw new LdapSaveException( exc.getMessage() );
		}
	}

	private byte[] encodePassword(String password) throws UnsupportedEncodingException {
		String newQuotedPassword = "\"" + password + "\"";
		return newQuotedPassword.getBytes("UTF-16LE");
	}	

	private DistinguishedName userToDistinguishedName( User luzer ) {
		return new DistinguishedName( "cn=user_"+luzer.getUserId() );
	}

	public class UserAttributeMapper implements AttributesMapper{

	    public Object mapFromAttributes(Attributes attributes) throws NamingException {
	        User user = new User();

	        user.setFirstName( attributes.get("givenName").get().toString() );
	        user.setLastName( attributes.get("sn").get().toString() );
	        user.setEmailAddress( attributes.get("userPrincipalName").get().toString() );

//        	logger.debug( " ====== ATTRIBUTES ============ " );
//	        NamingEnumeration list = attributes.getAll();
//	        while( list.hasMore() ) {
//	        	Attribute attr = list.next();
//	        	String output = " Attribute ID [" + attr.getID() + "] values [" ;
//	        	NamingEnumeration vals = attr.getAll();
//	        	while( vals.hasMore() )
//	        		output = output + "{" + vals.next().toString()+"},";
//	        	output = output + "]";
//	        	logger.debug( output );
//	        }
//
//        	logger.debug( " ============================== " );

	        return user;
	    }

	}

}</pre>
<p><strong>Some key sticking points I wan to point out:</strong></p>
<p><strong>- Check your existing users in AD to make sure you provide all required attributes</strong></p>
<p><strong>- OperationNotSupportedException usually indicates that something required is missing or password requirements are not met. If you are getting this exception change userAccountControl value to 2 &#8211; user dissabled. This will allow user creation with out password verification.</strong></p>
<p><strong>- Password has to bent to AD in unicode format.</strong></p>
<p><strong>Here is portion of the spring config that pertains to the bean:</strong></p>
<pre>	&lt;bean id="ldapContextSource" class="org.springframework.ldap.core.support.LdapContextSource"&gt;
		&lt;property name="url" value="${ldap.url}" /&gt;
		&lt;property name="base" value="${ldap.base}" /&gt;
		&lt;property name="userDn" value="${ldap.admin.bind.userdn}" /&gt;
		&lt;property name="password" value="${ldap.admin.bind.password}" /&gt;
	&lt;/bean&gt;

	&lt;bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate"&gt;
		&lt;property name="contextSource" ref="ldapContextSource" /&gt;
	&lt;/bean&gt;

	&lt;bean id="ldapUserDAO" class="com.trx.wfm.model.dao.LdapUserDAO"&gt;
		&lt;property name="ldapTemplate" ref="ldapTemplate" /&gt;
		&lt;property name="ldapReadonly" value="${ldap.readonly}" /&gt;
		&lt;property name="ldapBase" value="${ldap.base}" /&gt;
	&lt;/bean&gt;</pre>
<p><strong>ldap.base points to the OU where the users are created. This simplifies the operations, since I am checking for users only on this branch of the tree and adding them there too.</strong></p>
<p><strong>And on more point &#8211; to change anythign in AD you need to go via secure connection ldaps://your.ldap.server:636. Most likly you will get an certificate error, since my server&#8217;s is self signed, so use keytool provided in java home/bin directory to import security certificates into key store. Make sure you specify the path of they keystore file! I did it wihout the keystore file and my app was still getting exceptions. You can provide they keystore location to java as VM startup parameter (RTFM).</strong></p>
<p><a href="http://www.wetfeetblog.com/spring-ldap-microsoft-ad/60">Using Microsoft Ad to Create and Authenticate Users</a> is a post from: <a href="http://www.wetfeetblog.com">Wet Feet - Online Marketing and Technology Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.wetfeetblog.com/spring-ldap-microsoft-ad/60/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

