Spring HTTP Status 400 - The request sent by the c

2020-03-08 06:20发布

问题:

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/docreatetask?task=hi+there&description=stucked+here&date=2014-02-21&idTaskCategory=1&idTaskPriority=1&idTaskState=1&pending=1&idUser=0&idUser_responsible=0&evaluation=aaaaaa

Partly form.jsp

<form method="GET"
    action="${pageContext.request.contextPath}/docreatetask">
    <table>
        <tr>
            <td>Task</td>
            <td><input name="task" type="text" /></td>
        </tr>
        <tr>
            <td>Description</td>
            <td><textarea name="description"></textarea></td>
        </tr>
        <tr>
            <td>Date</td>
            <td><input name="date" type="date"/></td>
        </tr>

Partly Task.java component

@Component("task")
public class Task {

    private long id;
    private String task;
    private String description;
    private Date date;
    private Date deadline;
    private Category category;
    private Priority priority;
    private State state;
    private User user;
    private User userResponsible;
    private String evaluation;
    private Date timestamp;
    private int pending;

Partly Task controller

@RequestMapping("/createtask")
public String createTask(Model model) {
    List<Category> categories = taskService.getCategories();
    List<Priority> priorities = taskService.getPriorities();
    List<State> states = taskService.getStates();
    List<User> users = taskService.getUsers();

    model.addAttribute("categories",categories);
    model.addAttribute("priorities",priorities);
    model.addAttribute("states",states);
    model.addAttribute("users",users);

    return "createtask";
}


@RequestMapping(value="/docreatetask", method=RequestMethod.GET)
public String doCreateTask(Model model, Task task) {
    System.out.println(">TaskController doCreateTask " + task);

    return "taskcreated";
}

Any idea what this can be?

Thanks in advance!


New extra information about the error I added some validations annotations and I got new information about the error.

Field error in object 'task' on field 'date': rejected value [01/01/2001]; codes [typeMismatch.task.date,typeMismatch.date,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [task.date,date]; arguments []; default message [date]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @org.springframework.format.annotation.DateTimeFormat java.util.Date for value '01/01/2001'; nested exception is java.lang.IllegalArgumentException: Unable to parse '01/01/2001']

回答1:

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) .



回答2:

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));
}