ajax error 400 (Failed to load resource: the server responded with a status of 400 (Bad Request))

Failed to load resource: the server responded with a status of 400 (Bad Request)
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&lt 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: