Recently Angularjs post to background 4,001 header fog without any errors.
Finally found a good article, thank the author
The solution to the "bad Request" error in SPRINGMVC (using @responsebody to process the JSON data transmitted by Ajax into the Bean)
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.
1, my solution is to change the type of the entity class JavaBean inside to a string type, in the configuration of SQL statements with database functions To_date or to_number conversion, if in Java to use this string type of date, If necessary, use the for Format=new SimpleDateFormat ("Yyyy-mm-dd"), Format.parse () to convert.
2, in fact, in the entity class can also define the date and the type of int string type member variable, so that the foreground of the form field or name corresponding to it, which also successfully turned into the 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.
3, in is also a method is in the corresponding entity class corresponding non-string type of variable setter method passed the string type, and then in the inside with SimpleDateFormat or integer conversion.
4, finally there is a method, is the entity class date attribute plus @datetimeformat (pattern= "YYYY-MM-DD") annotations, most of which can be used successfully. If this method is not available, you can continue to try the following methods: But this premise is that the foreground passes through the date in JSON rather than string form, such as the foreground is similar to $ ("#id"). Val () to get the date directly to the backstage. You need to introduce the Json.js Library of the JSON website at the front desk or the Jquery.json-2.4.js Library of jquery, and then the new Date (stringdate) If it is the former. Parsejson () to convert back to the background, If it is the latter, $.tojson (new Date (StringDate)) to the background, this way is more troublesome, there are netizens comments specifically discussed the problem, so the first way is recommended.
If you have a better way to solve this problem, but also please your mercy, leave your valuable comments, so that we all learn and grow together.
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) Error in log4j configuration file to hit part of log as info level
(2) When the parameter is passed, the parameter name uses the keyword "name" (I tried, my error is normal)
(3) to return the JSON but forgot to add @requestbody
(4) The connection behind the Ajax request forgot to add the parameter
(5) The order of parameters in the foreground is inconsistent with the order of the properties of the background entity class (I tried, my order changed without effect)
(6) This happens if you pass a parameter that is a property of a non-entity bean.
Note: Regarding the conversion of date types to each other, I have re-wrote a blog post: springmvc date type conversion problem three methods induction, I believe it will effectively solve the problems you encounter!
The solution to the "bad Request" error in SPRINGMVC (using @responsebody to process the JSON data transmitted by Ajax into the Bean)