http://localhost:8081/ezsonar/npm/appportmanage/saveEditAppPortManage
The reason for the error code 400: basically it is caused by the incorrect data format passed by the front desk.
But how this format is incorrect depends on how you understand how the data is transmitted.
For example, if the back end parameter is an int, but the front end passes a NULL, then the type cannot be cast, so 400.
Now for my reason, this is a bit weird: it’s because the backend model’s unreferenced constructor was overridden by the referenced constructor and reported an error of 400.
Write down the reason for my code error 400:
Foreground code:
$.ajax({
url: SUBSYSTEM_APP_NAME + "appportmanage/saveEditAppPortManage",
type: "post",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(data),
dataType: "json",
success: function (data) {
if (data.success) {
GMS.success(data.msg);
} else {
GMS.error(data.msg, 3000);
}
}
});
First of all, the properties of the data object encapsulated in the foreground are consistent with the properties of the Java model in the background, which is not the cause of the problem. Don’t worry.
Notice, for those of you who reported 400 wrong, I’m saying that the front and back of all of my attributes are OK, but you reported 400 wrong, are you sure that your front and back objects are the same parameter types for all of your attributes?For example: a property is List< in front and background; String> ?Boolean?And so on and so forth. This is the most basic error gesture.
You’d better check the problem first and then continue to see if it is the same as the original cause of my error.
However, the Ajax type and various parameters can be matched with the background, which is not the cause of the problem.
Background code:
@RequestMapping(value = "/saveEditAppPortManage")
public @ResponseBody JsonResult saveEditRenameDetail (@RequestBody Appportmanage detail) {
LOG.debug("---------------AppportmanageController:saveEditAppPortManage---------------");
LOG.debug("---------------detail:" + detail + "---------------");
return appportmanageService.saveEditAppPortManage(detail);
}
Second, the front and back urls are also aligned, as are the attributes of the model. Then again, spring MVC’s annotation tag is fine.
The original code was OK. Or the above code, the program can work normally, run no problem.
But I made the following changes:
Modify the data model: the original model Java file is some of the properties and simple getter and setter, s then I due to business needs, to the original model to add an attribute, of course, also add the corresponding getter and setter, then demand because I added a constructor with parameters, because I’m new in other places the object model.
Then,,,
The problem arises.
I’ll just give you an error number of 400.
At first, I thought it was the problem of adding attributes, but after checking, I found it was not the problem.
The problem is the constructor for the Model Java file.
Solution: Add another no-arguments constructor to the Model Java file. The explanation is below.
As for why, it’s up to you to understand how spring MVC passes parameters between front and back.
Originally I didn’t add a constructor, each model will bring a with no parameters by default constructor, then the front desk the data format of encapsulation and attributes of the model as long as the background, and then spring MVC can own in the background, according to the model to the front desk to get data, corresponding to pack into the @ RequestBody Appportmanage detail, this parameter, is the premise of the implementation, your model has a default constructor, with no arguments, Then the system itself goes to new an object, and then it goes to load the data into it. Then you can use it.
Because, I have updated the model constructor, if you don’t write constructor with no parameters, so the original model’s own without arguments constructor, then the controler layer, is in the foreground of data obtained, to loading data, he will not according to your arguments constructor with a new object model you want, so that problems just appeared.
Then, after today’s error, you’ll know exactly how the data in the background and in the front corresponds. Originally I just thought, as long as the consistent data model is ok.
Now, you know, oh, it has to do with constructors.
Why did I delete the empty constructor?Because the IDE tells me that the constructor is useless, and I delete it. And then I was miserable. Oh, Sid!!
The reason I gave this error is a little bit deeper, because this error gives you an idea of how springMVC’s annotations work, and if you know IOC, which is what Spring talks about dependency injection and inversion of control, you’ll understand this error even better.
Read More:
- jsp: Failed to load resource: the server responded with a status of 400 (Bad Request)
- Nginx 502 Error: Failed to load resource: the server responded with a status of 502 (Bad Gateway)
- HTTP 405 Error: Failed to load resource: the server responded with a status of 405 (Method Not Allowed)
- (springmvc) Failed to load resource: the server responded with a status of 404 (Not Found)
- Failed to load resource: the server responded with a status of 403 (Forbidden)
- Failed to load resource: the server responded with a status of 503 (Service Unavailable)
- JS Ajax request is successful, the response displays “failed to load response data”
- Spring MVC error HTTP status 400 – bad request
- In the HTML page request Ajax times 400 error, solve Yii submit post form 400 error, and Ajax post request 400 problem (example code)
- Error: request failed with status code 500
- HTTP 400 error – bad request
- ASP.NET AJAX client-side framework failed to load
- IIS “Bad Request – Request Too Long. HTTP Error 400. The size of the request headers is too long.”
- Docker Nacos deployment uses container name to access 400 bad request
- Vue failed to log in. F12 reported an error: request failed with status code 404
- Failed to load resource: net::ERR_CONNECTION_REFUSED
- Solve the Google Chrome Failed to load resource: net::ERR_FAILED problem
- Chrome Failed to load resource: net::ERR_CACHE_MISS
- Smbclient error tree Connect failed: NT_ STATUS_ BAD_ NETWORK_ NAME
- Solution of Vue axios400 bad request problem