js.hrt
js.hrt

Reputation: 159

Spring MVC controller method called for GET but not for POST

Respected fellows, I have defined two methods for the URL "/persons/search" in the Controller class. One is with RequestMethod.Get and other one is with RequestMethod.POST. The method with GET request is getting called but the method with POST request is not getting called. In the browser, when I load /persons/search and GET request is sent, the view is returned successfully. However, when I fill up the search field and press submit (generating a POST), I receive a 404. I have tried to print values in the method to check if it is getting invoked or not, but the values don't print. The method is never invoked. Note that it was working fine a few days back, and I just implemented spring security in the meanwhile, which I believe shouldn't have done any harm, but then its not working. I have permitted all requests to /persons/**. Any help regarding what could be causing this issue would be appreciated. Regards

Controller:

@RequestMapping(value = "/persons/search", method = RequestMethod.GET)
public String searchPersons(Model model) {
    logger.debug("searchPersons()");
    return "users/searchPersons";

}

@RequestMapping(value = "/persons/search", method = RequestMethod.POST)
public String listPersons(@RequestParam("searchString") String searchStr, Model model) {
    logger.debug("listPersons()");
    System.out.println("Entered search");
    userService.initialize();
    institutions = userService.getInstitutions();
    model.addAttribute("persons", userService.searchPersons(searchStr));
    System.out.println("completed search");
    return "users/listPersons";
}

View:

<%@ page session="false"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

<!DOCTYPE html>
<html lang="en">

<jsp:include page="../fragments/header.jsp" />

<body>

<div class="container">

    <c:if test="${not empty msg}">
        <div class="alert alert-${css} alert-dismissible" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
            <strong>${msg}</strong>
        </div>
    </c:if>
    <h1>Search Persons</h1>

<!-- The form -->
    <spring:url value="/persons/search" var="searchPersonsActionUrl" />
    <form class="searchBar" action="${searchPersonsActionUrl}" method="POST">
      <input type="text" placeholder="Enter uid or fullname..." name="searchString">
      <button type="submit"><i class="fa fa-search"></i></button>
    </form>

</div>

<jsp:include page="../fragments/footer.jsp" />

</body>
</html>

Screenshot of browser's network tab:

enter image description here

Upvotes: 1

Views: 1764

Answers (1)

Marc Stroebel
Marc Stroebel

Reputation: 2357

newer spring security versions enable csrf by default which leads to strange errors sometimes. Try disabling by .csrf().disable() in your security config or look here: https://www.baeldung.com/spring-security-csrf

Upvotes: 3

Related Questions