POST 使用 angular $http 和 Chrome 的 REST 客户端给出 400 Bad request 错误
POST with angular $http and Chrome's REST client gives 400 Bad request error
我正尝试在 jboss
上对我的 spring 控制器进行 REST 调用
@RequestMapping(value = "/myMethod", method = RequestMethod.POST)
public
@ResponseBody
String myMethod(@RequestBody MyClass myClass) {
String json = null;
String METHOD_NAME = "getAuditForRecordId";
Gson gson = new GsonBuilder().create();
try {
json = gson.toJson("success");
}
return json;
}
我的angularpost通话
$http.post(<URL to myMethod>, postData,
{
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
}).success(function () {
d.resolve(response);
}).error(function () {
d.reject();
}
);
失败并出现 400 错误请求错误。我的 jboss 应用程序
有一个 CORSFilter
@Override
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse response=(HttpServletResponse) resp;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Accept, Content-Type");
chain.doFilter(req, resp);
}
同一控制器内的 GET 方法工作正常。但是上面的 POST 失败了。
我的POSTJSON对象有嵌套对象。
我也尝试了来自 Chrome 的 REST 客户端的相同 POST 调用,但仍然出现相同的错误。有人可以指出我在这里犯的明显错误吗?
错误是 post 上的 url 分配在其他域中,而不是与您的项目 运行 相同的域中,您不调用 url 形成其他域。
这是我的例子:
@RequestMapping(value = "/listAll.json", params = {"page","pageSize","order","filter"}, method = RequestMethod.GET, produces={"application/json; charset=ISO-8859-1"})
@ResponseBody
public String listAll(@RequestParam("pageSize") int pageSize, @RequestParam("page") int page, @RequestParam("order") String order, @RequestParam("filter") String filters){
return this.pilmotiformService.listAllIntermediario(pageSize, page, order, filters);
}
@RequestMapping(value = "updateRecord.json", method = RequestMethod.POST, produces={"application/json; charset=ISO-8859-1"})
@ResponseBody
public String UpdateRecord(@RequestParam("cons") Long cons) throws Throwable{}
来自Angular:
myApp.service('myService', function($http, $rootScope, $routeParams) {
this.oneFunction = function() {
return $http({
method: 'GET',
url: WEB_SERVER+'/oneMethod.json',
params: {modulo: 'FMT_FORMREGI,FMT_AUDITORIA,FMT_ESTADO,FMT_ADJUNTO,PIL_USUA,PIL_MOTIVO,PIL_MOTIFORM' }
});
}
this.otherFunction = function(one, two, three) {
data = {one : one, two : two, three : three};
return $http({
method: 'POST',
url: WEB_SERVER+'/otherMethod.json',
data: data
});
}
});
Jackson 映射器是将数据转换或序列化为实体的组件。
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-lgpl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-lgpl</artifactId>
<version>1.9.13</version>
</dependency>
我不确定,但我认为你在这里缺少的是 json 这种格式
{"key" : "value"}
而您正在尝试做 "success"
所以这可能就是您所缺少的。
我可能是错的,因为我没有在 gson 上工作或 json 很多
我发现了问题。它在我随 post 请求传递的数据中。我传入的嵌套对象中有一个额外的 属性。当我更改服务器端的数据模型以包含额外的 属性 我的问题得到解决。
我正尝试在 jboss
上对我的 spring 控制器进行 REST 调用@RequestMapping(value = "/myMethod", method = RequestMethod.POST)
public
@ResponseBody
String myMethod(@RequestBody MyClass myClass) {
String json = null;
String METHOD_NAME = "getAuditForRecordId";
Gson gson = new GsonBuilder().create();
try {
json = gson.toJson("success");
}
return json;
}
我的angularpost通话
$http.post(<URL to myMethod>, postData,
{
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
}).success(function () {
d.resolve(response);
}).error(function () {
d.reject();
}
);
失败并出现 400 错误请求错误。我的 jboss 应用程序
有一个 CORSFilter@Override
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse response=(HttpServletResponse) resp;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Accept, Content-Type");
chain.doFilter(req, resp);
}
同一控制器内的 GET 方法工作正常。但是上面的 POST 失败了。
我的POSTJSON对象有嵌套对象。
我也尝试了来自 Chrome 的 REST 客户端的相同 POST 调用,但仍然出现相同的错误。有人可以指出我在这里犯的明显错误吗?
错误是 post 上的 url 分配在其他域中,而不是与您的项目 运行 相同的域中,您不调用 url 形成其他域。
这是我的例子:
@RequestMapping(value = "/listAll.json", params = {"page","pageSize","order","filter"}, method = RequestMethod.GET, produces={"application/json; charset=ISO-8859-1"})
@ResponseBody
public String listAll(@RequestParam("pageSize") int pageSize, @RequestParam("page") int page, @RequestParam("order") String order, @RequestParam("filter") String filters){
return this.pilmotiformService.listAllIntermediario(pageSize, page, order, filters);
}
@RequestMapping(value = "updateRecord.json", method = RequestMethod.POST, produces={"application/json; charset=ISO-8859-1"})
@ResponseBody
public String UpdateRecord(@RequestParam("cons") Long cons) throws Throwable{}
来自Angular:
myApp.service('myService', function($http, $rootScope, $routeParams) {
this.oneFunction = function() {
return $http({
method: 'GET',
url: WEB_SERVER+'/oneMethod.json',
params: {modulo: 'FMT_FORMREGI,FMT_AUDITORIA,FMT_ESTADO,FMT_ADJUNTO,PIL_USUA,PIL_MOTIVO,PIL_MOTIFORM' }
});
}
this.otherFunction = function(one, two, three) {
data = {one : one, two : two, three : three};
return $http({
method: 'POST',
url: WEB_SERVER+'/otherMethod.json',
data: data
});
}
});
Jackson 映射器是将数据转换或序列化为实体的组件。
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-lgpl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-lgpl</artifactId>
<version>1.9.13</version>
</dependency>
我不确定,但我认为你在这里缺少的是 json 这种格式
{"key" : "value"}
而您正在尝试做 "success"
所以这可能就是您所缺少的。
我可能是错的,因为我没有在 gson 上工作或 json 很多
我发现了问题。它在我随 post 请求传递的数据中。我传入的嵌套对象中有一个额外的 属性。当我更改服务器端的数据模型以包含额外的 属性 我的问题得到解决。