Spring HTTP Status 400 - 客户端发送的请求在语法上不正确(添加日期输入时) [英] Spring HTTP Status 400 - The request sent by the client was syntactically incorrect (when adding date input)

查看:1645
本文介绍了Spring HTTP Status 400 - 客户端发送的请求在语法上不正确(添加日期输入时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在jsp表单上添加日期字段时,我收到此错误,但没有它就可以正常工作。

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

请求

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

部分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>

部分 Task.java 组件

@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;

部分任务控制器

@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";
}

知道这是什么意思吗?

提前致谢!

有关错误的新额外信息
我添加了一些验证注释,并获得了有关错误的新信息。

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


字段'日期对象'任务'中的字段错误':被拒绝的价值
[01/01/2001];代码
[typeMismatch.task.date,typeMismatch.date,typeMismatch.java.util.Date,typeMismatch];
参数
[org.springframework.context.support.DefaultMessageSourceResolvable:
codes [task.date,date];参数[];默认消息[日期]]; default
message [无法将类型'java.lang.String'
的属性值转换为属性'date'的必需类型'java.util.Date';嵌套
异常是
org.springframework.core.convert.ConversionFailedException:无法
从类型java.lang.String转换为
@ org.springframework.format.annotation类型。 DateTimeFormat java.util.Date
for value '01 / 01/2001';嵌套异常是
java.lang.IllegalArgumentException:无法解析'01 / 01/2001']

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参数(idTaskCategory,idTaskPriority,idXXX)不匹配任务字段。 (这不是导致问题的原因,但它不起作用。当你更改名称以使它们匹配时,问题是你的请求包含id,但你的任务期望对象。所以你需要让任务期望id,或者你需要注册一些转换器)

  • 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(我认为这是问题),我希望日期格式/转换器不接受提交的日期格式。将 @DateTimeFormat(pattern =yyyy-MM-dd)添加到所有日期字段。

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.


我认为一个问题可能是您使用http方法 GET 。 GET请求使用URL查询字符串(之后的内容)发送参数。
但是URL的总长度受浏览器,聊天,网络服务器的技术限制。
因此问题的一个原因可能是,如果您有很多参数或长值(例如长描述),则URL会变长。
这是正确的,但不是原因

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)

所以我建议使用http方法 POST 。 - 使用 POST 是改变服务器上某些内容的更好的动词( http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html )。

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

这篇关于Spring HTTP Status 400 - 客户端发送的请求在语法上不正确(添加日期输入时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆