Spring 3 MVC pass param and respond via XML

I was looking for a way to accept a basic POST / GET and return an XML (or JSON or open format) response. The gist is; I want to write the interface between my client (javascript / flex / another client) and my services (java / php / whatever you want) and be able to swap out any of these things, but not have to recode the other end. And do it using simple web protocols, not AMF where a client library or something ‘more’ is needed. Simple, simple, simple.

War File: http://www.wookets.com/code/java/SpringXmlService.war

Eclipse Project: http://www.wookets.com/code/java/SpringXmlService/

Sorry there is no live demo… I could stick it up on my EC2, but I figure that won’t be up for more than a few months before it gets removed.

After downloading and sending to Tomcat try:

http://localhost:8080/SpringXmlService/login?name=joe

http://localhost:8080/SpringXmlService/login

References:

http://static.springsource.org/spring/docs/3.1.0.M1/spring-framework-reference/html/mvc.html

http://blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/

Source dump:



HelloService.java

 package com.wookets.demo.services;  
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.oxm.xstream.XStreamMarshaller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.wookets.demo.dto.HelloResponse;
@Controller
public class HelloService {
@Autowired
private XStreamMarshaller objectXmlMarshaller;
@RequestMapping(value = “/login”)
public @ResponseBody String handleLogin(@RequestParam(defaultValue=“world”) String name) {
HelloResponse response = new HelloResponse();
response.message = “Hello ” + name;
return objectXmlMarshaller.getXStream().toXML(response);
}
}

HelloResponse.java
 package com.wookets.demo.dto;  
import com.thoughtworks.xstream.annotations.XStreamAlias;
@XStreamAlias(“response”)
public class HelloResponse {
public String message;
}

web.xml
 <?xml version=“1.0” encoding=“UTF-8”?>  
<web-app xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance" xmlns=“http://java.sun.com/xml/ns/javaee" xmlns:web=“http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation=“http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id=“WebApp_ID” version=“2.5”>
<display-name>SpringXmlService</display-name>
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

spring-servlet.xml
 <?xml version=“1.0” encoding=“UTF-8”?>  
<beans xmlns=“http://www.springframework.org/schema/beans"
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance" xmlns:context=“http://www.springframework.org/schema/context"
xmlns:mvc=“http://www.springframework.org/schema/mvc"
xsi:schemaLocation=“http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!– Scans the classpath of this application for @Components to deploy as beans –>
<context:component-scan base-package=“com.wookets.*” />
<!– Configures the @Controller programming model –>
<mvc:annotation-driven />
<!– oxm integration –>
<bean id=“objectXmlMarshaller” class=“org.springframework.oxm.xstream.XStreamMarshaller”/>
</beans>

Published by using 286 words.