当我使用Angularjs向SpringMVC发送数据时,SpringMVC无法获取数据,它是400或null对象。
所以,我尝试在浏览器中查看控制台信息,我发现RequestHeaders和ResponseHeaders之间存在差异,我将其显示如下:
response headers
Connection:close
Content-Language:en
Content-Length:1105
Content-Type:text/html;charset=utf-8
Date:Mon, 25 Apr 2016 13:52:07 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
Cookie:JSESSIONID=21DCE26028722FE4C396D8CA3D6FC4C4
我尝试更改springMVC的响应头,但它没有任何变化,仍然是这样。
@ResponseBody
@RequestMapping(value = "/saveNewUser", method = RequestMethod.POST, consumes = "application/x-www-form-urlencoded")
public String saveRegisterInfo(@RequestParam("user") User user) {
//I can't get user here. When I use @RequestBody, user is null.
//When I use @RequestParam, it's error 400 or 415.
//these days I try to learn Angularjs,I think the type of data make me crazy,just like contentType, json
System.out.println(user);
// User u = userServive.saveRegisterUser(user);
if (user != null) {
return "1";
} else {
return "0";
}
}
如何将conent-type从“text / html; charset = utf-8”更改为“application / json,text / plain”for response-headers?
答案 0 :(得分:0)
两个步骤可以帮到你;
将Jackson Mapper Asl添加到项目的依赖项
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.10</version>
</dependency>
将@ResponseBody
注释添加到控制器
然后您的控制器将响应JSON对象。