<?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; json</title>
	<atom:link href="http://www.wetfeetblog.com/tag/json/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>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 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>
	</channel>
</rss>

