SpringMVC中向服务器传递时间参数时出现的问题

时间:2022-05-23 05:51:38

1. 问题描述:

  今天在SpringMVC应用中上传参数的时候遇到如下问题:

The request sent by the client was syntactically incorrect 

  这说明在提交的参数中,有的参数不符合服务器端数据要求。在排除其它参数没问题的情况下,确定是其中的时间参数除了问题。

  客户端传送的时间参数如下所示:

  aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAfwAAAAzCAIAAAD9+S2RAAAC0klEQVR4nO3cO7qiMACA0bv8WYEbYAMugUXYXijS5lPLKadwrrxCRL+LD3JOJwZJ9YMB/ToejzHGEELTNN8AbEXbtiGEGOOx5yvGKPcAW9U0Tb/7XyGEV08JgBWFELroXy/zz+fzXwC24nQ6XfLetm0X/eup4NXTA+CXXQsv+gDbJ/oABRF9gIKIPkBBRB+gIKIPUBDRByiI6AMURPQBCiL6AAURfYCCiD5AQUQfoCCiD1CQVaN/2O/+XFX17Fu7/SGxd11N36irGzulRnbHHhx1bmqL5t8fMrc7wNtZL/qH/a5X5rrqd7OuZt/q7z0Ke/8sMPz05LEHpU8PzX1Kbv69GWXOGQBvZ7XoH/a7YU57oa+rYSlH1/TdBfZ4a+515tgzl+PZE0du/tfX+S8KAG/neWv6XUUnPe1vuBS/qseRzTR+YnxSmW742bj0E8cTqKvLKWnwlQLg7T0v+l14JwlOpnPU5P87ddfX872enh8Sfb8718kTh+gDn+VZ0e/X8ZHo/yz5XEfdsbqTin56jX7Z/G9vBnhTz4n+sI0PR3/a7WRvF0T/gaWd5LFEH/gsz4h+6hboQ9EfDpkN9+3o39f8zGjRBz7L6tFPrKNkb+QOdpyu6ScGTB/5v3UjN5Hq2d8N5NeBRB/4LKtG/+cZl9T20SObtx+uGQ/K9PbW052zC0ML579oEgBvaL3o5364NLh8nrmWniyqDNOdXaEZ3AGY3A5Y9vTnoh9eiT7wWdaK/q0/PBj8S0KywMmq93bLp3Z4/IU3A+6Yf3+Y6AMf43nP6QPwcqIPUBDRByiI6AMURPQBCiL6AAURfYCCiD5AQUQfoCCiD1AQ0QcoiOgDFET0AQoi+gAFEX2AgiSi3zTNZdP5fH719AD4NafT6ZL3tm276IcQvgHYrhBCF/0Y4/ViH4CNaZomxthF/3g8xhhDCNIPsCVt24YQ+sX/H30ACvEP4gtpC5w1FRAAAAAASUVORK5CYII=" alt="" />

  而接受此参数的字段是一个Date类型的数据:

  aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAR4AAAAeCAIAAAB13OY1AAAEq0lEQVR4nO2bO3LbMBCGcS7cQzfI4ATulSPQhascwYWrsJZaNqnkho0bT2bk2jVTAASx2B8g+Iws7z9sDIILyNxP+yClupT+/v75I9SvP8mpom+tttK6aslQbZRS8eA3k/rfGxCJ7lOClki0iQQtkWgTCVoi0SYStESiTSRoiZDaN61OSjXfu8m3SHuh1b48qcNRHZ6q951WFEWqL0qddPVZMretGqVO5fO33g+5znwN5ndC66N6OKrDUR2O+uVj26WaZ3U4qofzhv/6ttKKydTLzM2/vlCTXHmNqGX5TH4uQWsd7RW1eob3R0vNfkh6i2itIUHrrrQjWsRnajM/eAlak/S10XIOql8+uvezPrhcLvZXm3odnushKLGUz80ZDtOQlerHozcyMh7uxB6Pr2w+Ohhm4W75lsaFWXCxjEauOMCFF3kcgZhDUkNzOPSu7JI9e1yopc9K+1Mnpd+QA39Wuj9VX/zkAZJgkB3BcnA/cEWyYXvEaPni0B3mGpy8GpzcXg1c0f2rl37XZdFCR+DrPVoV9VTirGNo+Qm0Bns1ETzMToRNMVqJjxZQOq5UmLGsDGxhdvx1xWjhBHTqrbeurJucu09BqzIxOY6uSWjx/dBFY2YQWjXbSUSXNRJFyFRQXZKAhBpHq2eJMxC6u3NNhoRXP5mFCJTCuclBYdY8R8GnZykq3kYSQh+v/DbmFIEptOw4QYvGsJi9vDlympyPFyrS4PHevx1IKCsLQhM8FbmvNY6oyCeElBMWXlA3hSeEtYk+QsIO2Z79FBewu32iVpin9YM8kgwgpT07iRZwbodNvl7CBvNoQfLd4ITWZZKF0Re+4YQ8Wm2lgU08mpV1ZZIp9T4XDXZdAVpRfgXmF6GVvcSGo8hCSa0F8eOwbVp55tGCqZQfTNMClJscObf7k83swchlmFm0eLU2LycsjVodTvqmoTWpIssJtw0S9cY4Wuj7nmpGG4NegheCaKGckM6hy7VVs3Uv5CbQomEq6I54taycu0W0LAWjtZSgldzPLLRs+jeGFrF2NThWr6lytLxfsoRwOVq98afq3WGGuxqgHpuTEC59bI1ZcAz0w7BhOCMhXO1XhRCtZAf8FtCCASquo1yfg24VRrbBeH3ZoX1fitbQ2ODdvxXQGtr9Z8ObCpzqIP4kuvm4LeEbifSqV8O6/zlxFnwPTwe3PC6G+0kYrSQ+EbKzxV0598rFamgljBShFVsYuvAMLdYPTHbbzbU26V11w33avENIDvjMKo1WponPQkfYb4xjTs5OqpufsIYLNv5gLafk2xgFLXPEEJiJn0eP2Mkr0RMnzp3sm/MG/Tha6HkUeq5F/hNRoOPJXmNMg7qI/ABfGb4ky2Az3I1lucIEtJjzrYtW4PTIIDX1cG6T3Q5GF0sO44dgU1/dACjgu0CZMLUdQXNzj5aBqXl3ntX6zPnWRavjfj8VrY7Qpd9a2IGg286lfG4/+c37m7Eobk1oY4hEX15lb1eNpOhlErRE30YuuI3F21RNPFGClujeRZPSXJIXpuWLm7KClujeNaBVGq9W+RXCvf2oRCS6EQlaItEmErREok0kaIlEm0jQEok20T+qFX61kOtklQAAAABJRU5ErkJggg==" alt="" />

  在Controller中使用自动装箱:

  aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAyMAAAA7CAIAAADn3ML5AAAQVUlEQVR4nO2dO3LcOBCGeR4dgeewbzDFE2yiSDoCHTia8gk2cCSmklMniuxkgnWypSo73pgb8IVHd6NBAnzp/0qBPQMCDRIEfnY3MUULAAAAAADyUGxtAAAAAADAaYHSAgAAAADIBZQWAAAAAEAuoLQAAAAAAHIBpQUAAAAAkAsoLQAAAACAXEBpAQAAAADkAkoLAAAAACAXUFoAAAAAALmA0gIAAAAAyAWUFg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y6wHAIAYVErL9DwNH/bCJUppCfUYLFNazaeHhMnyCoeLTmlR69cMpSUv9mxsKnZxCdtMG+p8GmePoLS074zJ10Jls/p6ieu+UmmF+5XW5gAqpbWdzbOdWsZdDIcWAGB9NEpLSsyKzdOSKycPp2DLRGbfa5nm6uiM+Kj1IaC0AqtgYp/W4iy1ZD4tR/Ky/ZHt1sUYtdcrhdIK9yutzTO6RFS9mc2znhocg+HRAgCsDpSWHvZ9RHECX1Fp6XJotGakeB8gxh6ND8aqLzZ6uEelFerXHpXWVjYv7aXWMwoAAGmJVVpjFhQfPRwT0iWl5dZjsKfooVs5keixJ6Xlr3HzoiVKpRWsPMqemGgXs2gqlFawAe31mtNWMFHI61damwMQjSnUyWo2L9/oAZtsAAC2IE5pTbnto8RxlI353h+vtIh6JhZmxE/vJy7IiL/VJbs9ACl35ka0qMJzlVa6N6sUNgffzYy2hy9OXA3awsC1UNgccb0UTi3S12O7dUL9SmuzjKuZiLcwNrQZ7x4CAA7KrF0erBcGvZ0XWKUl1jMoNv9v9E4Ju04oqopWWjT+PE2U9d+60qgWH/mVLzojXmWzqvuBRY1pLbSHgWOPpu/c1eDCh3yhsM0RC7rsoWKa8odGqF9JbZYhDOo2FvFyAde3OUUfobQAAFsQrbSIHbCs/as+/Rx3DR0dTtQGV249CZVW23r6b07alr9ahLYPEJbTVZSWpBCjlqg4vw6/mirs0fSdqmjWtdDYHLGiB4uGfzZT2a90NocwWyrrG5WGvonN2CMeAHBYojPiwW5hfCzk6/Tv0Z4MYIemdUh0nqG0AABbAKV1GpjsmM3euNqbPXmYuY8GiCCZnj3Z2AMAHAQorfMgvF2/iRLYmz2ZoJLfQSpCL5xE4gVHAQAgO1Ba54JQN5sqgL3Zk4dbXWLZzkTyczsOSVwyAMA6SEoLAAAAAAAsAUoLAAAAACAXUFoAAAAAALmA0gIAAAAAyMX/3jA/W2AQRgYAAAAASUVORK5CYII=" alt="" />

  原因所在: 在装箱过程中时间的参数的转化出错。

2. 问题解决

  2.1 方法一:

    在需要转变时间的Controller中添加如下代码:

@InitBinder
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws Exception {
// DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
CustomDateEditor dateEditor = new CustomDateEditor(format, true);
binder.registerCustomEditor(Date.class, dateEditor);
super.initBinder(request, binder);
}

  2.2 方法二:

    在实体类中对应的时间字段上加上如下注释:

@DateTimeFormat(pattern="yyyy-MM-dd")
private Date birthday;

    用这种方法的时候注意要引入joda-time.jar包,在Maven工程的pom.xml文件中加入如下依赖:

    

        <dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.</version>
</dependency>