Showing posts with label spring mvc. Show all posts
Showing posts with label spring mvc. Show all posts

Tuesday, April 28, 2009

GWT, Spring 3.0 MVC, and REST on Google App Engine / Java - Part 2

From Part 1 of GWT, Spring 3.0 MVC and REST on Gae/J posting, you should now have a spring application with REST up and running and should be deployable on google app engine.

Integrating GWT with your backend now is pretty straight forward for the most part. Restlet is a REST framework, they released a module called Restlet-GWT module. For more understanding about it and why use rest with GWT, here is a link to blog posting on release Restlet ported to GWT. I'll be referring to the Greeting demo when you create a new application with the Google eclipse plugin. In what ever you named your Entry point class, look for the function

private void sendNameToServer()

your going to replace everything from greetService.greetServer(textToServer, with this..

final Client client = new Client(Protocol.HTTP);
client.get("http://localhost:/greet.json", new Callback() {
public void onEvent(Request request, Response response) {
dialogBox.setText("Remote Procedure Call");
serverResponseLabel.removeStyleName("serverResponseLabelError");
JSONValue json;

json = JSONParser.parse(response.getEntity().getText());
JSONValue value = json.isObject().get("hello");

serverResponseLabel.setHTML(value.toString());
dialogBox.center();
closeButton.setFocus(true);
}
});

Sorry for formatting, but you can see
client.get("http://localhost:/greet.json", new Callback() {

here is where I'm calling the rest url to retrieve the json string returned from the backend and GWT already provides functionality to parse JSON. For more information on Restlet-GWT. Restlet Maven Repository.

I found an issue with running Spring MVC 3.0 on google app engine ( could be only hosted mode). GWT compiles and generates a bunch of css, and javascript files. To get the nice REST style url's all requests need to be mapped to context
( / )


<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>



The servlet mapping will match the longest matching map first. So when something like


<script type="text/javascript" language="javascript" src="/static/app/app.nocache.js"></script>



is given .. this should go directly to the mapping default servlet (above example), and not the main spring dispatcher. The issue is with jetty vs tomcat and how they handle paths. From the posting I found on stackoverflow (servlet for serving static content) "The problem is that Tomcat's default servlet does not take the ServletPath into account (so it looks for the static files in the main folder), while Jetty does (so it looks in the static folder)." Luckily the user provides the file of a servlet. In the servlet I made one change in the servlet class provided.
In the function protected String getPath(HttpServletRequest req).. I changed it to return pathInfo; and not return servletPath + pathInfo; . This changes makes the behavior like Tomcat, and so you won't have to move all your files inside of a folder called static. This basically ignores the /static/ and looks for path after that. Now in your web.xml just define new servlet, and define for static mapping.

Thats it.. You should now have your GWT front end talking to your Spring 3.0 MVC application running in hosted mode, and GAE/J. The cool thing is now you can switch between regular spring mvc application functionality (exmaple: submitting forms) using html, css and GWT. Coming up - Nightmare dependencies with Spring 3.0 M2 and Data nucleus GAE/J, GenericDao with Spring and JDO, and Spring Security.

Tuesday, April 21, 2009

GWT, Spring 3.0 MVC, and REST on Google App Engine / Java - Part 1

When googleing how to integrate GWT and Spring/Spring mvc, the most popular answer is to use the GWT- Widget/Server Library. The solutions are wrapping pojos as an RPC service or for better performance taking a spring controller and adding RPC functionality by extension.

With the intoduction of REST features in Spring MVC 3.0, this should make integration between GWT and Spring more natural. This is how to get it all working together on Google App Engine / Java.

First use Spring 3.0 M2. To include this in maven


<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.spring-library</artifactId>
<type>libd</type>
<version>3.0.0.M2</version>
</dependency>



Add the milestone repository
SpringSource Enterprise Bundle Repository - External Bundle Milestones
http://repository.springsource.com/maven/bundles/milestone
Note: ensure your using asm 2.2.3

Now in the web application you need to create the REST support with MVC. The most important feature added is the ContentNegotiatingViewResolver, this will determine which view to present from Accept header or extension, and from media type can resolve appropriate view. Probably the more important type will be for JSON support. In the blog post from Spring about REST in Spring 3 they hint at the JacksonJsonView and its inclusion in WebFlow or Spring Js, and at the time of this posting, unavailable. Anyways.. I really don't care to add web flow, or spring js to my project when using GWT. The project json-lib-ext-spring : details provides a json view that works seamlessly. Ensure that you exclude all spring libraries in your maven dependency.

In you spring configuration , it will look like this now..


<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="html" value="text/html"/>
<entry key="json" value="application/json"/>
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
</bean>





<bean id="greet" class="path.to.GreetJsonView"/>



In the GreetJsonView just :

import net.sf.json.spring.web.servlet.view.JsonView;
public class GreetJsonView extends JsonView {
}

In the future it should be as easy and changing the class that you extend when spring releases a proper JSON view.

@Controller
@RequestMapping("/greet")
public class GreetController {

@RequestMapping(value = "greet", method = RequestMethod.GET)
public String list(ModelMap modelMap) {
modelMap.put("hello","REST rocks");

return "greet";
}
}

Now when you go to /path/greet it will display the jsp or if you go to /path/greet.json it will return the json string.

For additional details on how to get Spring MVC started and configured -
The most helpful is the pet clinic demo for Spring 3 M2 found in the subversion repository.

AtomView and REST Support

Example program called Spring Finance

Thats it for part 1, in my next post, I will show how to get GWT working with your spring mvc back end through REST calls, and putting it all together to get it working nicely on GAE/J