Spring MVC data Binding 400 error

Source: Internet
Author: User

Scenario: When you use bind data in a method, you open the link and a 400 error occurs.

@RequestMapping(value"editItemSubmit")    publiceditItemSubmit(int id, Items item) {        itemService.updateItemFromId(id, item);        return"redirect:queryItemlList";    }
publicclass Items {    privateint id;    private String name;    privatefloat price;    private String pic;    private Date createtime;    private String detail;

Note: There is a date type in my bean, but spring MVC does not convert the string type of the previous paragraph to the sorted date type, so this error occurs.

WORKAROUND: Use custom parameter bindings (Converter converter mode)

    1. Write the converter of the response to implement the fixed interface:
 The first parameter is the original type, the second parameter is the converted type public  class   Customdateconverter  implements  converter  <string , date  > { @Override            Span class= "Hljs-keyword" >public  Date convert (String s) {try  { return  new         SimpleDateFormat ( "Yyyy-mm-dd HH:mm:ss" ). Parse (s);        } catch  (parseexception e) {e.printstacktrace ();    } return  null ; }}

Configure the Format conversion Service Factory Bean:formattingconversionservicefactorybean in the Springmvc.xml configuration file

id="conversionService"class="org.springframework.format.support.FormattingConversionServiceFactoryBean">        <propertyname="converters">            <list>                class="xyz.zhulei.spring_mybatis.controller.convert.CustomDateConverter"/>            </list>        </property>    </bean>

Then add:

<mvc:annotation-driven conversion-service="conversionService"/>

When you're done, you can automatically complete the string-to-date conversion.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Spring MVC data Binding 400 error

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.