title: Redirect Post Method to Get Method in Spring with ModelMap
body: <p>So, I've the following code</p>
  <pre><code>    @RequestMapping(value = &quot;/api/store&quot;)
      @ResponseBody
      public ModelMap doSomething(
          HttpServletResponse response,
          @RequestParam String a,
          @RequestParam(defaultValue = &quot;false&quot;) final boolean x,
      ) {
          ModelMap model = new ModelMap();
          if (x) {
            throw new throw new Exception(&quot;You already submitted your data!&quot;);
          }
          model.put(&quot;a&quot;, a);
          return model;
      }
  
  </code></pre>
  <p>now the scenario is, that when the user fill out the form and clicks on submit, the request goes through this mapping and it working perfectly, and the x will be assigned to <em>true</em>.</p>
  <p>Now the problem: when the user refreshes the page, another POST Method will be sent to this mapping, and hence an error will be shown to the user (because of the if statement).</p>
  <p>Now I think, I have to use the PRG Pattern, and redirect the user to GET (from POST), but the examples were mostly using ModelAndView, and since I'm using ModelMap, I'm lost, how to implement it using ModelMap.</p>
  <p>This backend API is then connected to some JS Frontend.</p>
  <p>I assume, that I have to create two methods, one GET Method and one POST Method, in the GET Method I have to implement the logic and everything and in POST I've to simply redirect to the GET Method.</p>
  <p>Update:</p>
  <p>I added the following get method:</p>
  <pre><code>@GetMapping(value = &quot;/api/store&quot;)
  public ModelAndView doSomethingGet(
      HttpServletResponse response,
      @RequestParam final String code,
      @RequestParam(defaultValue = &quot;false&quot;) final boolean x
  ) {
      return new ModelAndView(&quot;redirect:/api/store&quot;);
  }
  </code></pre>
  <p>and I've also changed the post method to this:</p>
  <pre><code>    @PostMapping(value = &quot;/api/store&quot;)
      public ModelMap doSomething(
          HttpServletResponse response,
          @RequestParam String a,
          @RequestParam(defaultValue = &quot;false&quot;) final boolean x,
      ) {
          ModelMap model = new ModelMap();
          if (x) {
            throw new throw new Exception(&quot;You already submitted your data!&quot;);
          }
          model.put(&quot;a&quot;, a);
          return model;
      }
  
  </code></pre>