SyntaxHighlighter

Saturday, April 6, 2013

Spring Web MVC - without XML

Spring Web MVC - without XML


how to configure a Spring Web MVC app without any XML thanks to Servlet 3.0.

here's the controller;

package de.incompleteco.spring.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/simple")
public class SimpleController {

 @RequestMapping(method=RequestMethod.GET)
 public @ResponseBody String get() throws Exception {
  return "hello world";
 }
 
}


here's the @Configuration class

package de.incompleteco.spring.web;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@EnableWebMvc
@ComponentScan("de.incompleteco.spring.web")
public class WebAppConfig {
 
}

here's the Servlet 3.0 compliant bootstrap

package de.incompleteco.spring.web;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class Bootstrap implements WebApplicationInitializer {

 @Override
 public void onStartup(ServletContext servletContext) throws ServletException {
  AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext();
  mvcContext.register(WebAppConfig.class);
  //register
  ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(mvcContext));
  dispatcher.setLoadOnStartup(1);
  dispatcher.addMapping("/app/*");
 }

}


and here's the JUnit test

package de.incompleteco.spring.web.controller;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.context.WebApplicationContext;

import de.incompleteco.spring.web.WebAppConfig;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes={WebAppConfig.class})
public class SimpleControllerTest {

 @Resource
 private WebApplicationContext context;
 
 @Test
 public void testGet() throws Exception {
  //setup
  MockMvc mvc = webAppContextSetup(context).build();
  //execute
  mvc.perform(get("/simple")).andExpect(status().isOk()).andExpect(content().string("hello world"));
 }

}

and here's the sample up on github

https://github.com/incomplete-code/spring-web-config

No comments:

Post a Comment