title: Forwarding or Redirecting to a @ResponseBody method with GET/POST
body: <p>Just written some code for redirecting and forwarding from one action to another action in spring mvc.Also done some permutations with GET and POST.</p>
  
  <p>My question is how things work when we redirect/forward from one action with @RequestMapping to another action having @RequestMapping and @ResponseBody.</p>
  
  <p>Below is the code snippet for controller and jsp with ajax script.Comments are mentioned on the methods which are not working with response error.</p>
  
  <pre><code>@Controller
  @RequestMapping(value = "/myMvc")
  public class MyMvcController {
  
      @RequestMapping(value = "/home", method = RequestMethod.GET)
      public String home(Model map, HttpServletRequest req) {
          return "home";
      }
  
      @RequestMapping(value = "/action1", method = RequestMethod.POST)
      public @ResponseBody String action1(Model map, HttpServletRequest req) {
  
          return "Sending response body as POST";
      }
  
      @RequestMapping(value = "/action2", method = RequestMethod.GET)
      public @ResponseBody String action2(Model map, HttpServletRequest req) {
  
          return "sending response body as GET";
      }
  
      /*****action3 to action6 Redirect******/
  
      //POST to GET
      @RequestMapping(value = "/action3", method = RequestMethod.POST)
      public String action3(Model map, HttpServletRequest req) {
  
          return "redirect:/myMvc/action2";
      }
  
      //POST to POST
      @RequestMapping(value = "/action4", method = RequestMethod.POST)
      public String action4(Model map, HttpServletRequest req) {
  
          //will not execute 
          //Request method 'GET' not supported
          return "redirect:/myMvc/action1";
      }
  
      //GET to POST
      @RequestMapping(value = "/action5", method = RequestMethod.GET)
      public String action5(Model map, HttpServletRequest req) {
  
          //will not execute 
          //Request method 'GET' not supported
          return "redirect:/myMvc/action1";
      }
  
      //GET to GET
      @RequestMapping(value = "/action6", method = RequestMethod.GET)
      public String action6(Model map, HttpServletRequest req) {
  
          return "redirect:/myMvc/action2";
      }
  
      /*****action7 to action10 Forward******/
  
      //POST to GET
      @RequestMapping(value = "/action7", method = RequestMethod.POST)
      public String action7(Model map, HttpServletRequest req) {
  
          //will not execute 
          //Request method 'POST' not supported
          return "forward:/myMvc/action2";
      }
  
      //POST to POST
      @RequestMapping(value = "/action8", method = RequestMethod.POST)
      public String action8(Model map, HttpServletRequest req) {
  
          return "forward:/myMvc/action1";
      }
  
      //GET to POST
      @RequestMapping(value = "/action9", method = RequestMethod.GET)
      public String action9(Model map, HttpServletRequest req) {
  
          //will not execute 
          //Request method 'GET' not supported
          return "forward:/myMvc/action1";
      }
  
      //GET to GET
      @RequestMapping(value = "/action10", method = RequestMethod.GET)
      public String action10(Model map, HttpServletRequest req) {
  
          return "forward:/myMvc/action2";
      }
  }
  </code></pre>
  
  <p>home.jsp file</p>
  
  <pre><code>&lt;button onclick="hitAction3()"&gt;hit action3&lt;/button&gt;&lt;br&gt;&lt;br&gt;
      &lt;button onclick="hitAction4()"&gt;hit action4&lt;/button&gt;&lt;br&gt;&lt;br&gt;
      &lt;button onclick="hitAction5()"&gt;hit action5&lt;/button&gt;&lt;br&gt;&lt;br&gt;
      &lt;button onclick="hitAction6()"&gt;hit action6&lt;/button&gt;&lt;br&gt;&lt;br&gt;
      &lt;button onclick="hitAction7()"&gt;hit action7&lt;/button&gt;&lt;br&gt;&lt;br&gt;
      &lt;button onclick="hitAction8()"&gt;hit action8&lt;/button&gt;&lt;br&gt;&lt;br&gt;
      &lt;button onclick="hitAction9()"&gt;hit action9&lt;/button&gt;&lt;br&gt;&lt;br&gt;
      &lt;button onclick="hitAction10()"&gt;hit action10&lt;/button&gt;&lt;br&gt;&lt;br&gt;
  
  &lt;script&gt;
      function hitAction3() {
          $.ajax({
                  method:'POST',
                  url: "${pageContext.request.contextPath}/myMvc/action3",
          }).done(function(data) {alert(data);});
      }
  
      function hitAction4() {
          $.ajax({
                  method:'POST',
                  url: "${pageContext.request.contextPath}/myMvc/action4",
          }).done(function(data) {alert(data);});
      }
  
      function hitAction5() {
          $.ajax({
                  method:'GET',
                  url: "${pageContext.request.contextPath}/myMvc/action5",
              }).done(function(data) {alert(data);});
      }
  
      function hitAction6() {
          $.ajax({
                  method:'GET',
                  url: "${pageContext.request.contextPath}/myMvc/action6",
              }).done(function(data) {alert(data);});
      }
  
      function hitAction7() {
          $.ajax({
                  method:'POST',
                  url: "${pageContext.request.contextPath}/myMvc/action7",
          }).done(function(data) {alert(data);});
      }
  
      function hitAction8() {
          $.ajax({
                  method:'POST',
                  url: "${pageContext.request.contextPath}/myMvc/action8",
          }).done(function(data) {alert(data);});
      }
  
      function hitAction9() {
          $.ajax({
                  method:'GET',
                  url: "${pageContext.request.contextPath}/myMvc/action9",
              }).done(function(data) {alert(data);});
      }
  
      function hitAction10() {
          $.ajax({
                  method:'GET',
                  url: "${pageContext.request.contextPath}/myMvc/action10",
              }).done(function(data) {alert(data);});
      }
  &lt;/script&gt;
  </code></pre>
  
  <p>What I already know is :</p>
  
  <p>In Redirect :A new request object is created.</p>
  
  <p>In Forward :Same request object is forwarded to next action.</p>
  
  <p>Please share some rules or information regarding this if I am missing.</p>