[java] Spring MVC: Error 400 The request sent by the client was syntactically incorrect

This seems to be a common issue. I have gone all the answers given in SO but could not make it work.
I am trying to integrate Spring MVC+Freemarker in already existing web application. It works fine for the GET request and Freemarker Template reads java object provided by Controller without any issue.
But Form submission is not able to hit Controller method. Finally I made log4j work. Here is the error I am getting:
Error

    HandlerMethod details: 
    Controller [application.entry.controller.UserController]
    Method [public void application.entry.controller.UserController.handleSave(java.lang.String)]

    org.springframework.web.bind.MissingServletRequestParameterException: 
Required String parameter 'action' is not present


Freemarker:

<form method="POST" action="save.html">
  ------------
  <input type="submit" class="btnnew" name="saveWithoutValidation" value="Save Without Validation"></input>
  <input type="submit" class="btnnew" name="submit" value="Submit"></input>
</form>

context-root is PORTAL.
spring-servlet.xml

<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
      <property name="cache" value="true"/>
      <property name="prefix" value=""/>
      <property name="suffix" value=".ftl"/>

web.xml

<servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

Controller

@RequestMapping(value="/save", method=RequestMethod.POST)
    public void handleSave(@RequestParam String action){

        if( action.equals("submit") ){
            System.out.println("Damn! You clicked submit");
        }
        else if( action.equals("saveWithoutValidation") ){
           System.out.println("Sweet! You want no string attached.");
        }

    }

For logs I have tried to add log4j.logger.org.springframework.web=DEBUG to my existing log4j.properties but it didn't work.

This question is related to java spring spring-mvc

The answer is


Add BindingResult parameter in your method. For reference please my code below.

save(@ModelAttribute Employee employee,BindingResult bindingResult)


@CookieValue(value="abc",required=true) String m

when I changed required from true to false,it worked out.


Controller trying to find the "action" value in bean but according to your example you have not set any bean name of "action". try to do name="action". @RequestParam always find in the bean class.


Based on the error:

Required String parameter 'action' is not present

There needs to be a request parameter named action present in the request for Spring to map the request to your handler handleSave.

The HTML that you pasted shows no such parameter.


Your request mapping is /save, but your POST is to /save.html. Changing the POST to /save should fix it.


Please keep your

<form method="POST" action="XYZ">

@RequestMapping(value="/XYZ", method=RequestMethod.POST)
    public void handleSave(@RequestParam String action){

Your form action attribute value must match to value of @RequestMapping, So that Spring MVC can resolve it.

Also, as you told it is giving 404 after changing, for this, can you please check whether control is entering inside handleSave() method.

I think, as you are not returning any thing from handleSave() method, you have to look at it.

if it still not work, can you please post your spring logs.

Also, make sure that your request should come like

/PORTAL/save

if there is anything between like PORTAL/jsp/save the mention in @RequestMapping(value="/jsp/save")


I was facing a similar issue and found that a few fields like Date were not getting a concrete value, once given the values things worked fine. Please make sure you do not have date or any other field present on the form which needs a concrete value.


My problem was the lack of BindingResult parameter after my model attribute.

 @RequestMapping(method = RequestMethod.POST, value = "/sign-up", consumes = "application/x-www-form-urlencoded")
    public ModelAndView registerUser(@Valid @ModelAttribute UserRegistrationInfo userRegistrationInfo
                                HttpServletRequest httpRequest,
                                HttpSession httpSession) { ... } 

After I added BindingResult my controller became

@RequestMapping(method = RequestMethod.POST, value = "/sign-up", consumes = "application/x-www-form-urlencoded")
    public ModelAndView registerUser(@Valid @ModelAttribute  UserRegistrationInfo userRegistrationInfo, BindingResult bindingResult,
                                HttpServletRequest httpRequest,
                                HttpSession httpSession) { ..}

Check answer @sashok_bg @sashko_bg Mersi mnogo


Another possible cause is to have the wrong order of RequestMapping attributes. As spring doc says:

An @RequestMapping handler method can have a very flexible signatures. The supported method arguments and return values are described in the following section. Most arguments can be used in arbitrary order with the only exception of BindingResult arguments. This is described in the next section.

If you scroll down the doc, you will see that the BindingResult has to be immediatelly after the model attribute, since we can have multiple model objects per request and thus multiple bindings

The Errors or BindingResult parameters have to follow the model object that is being bound immediately as the method signature might have more than one model object and Spring will create a separate BindingResult instance for each of them so the following sample won’t work:

Here are two examples:

Invalid ordering of BindingResult and @ModelAttribute.

@RequestMapping(method = RequestMethod.POST) public String processSubmit(@ModelAttribute("pet") Pet pet, Model model, BindingResult result) { ... } Note, that there is a Model parameter in between Pet and BindingResult. To get this working you have to reorder the parameters as follows:

@RequestMapping(method = RequestMethod.POST) public String processSubmit(@ModelAttribute("pet") Pet pet, BindingResult result, Model model) { ... }


The @RequestParam String action suggests there is a parameter present within the request with the name action which is absent in your form. You must either:

  1. Submit a parameter named value e.g. <input name="action" />
  2. Set the required parameter to false within the @RequestParam e.g. @RequestParam(required=false)

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to spring

Are all Spring Framework Java Configuration injection examples buggy? Two Page Login with Spring Security 3.2.x Access blocked by CORS policy: Response to preflight request doesn't pass access control check Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified Spring Data JPA findOne() change to Optional how to use this? After Spring Boot 2.0 migration: jdbcUrl is required with driverClassName The type WebMvcConfigurerAdapter is deprecated No converter found capable of converting from type to type

Examples related to spring-mvc

Two Page Login with Spring Security 3.2.x ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean Spring 5.0.3 RequestRejectedException: The request was rejected because the URL was not normalized The type WebMvcConfigurerAdapter is deprecated RestClientException: Could not extract response. no suitable HttpMessageConverter found Spring boot: Unable to start embedded Tomcat servlet container UnsatisfiedDependencyException: Error creating bean with name 8080 port already taken issue when trying to redeploy project from Spring Tool Suite IDE Error creating bean with name 'entityManagerFactory' defined in class path resource : Invocation of init method failed Difference between the annotations @GetMapping and @RequestMapping(method = RequestMethod.GET)