user3844950
user3844950

Reputation: 71

400 bad request spring mvc controller with application/x-www-form-urlencoded data

I have a spring mvc controller which takes application/x-www-form-urlencoded data. I am posting data to this controller from a html form and I have date fields in my form. My issue is that when I send null values for date I get error 400 bad request, when I specify date values it works fine.

Here is my spring MVC controller

@RequestMapping(value="/createorderform",method=RequestMethod.POST,headers = "content-type=application/x-www-form-urlencoded")
public void createOrder(Model model,  @ModelAttribute Order order,HttpSession session){
    System.out.println("Inside Order Controller");  
    String CustomerName;
    System.out.println("hiiiii");
    Customer customer=null;
}

When I send null date values, control is not coming inside mvc controller. How can I allow my mvc controller to take null values?

Upvotes: 0

Views: 1682

Answers (1)

user3844950
user3844950

Reputation: 71

I solved this issue by using BindingResult

public void createOrder(Model model, @Valid @ModelAttribute Order order, BindingResult errors,HttpSession session){


    if (errors.hasErrors()) {
        // error handling code goes here.
        System.out.println("inside controlelr");
      }

@Valid tells spring controller to validate Order object and BindingResult object holds the result of validation and binding the errors that may have occurred. BindingResult must come right after the model object

Upvotes: 2

Related Questions