diff --git a/src/main/java/org/springframework/samples/mvc/mapping/MappingController.java b/src/main/java/org/springframework/samples/mvc/mapping/MappingController.java index cbebdcbe2..48a8759a6 100644 --- a/src/main/java/org/springframework/samples/mvc/mapping/MappingController.java +++ b/src/main/java/org/springframework/samples/mvc/mapping/MappingController.java @@ -1,64 +1,61 @@ package org.springframework.samples.mvc.mapping; -import javax.servlet.http.HttpServletRequest; - import org.springframework.http.MediaType; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletRequest; -@Controller +@RestController +@RequestMapping("/mapping") public class MappingController { - @RequestMapping("/mapping/path") - public @ResponseBody String byPath() { + @RequestMapping("/path") + public String byPath() { return "Mapped by path!"; } - @RequestMapping(value="/mapping/path/*", method=RequestMethod.GET) - public @ResponseBody String byPathPattern(HttpServletRequest request) { + @RequestMapping(value="/path/*", method=RequestMethod.GET) + public String byPathPattern(HttpServletRequest request) { return "Mapped by path pattern ('" + request.getRequestURI() + "')"; } - @RequestMapping(value="/mapping/method", method=RequestMethod.GET) - public @ResponseBody String byMethod() { + @RequestMapping(value="/method", method=RequestMethod.GET) + public String byMethod() { return "Mapped by path + method"; } - @RequestMapping(value="/mapping/parameter", method=RequestMethod.GET, params="foo") - public @ResponseBody String byParameter() { + @RequestMapping(value="/parameter", method=RequestMethod.GET, params="foo") + public String byParameter() { return "Mapped by path + method + presence of query parameter!"; } - @RequestMapping(value="/mapping/parameter", method=RequestMethod.GET, params="!foo") - public @ResponseBody String byParameterNegation() { + @RequestMapping(value="/parameter", method=RequestMethod.GET, params="!foo") + public String byParameterNegation() { return "Mapped by path + method + not presence of query parameter!"; } - @RequestMapping(value="/mapping/header", method=RequestMethod.GET, headers="FooHeader=foo") - public @ResponseBody String byHeader() { + @RequestMapping(value="/header", method=RequestMethod.GET, headers="FooHeader=foo") + public String byHeader() { return "Mapped by path + method + presence of header!"; } - @RequestMapping(value="/mapping/header", method=RequestMethod.GET, headers="!FooHeader") - public @ResponseBody String byHeaderNegation() { + @RequestMapping(value="/header", method=RequestMethod.GET, headers="!FooHeader") + public String byHeaderNegation() { return "Mapped by path + method + absence of header!"; } - @RequestMapping(value="/mapping/consumes", method=RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE) - public @ResponseBody String byConsumes(@RequestBody JavaBean javaBean) { + @RequestMapping(value="/consumes", method=RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE) + public String byConsumes(@RequestBody JavaBean javaBean) { return "Mapped by path + method + consumable media type (javaBean '" + javaBean + "')"; } - @RequestMapping(value="/mapping/produces", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE) - public @ResponseBody JavaBean byProducesJson() { + @RequestMapping(value="/produces", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE) + public JavaBean byProducesJson() { return new JavaBean(); } - @RequestMapping(value="/mapping/produces", method=RequestMethod.GET, produces=MediaType.APPLICATION_XML_VALUE) - public @ResponseBody JavaBean byProducesXml() { + @RequestMapping(value="/produces", method=RequestMethod.GET, produces=MediaType.APPLICATION_XML_VALUE) + public JavaBean byProducesXml() { return new JavaBean(); } diff --git a/src/test/java/org/springframework/samples/mvc/form/FormControllerTests.java b/src/test/java/org/springframework/samples/mvc/form/FormControllerTests.java index c919e7159..7343eb620 100644 --- a/src/test/java/org/springframework/samples/mvc/form/FormControllerTests.java +++ b/src/test/java/org/springframework/samples/mvc/form/FormControllerTests.java @@ -15,7 +15,6 @@ import org.junit.Before; import org.junit.Test; -import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; import org.springframework.web.servlet.view.InternalResourceViewResolver; @@ -52,7 +51,7 @@ public void submitSuccess() throws Exception { .param("_additionalInfo[java]", "on") .param("subscribeNewsletter", "false")) .andDo(print()) - .andExpect(status().isMovedTemporarily()) + .andExpect(status().isFound()) .andExpect(redirectedUrl("/form")) .andExpect(flash().attribute("message", "Form submitted successfully. Bound properties name='Joe', age=56, " + diff --git a/src/test/java/org/springframework/samples/mvc/redirect/RedirectControllerTests.java b/src/test/java/org/springframework/samples/mvc/redirect/RedirectControllerTests.java index cd710cf65..7798ee19e 100644 --- a/src/test/java/org/springframework/samples/mvc/redirect/RedirectControllerTests.java +++ b/src/test/java/org/springframework/samples/mvc/redirect/RedirectControllerTests.java @@ -17,7 +17,7 @@ public class RedirectControllerTests { @Before public void setup() throws Exception { this.mockMvc = standaloneSetup(new RedirectController(new DefaultFormattingConversionService())) - .alwaysExpect(status().isMovedTemporarily()).build(); + .alwaysExpect(status().isFound()).build(); } @Test