Spring HTTP Status 400 - The request sent by the client was syntactically incorrect (when adding date input)

前端 未结 2 409
故里飘歌
故里飘歌 2021-01-07 12:46

When I add a date field on the jsp form I get this error, but it works fine without it.

Request

http://localhost:8080/myTasks/docrea         


        
相关标签:
2条回答
  • 2021-01-07 12:54

    There are two probems:

    • 1 the parameters (idTaskCategory, idTaskPriority, idXXX) does not match the Task fields. (this is not the cause for your problem, but it will just not work. And when you change the names so that they match, it problem is that your request contains ids, but your Task expect objects. So you need to make the task expect ids too, or you need to register some converter)

    • 2 (I think that this is the problem), I expect that the date format / converter does not accept the submitted date format. Add @DateTimeFormat(pattern = "yyyy-MM-dd") to all date fields.

    I think one problem could be that you use the http method GET. A GET Request send the parameter by using the URL query string (the stuff after the ?). But the total length of the URL is technical restricted by browsers, chaches, webservers. So one cause for the problem could be, that the URL gets to long, if you have a lot of paramters or a "long" value (for example a long description). (this is correct, but not the cause)

    So I would recommend to use http method POST instead. -- And using POST is the better verb for an Request that changes something on the server (http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html) .

    0 讨论(0)
  • 2021-01-07 13:10

    A long time back I also faced this problem.

    I solved it using:

    @InitBinder
    
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(
                dateFormat, false));
    }
    
    0 讨论(0)
提交回复
热议问题