I have been killed by this thing in the past two days. The most hateful thing is that some projects are fine and some projects are troublesome, but I don’t know why. Just studied for a week.
That’s it. There are two methods below. The first one receives json, so it’s okay.
The latter one accepts text and then directly 400. If it is changed to @requestBody, it will be null.
/**
* 验证注册名
**/
@ResponseBody
@RequestMapping(value = "validateName", method = RequestMethod.POST)
public String userNameValidate(@RequestParam("name") String name) {
if (userServive.checkRegisterName(name)) {
return "1";
} else {
return "0";
}
}
/**
* 保存注册信息
**/
@ResponseBody
@RequestMapping(value = "/saveNewUser", method = RequestMethod.POST, consumes = "application/x-www-form-urlencoded")
public String saveRegisterInfo(@RequestParam("user") User user) {
System.out.println(user);
// User u = userServive.saveRegisterUser(user);
if (user != null) {
return "1";
} else {
return "0";
}
}
Then, look at the difference between the request headers and response headers of the two:
The previous method: Content-Type all correspond to each other, it’s okay.
Response Headers
view source
Content-Length:1
Content-Type:application/json;charset=UTF-8
Date:Mon, 25 Apr 2016 13:51:18 GMT
Proxy-Connection:keep-alive
Server:Apache-Coyote/1.1
Request Headers
view source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, lzma
Accept-Language:zh-CN,zh;q=0.8
Connection:keep-alive
Content-Length:11
Content-Type:application/x-www-form-urlencoded
But the latter one doesn’t correspond: something went wrong.
Response Headers
view source
Connection:close
Content-Language:en
Content-Length:1105
Content-Type:text/html;charset=utf-8
Date:Mon, 25 Apr 2016 14:10:17 GMT
Proxy-Connection:keep-alive
Server:Apache-Coyote/1.1
Request Headers
view source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, lzma
Accept-Language:zh-CN,zh;q=0.8
Connection:keep-alive
Content-Length:50
Content-Type:application/x-www-form-urlencoded
So, my second method above is to add a consumer or production, and even the header has been tried, but still can’t change that:
Content-Type:text/html;charset=utf-8
Still wrong, wrong, wrong, wrong, wrong. I added Jackson’s three bags
What’s going on? ?
The most hateful thing is that a few days ago, the same method worked well, but it didn’t work when I changed the vest. Hey
The request of angular post will not serialize the data, you need to modify $httpProvider