Spring 4 - HTTP Status 400, Required parameter is not present

24,269

Solution 1

You may try with @ModelAttribute (Visit ModelAttribute question in SO to get clear understanding about it)

@RequestMapping(value = "/save", method = RequestMethod.POST)
public String save(@ModelAttribute("employeeDTO") EmployeeDTO employeeDTO){
    return "saved";
}

I used this in spring mvc 3.1

Solution 2

Use @RequestBody to map all entire body content (Example: JSON) to your DTO object. Use @ModelAttribute for map all the form post parameters to DTO object.

Solution 3

As mentioned in the previous answers, @ModelAttirube is a part of your fix, but, to have the values actually bind to the model attribute, you'll have to add the name attributes on your form, like this

<form:form action="save" name="employeeDTO" method="POST">
    <label for="name">Name</label><input id="name" name="name" type="text" required><br>
    <label for="surname">Surname</label><input id="surname" name="surname" type="text" required><br>
    <label for="email">E-mail</label><input id="email" type="email" name="email" required><br>
    <label for="salary">Salary</label><input id="salary" type="number" name="salary" required><br>
    <input type="submit" value="Save">
</form:form>
Share:
24,269
Radek Anuszewski
Author by

Radek Anuszewski

Software developer, frontend developer in AltConnect, mostly playing with AngularJS and, recently, BackboneJS / MarionetteJS. I have a blog on Medium.com, where I am writer in Frontend Weekly.

Updated on November 09, 2020

Comments

  • Radek Anuszewski
    Radek Anuszewski over 3 years

    I have Spring form in index.jsp:

    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
    <html>
    <body>
    <form:form action="save" name="employeeDTO" method="POST">
            <label for="name">Name</label><input id="name" type="text" required><br>
            <label for="surname">Surname</label><input id="surname" type="text" required><br>
            <label for="email">E-mail</label><input id="email" type="email" required><br>
            <label for="salary">Salary</label><input id="salary" type="number" required><br>
            <input type="submit" value="Save">
    </form:form>
    </body>
    </html>
    

    In WorkController.java I try to map form submit (at this moment, it doesn't do anything with data):

    @Controller
    public class WorkController {
    
        @RequestMapping(value = "/save", method = RequestMethod.POST)
        public String save(@RequestParam EmployeeDTO employeeDTO){
            return "saved";
        }
    }
    

    But I got HTTP 400 Status: Required EmployeeDTO parameter 'employeeDTO' is not present with description: The request sent by the client was syntactically incorrect.

    There is EmployeeDTO.java:

    public class EmployeeDTO implements Serializable, DTO {
        private Long id;
        private String name;
        private String surname;
        private String email;
        private Double salary;
    
        public EmployeeDTO(){}
    
        public EmployeeDTO(Long id, String name, String surname, String email, Double salary){
            this.id = id;
            this.name = name;
            this.surname = surname;
            this.email = email;
            this.salary = salary;
        }
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getSurname() {
            return surname;
        }
    
        public void setSurname(String surname) {
            this.surname = surname;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public Double getSalary() {
            return salary;
        }
    
        public void setSalary(Double salary) {
            this.salary = salary;
        }
    
        @Override
        public Serializable toEntity() {
            return new Employee(getId(), getName(), getSurname(), getEmail(), getSalary());
        }
    }
    

    If I remove @RequestParam EmployeeDTO employeeDTO from save method signature - it works, it redirects to saved.jsp file. Earlier, I uses @RequestParam String name, @RequestParam String surname etc to catch data from HTML forms. Is there any solution to "catch" data from Spring form as DTO object? I wolud be happy if anbyody decides to help me - thank you in advance.