title: When saving data to server is working for @GetMapping instead of @PostMapping
body: <p>I am new to Spring Boot and to web applications, in general. I was facing an issue with my saveStudent() method </p>
  
  <pre><code>@PostMapping("/students")
  public ModelAndView saveStudent(Student student) {
      repo.save(student);
      ModelAndView mv = new ModelAndView();
      mv.addObject("obj",student);
      mv.setViewName("addStudent");
      return mv;
  }
  </code></pre>
  
  <p>I know that if I am saving data to server HTTP uses POST method, but when retrieving data it uses GET method. In this case, since I am saving a Student instance to database, I should use PostMapping right? But to my surprise this method works if I change annotation to @GetMapping, can you please explain why?</p>
  
  <pre><code>@GetMapping("/students")
  public ModelAndView saveStudent(Student student) {
      repo.save(student);
      ModelAndView mv = new ModelAndView();
      mv.addObject("obj",student);
      mv.setViewName("addStudent");
      return mv;
  }
  </code></pre>
  
  <p>This method works fine and PostMapping does not work. But why?</p>
  
  <p>MainController</p>
  
  <pre><code>package com.firstdbproj.demo.controller;
  
  
  import java.util.List;
  import java.util.Optional;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.web.bind.annotation.GetMapping;
  import org.springframework.web.bind.annotation.PathVariable;
  import org.springframework.web.bind.annotation.PostMapping;
  import org.springframework.web.bind.annotation.RequestBody;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestParam;
  import org.springframework.web.bind.annotation.ResponseBody;
  import org.springframework.web.bind.annotation.RestController;
  import org.springframework.web.servlet.ModelAndView;
  
  import com.firstdbproj.demo.Student;
  import com.firstdbproj.demo.dao.StudentRepo;
  
  
  @RestController
  public class MainController {
  
      @Autowired
      StudentRepo repo;
  
      @RequestMapping("/")
      public ModelAndView showIndexPage() {
          ModelAndView mv = new ModelAndView();
          mv.setViewName("index");
          return mv;
      }
  
      @PostMapping("/students")
      public ModelAndView fetchStudent(Student student) {
          repo.save(student);
          ModelAndView mv = new ModelAndView();
          mv.addObject("obj",student);
          mv.setViewName("addStudent");
          return mv;
      }
  
  
      @GetMapping(path="/students/{id}")
      @ResponseBody
      public Optional&lt;Student&gt; showStudentinJSON(@PathVariable("id")int ids) {
          Optional&lt;Student&gt; obj = repo.findById(ids);
          return obj;
      }
  
  
      @RequestMapping("/showStudentUsingId")
      public ModelAndView showStudent(@RequestParam("id")int ids) {
          Student obj = repo.findById(ids).orElse(new Student(0,"null"));
          ModelAndView mv = new ModelAndView();
          mv.addObject("student",obj);
          mv.setViewName("showStudent");
          return mv;
      }
  
      @RequestMapping("/showStudentUsingName")
      public ModelAndView showStudent(@RequestParam("id")String name) {
          Student obj = repo.findByName(name);
          ModelAndView mv = new ModelAndView();
          mv.addObject("student",obj);
          mv.setViewName("showStudent");
          return mv;
      }
  
      @RequestMapping("/showAllStudent")
      public String showAllStudent() {
          List&lt;Student&gt; obj = (List&lt;Student&gt;) repo.findAllSorted();
          return obj.toString();
      }
  
      @RequestMapping("/showAllStudentWhereName")
      public String showAllStudentWhereName(@RequestParam("id")String name) {
          List&lt;Student&gt; obj = (List&lt;Student&gt;) repo.findAllWhereName(name);
          return obj.toString();
      }
  
      @RequestMapping("/getStudent")
      public ModelAndView getStudent() {
          ModelAndView mv = new ModelAndView();
          mv.setViewName("getStudent");
          return mv;
      }
  
  }
  </code></pre>
  
  <p>Student Repo</p>
  
  <pre><code>package com.firstdbproj.demo;
  
  import javax.persistence.Entity;
  import javax.persistence.Id;
  
  import org.springframework.stereotype.Component;
  
  @Entity
  public class Student {
  
      @Id
      private int id;
      private String name;
  
      public Student(){
  
      }
      public Student(int id,String name){
          this.id = id;
          this.name = name;
      }
      public String getName() {
          return name;
      }
      public void setName(String name) {
          this.name = name;
      }
      public int getId() {
          return id;
      }
      public void setId(int id) {
          this.id = id;
      }
      @Override
      public String toString() {
          return "Student [id=" + id + ", name=" + name + "]";
      }
  }
  </code></pre>
  
  <p>addStudent.jsp</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1" isELIgnored="false"%&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta charset="ISO-8859-1"&gt;
  &lt;title&gt;Insert title here&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      Hello ${obj.id}, ${obj.name}!
      &lt;form action="/"&gt;
      &lt;input type="submit" value="Add another Record!"&gt;
      &lt;input type="submit" formaction="getStudent" value="Fetch Records!"&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>