Search code examples
htmlcssspringspring-bootthymeleaf

Whitelabel Error Page (type=Method Not Allowed, status=405)


Error message:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu Mar 14 22:44:00 IST 2024 There was an unexpected error (type=Method Not Allowed, status=405). Method 'GET' is not supported. org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' is not supported at org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:265) at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:441) at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:382) at org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.getHandlerInternal(RequestMappingInfoHandlerMapping.java:126) at org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.getHandlerInternal(RequestMappingInfoHandlerMapping.java:68) at org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:505) at org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1275) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1057) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:974) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1011) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903) at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:564) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885) at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:205) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:166) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:115) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:341) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:391) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:894) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1740) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.base/java.lang.Thread.run(Thread.java:1623)

Error description :

  • I am facing issue in the create user part.

  • This is the workflow of the application : Welcome page -> User List -> create user

  • Even if , I have mentioned POST method in user_create.html , still shows this error.

How to resolve this?

Following are the coding part related to it.

User Controller:

package com.app.expensetracker.controller;
import com.app.expensetracker.dto.UserCreateRequest;
import com.app.expensetracker.entity.User;
import com.app.expensetracker.service.UserService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/user_list")
    public String getAllUsers(Model model) {
       model.addAttribute("user_list", userService.getAllUsers());
        return "user";

    }

    @PostMapping("/create")
    public  String createUser(@RequestBody @Valid UserCreateRequest userCreateRequest,Model model) throws Exception {
        model.addAttribute("user",userService.createUser(userCreateRequest));
        return "user_create";
    }

user.html:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="ISO-8859-1">
    <title>User</title>
    <link rel="stylesheet"
          href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
          integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
          crossorigin="anonymous">
</head>
<body>

<div class ="container">
    <div class = "row">
        <h1> User List </h1>
    </div>

    <div class = "row">
        <div class = "col-lg-3">
            <a th:href = "@{/user/create}" class = "btn btn-primary btn-sm mb-3"> Add User</a>
        </div>
    </div>
    <table class = "table table-striped table-bordered">
        <thead class = "table-dark">
        <tr>
            <th> First Name</th>
            <th> Last Name</th>
            <th> Categories</th>
            <th> Actions </th>
        </tr>
        </thead>

        <tbody>
        <tr th:each = "user: ${user_list}">
            <td th:text = "${user.firstName}"></td>
            <td th:text = "${user.lastName}"></td>
            <td th:text = "${user.categories}"></td>
            <td>
                <a th:href = "@{/user/update/{user_id}(user_id=${user.id})}"
                   class = "btn btn-primary">Update</a>

                <a th:href = "@{/user/delete/{user_id}(user_id=${user.id})}"
                   class = "btn btn-danger">Delete</a>

            </td>
        </tr>
        </tbody>

    </table>

</div>
</body>
</html>

user_create.html:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="ISO-8859-1">
    <title>Add User</title>
    <link rel="stylesheet"
          href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
          integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
          crossorigin="anonymous">
</head>
<body>

<br>
<br>
<div class = "container">
    <div class = "row">
        <div class ="col-lg-6 col-md-6 col-sm-6 container justify-content-center card">
            <h1 class = "text-center"> Create New User </h1>
            <div class = "card-body">
                <form action="/user/create" method="POST">
                    <div class ="form-group">
                        <label> First Name </label>
                        <input
                                type = "text"
                                name = "firstName"
                                th:field = "*{firstName}"
                                class = "form-control"
                                placeholder="Enter User First Name"
                        />
                    </div>

                    <div class ="form-group">
                        <label> Last Name </label>
                        <input
                                type = "text"
                                name = "lastName"
                                th:field = "*{lastName}"
                                class = "form-control"
                                placeholder="Enter User Last Name"
                        />
                    </div>


                    <div class = "box-footer">
                        <button type="submit" class = "btn btn-primary">
                            Submit
                        </button>
                    </div>
                </form>

            </div>
        </div>
    </div>
</div>
</body>
</html>

Solution

  • Update

    Thanks to @lane.maxwell in the comment section to resolve this issue. In user.html , the add button makes a GET request to an endpoint that defined as a POST. Hence I got the error. So modified that button to get user_create.html. In user_create.html does the POST activity.

    This worked for me to resolve the error.