0

Hi I have a Controller class in my spring boot application which has two methods that correspond to GET and POST requests for the same url, When the user makes a get request it returns an html page with a form which when submitted makes a post request to the same url, I'm able to receive the form data but the problem is that the application is not returning another page when hitting the url with post. Instead I'm getting a WhiteLable error on the browser and on the console I'm getting this error -> DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported] Below is the code snippet

   @Controller
   public class SimpleController {

    @GetMapping("/index")
    public String getRequest()  {
        return "simple.html";
    }

    @PostMapping("/index")
    public String postRequest(String firstName, String lastName) {
        System.out.println("The result is " + firstName + " " + lastName);
        System.out.println("----------------------------------");
        return "simple.html";
    }

   }

And this is what my HTML code looks like

<body>
    <form action="index" method="POST">
        <input type="text" name="firstName">
        <input type="text" name="lastName">
        <input type="submit" value="submit">
    </form>
</body>
ansme
  • 183
  • 9
  • you may have forgotten the `@RequestBody` annotation before your parameters. – GameDroids May 03 '21 at 14:41
  • 1
    However you cannot pass two "RequestBody" parameters at once. You may have to create a Class (like `Person`) which can hold `firstName` and `lastName` values. The mapping will do the rest: `public String postRequest(@RequestBody Person person)` and then `person.getFirstName()`... [This question is about that problem](https://stackoverflow.com/q/12893566/896249) – GameDroids May 03 '21 at 14:48
  • 1
    in post method try using `return "redirect:/index"` and post your result again @ansme – Bishal Gautam May 03 '21 at 16:12
  • @GameDroids Hi I tried with `@RequestBody` but it is giving me `415 Unsupported Media Type` status code, I've also edited the question and have added the `html` content as well kindly check that too. – ansme May 03 '21 at 17:17
  • @BishalGautam redirecting works! But what if I want to send some other page? For example instead of `simple.html` I want to send `success.html`, Then how should I achieve that? – ansme May 03 '21 at 17:21
  • generally we only send the prefix or file name that is in the project like `return "simple";`. If you any other views with say `success.html` than just do `return "success"`. – Bishal Gautam May 03 '21 at 17:25
  • @BishalGautam AFAIK that works when we are using `thymeleaf` in dependencies and currently I'm not using it that's why I've also specified the extension of the file, Because `return "success"`, Generates 404 without `thymeleaf`. – ansme May 03 '21 at 17:30
  • what are you using for the view? @ansme – Bishal Gautam May 03 '21 at 17:32
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/231892/discussion-between-ansme-and-bishal-gautam). – ansme May 03 '21 at 17:33

1 Answers1

1

Use return "redirect:/success.html" so that you can get your html pages. Also from our chat, please use thymeleaf which is a template engine. You can add it by adding it in your dependencies in the pom.xml file. This is what you should add inside your dependecies tag in pom.xml.

<dependency>
  <groupId>org.thymeleaf</groupId>
  <artifactId>thymeleaf</artifactId>
  <version>3.0.12.RELEASE</version>
</dependency>
ansme
  • 183
  • 9
Bishal Gautam
  • 375
  • 3
  • 16