SPR-5251: URI Templates in @RequestMapping

This commit is contained in:
Arjen Poutsma
2008-11-17 16:00:03 +00:00
parent a1faaad9fa
commit fe72e8a5f7
11 changed files with 313 additions and 64 deletions

View File

@@ -40,6 +40,7 @@ import org.junit.Test;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.aop.interceptor.SimpleTraceInterceptor;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.BeansException;
import org.springframework.beans.DerivedTestBean;
import org.springframework.beans.ITestBean;
import org.springframework.beans.TestBean;
@@ -63,6 +64,7 @@ import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@@ -796,6 +798,29 @@ public class ServletAnnotationControllerTests {
}
}
@Test
public void uriTemplates() throws Exception {
DispatcherServlet servlet = new DispatcherServlet() {
@Override
protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) throws BeansException {
GenericWebApplicationContext wac = new GenericWebApplicationContext();
wac.registerBeanDefinition("controller", new RootBeanDefinition(UriTemplateController.class));
wac.refresh();
return wac;
}
};
servlet.init(new MockServletConfig());
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels/42/bookings/21");
MockHttpServletResponse response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals("test-42-21", response.getContentAsString());
}
/*
* Controllers
*/
@RequestMapping("/myPath.do")
private static class MyController extends AbstractController {
@@ -1285,7 +1310,6 @@ public class ServletAnnotationControllerTests {
@Controller
public static class MethodNotAllowedController {
@RequestMapping(value="/myPath.do", method = RequestMethod.DELETE)
public void delete() {
}
@@ -1314,4 +1338,15 @@ public class ServletAnnotationControllerTests {
}
}
@Controller
public static class UriTemplateController {
@RequestMapping("/hotels/{hotel}/bookings/{booking}")
public void handle(@PathVariable("hotel") int hotel, @PathVariable int booking, HttpServletResponse response)
throws IOException {
response.getWriter().write("test-" + hotel + "-" + booking);
}
}
}