Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
This is a complete working Spring MVC REST application. The only thing unspecified are the imports and the Maven pom.xml. Deploy this class as a .war using Spring MVC 3.2.x (and, specifically, the spring-webmvc library) and you're done!
* <p>This will be picked up in a Servlet 3 environment like Apache Tomcat 7.
* <p>This replaces <code>web.xml</code>.
public class MvcRestApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override protected Class<?> [] getRootConfigClasses() {
return new Class<?>[]{};
@Override protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{ RestConfiguration.class };
@Override protected String[] getServletMappings(){ return new String[]{"/"}; }
* Installs Spring MVC and all the required support for JSON assuming
* Jackson's on the CLASSPATH
class RestConfiguration {
// that's all!
* A simple Spring MVC REST controller. The returned value (Customer) will
* be converted into JSON if the client signals that it can read JSON (using the
* <CODE>ACCEPT</CODE> header.)
class StatusController {
@RequestMapping(value= "/customers/{customerId}", method= RequestMethod.GET)
public @ResponseBody Customer loadCustomer(@PathVariable long customerId ) {
// return a POJO Customer somehow (perhaps look it up in a database?)
return someCustomer ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.