0

I am building a very simple crud app using Spring boot, Jpa and Thymeleaf, but I am stuck at a "Request method 'GET' not supported" problem. I get this error whenever I want to access the /add page through which I can add a new student. The snippets associated with this error are as below:

Thymeleaf form:

<h1>Form</h1>
<form action="#" th:action="@{/add}" th:object="${addStudent}" 
   method="post">
    <p>Full name: <input type="text" th:field="*{fname}" /></p>
    <p>Major: <input type="text" th:field="*{major}" /></p>

    <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>

Controller addNewStudentMethod

@PostMapping("/add")
public String addNewStudent( @ModelAttribute StudentEntity studentEntity, Model model) {

    model.addAttribute("addStudent",studentRepository.save(studentEntity) );
    return "/allstudents";
}

The error I get:

There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'GET' not supported

Thanks,

Zeusox
  • 7,708
  • 10
  • 31
  • 60

3 Answers3

2

In your controller you only have a method which has mapped to POST request "/add". You have to have a GET request mapped to a different method OR change the @PostMapping("/add") to @RequestMapping("/add").

Please note:

@PostMapping is for mapping POST request only. @GetMapping is for mapping GET request only. @RequestMapping maps all request types

Johna
  • 1,836
  • 2
  • 18
  • 29
  • I did that and it seems to work only if I replace return "/allstudents" by return "/add" which then takes me to the form to submit a new record. I originally put return "/allstudents" because I would like to return to the allstudents page after I successfully submit the form? Any suggestions? Thank you – Zeusox Jan 11 '18 at 17:31
1

change your Controller methods's @PostMapping("/any-url") to either @GetMapping("/any-url") or @RequestMapping("/any-url")

In simple words, change your above controller's method to

@RequestMapping("/add")
public String addNewStudent( @ModelAttribute StudentEntity studentEntity, Model model) {

    model.addAttribute("addStudent",studentRepository.save(studentEntity) );
    return "/allstudents";
}
Afridi
  • 6,753
  • 2
  • 18
  • 27
  • I did that and it seems to work only if I replace return "/allstudents" by return "/add" which then takes me to the form to submit a new record. I originally put return "/allstudents" because I would like to return to the allstudents page after I successfully submit the form? Any suggestions? Thank you – Zeusox Jan 11 '18 at 17:30
1

You have some issues with how you have it set up. What you may want is:

@GetMapping("/add")
public String addNewStudent(Model model) {

    model.addAttribute("studentEntity", new StudentEntity()); //create a new bean so that your form can bind the input fields to it
    return "add"; //let's say add.html this is the name of your form
}

@PostMapping("/add")
public String addNewStudent( @ModelAttribute StudentEntity studentEntity, Model model) {

  //call any service methods to do any processing here
  studentRepository.save(studentEntity);
  return "redirect:/allstudents";  //this would be your confirmation page
}

Your add.html form would have something like:

<form th:object="${studentEntity}" th:action="@{/add}" method="post" action="allstudents.html">
<!-- input fields here --->
</form>

Note that the th:object is what you added to the model in the @GetMapping method.

riddle_me_this
  • 8,575
  • 10
  • 55
  • 80
  • Thanks, that works ! However, I would like to know if there is a way to combine both the Get and Post methods into one? As in using RequestMapping, for example. – Zeusox Jan 11 '18 at 22:25
  • Not sure that I understand. One method is responding with the page the user is seeking and the other method is getting called when the user clicks the submit button, based on the method="post" on the form. So you want both behaviors in one method? – riddle_me_this Jan 12 '18 at 01:43
  • Note that `@GetMapping` and `@PostMapping` are actually shortcuts: https://stackoverflow.com/questions/39077787/difference-between-the-annotations-getmapping-and-requestmappingmethod-requ – riddle_me_this Jan 12 '18 at 01:45