The solution to the "bad Request" error in SPRINGMVC (using @responsebody to process the JSON data transmitted by Ajax into the Bean)

Source: Internet
Author: User

In today's development process, there was an error in the action in SPRINGMVC that processed the JSON data from the foreground AJAX request directly into the corresponding entity class: The bad request, the background is not an error, 400 refers to invalid requests (the request has a syntax problem or can not meet the request) , debugging for a long time to solve, deliberately recorded, and share with you.

The most common reason for this error is that the background entity class Bean does not match the type of the foreground pass , and mine is because there are member variables in the JavaBean that define the date type and the int type. Causes the converter to convert the JSON data into beans can not be converted, in fact, if the conversion with the Jsonobject.tobean method, this situation will also error.

My solution is to change the type of the JavaBean inside the entity class to a string type, which is converted with to-date or to_number when the SQL statement is configured.

You can also define a string type member variable for the date and int types in the entity class, so that the field or name in the foreground form corresponds to it, and it is successfully turned into an entity class, but after the turn, You have to assign the string type to the corresponding date or int type to the appropriate member variable in Java.

There is also a way to pass in a string type in the setter method of the corresponding non-string variable of the corresponding entity class, and then convert it with SimpleDateFormat or integer in it.

Finally, there is a simple way to add @datetimeformat (pattern= "YYYY-MM-DD") annotations to the date attribute of the entity class.

Online I looked at some of the questions, see netizens There are some other reasons for this error, summed up a few convenient users to reference:

1, log4j in the configuration file error will be part of the log hit the info level caused by
2, the parameters of the time, the parameter name used the keyword "name" (I tried, my error is normal)
3, originally want to return JSON but forgot to add @requestbody
4, the Ajax request connection behind forgot to add parameters

Reprint please indicate-java my life (Chen Leixing) Source: http://blog.csdn.net/chenleixing/article/details/43740759



Finally, seriously read the netizens, the great gods, if there is a feeling I this program ape has a place to say wrong or wrong or you have a good

mention suggestion or Idea method, You kindness alms n seconds to leave your valuable text (message) so that you, me, and the vast Program Apes faster to grow and progress ....




Error Request in SPRINGMVC resolution (@responsebody Processing of JSON data transmitted from Ajax to Bean) is a workaround

Related Article

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.