我在服务器中有一个方法(春天安静):
@RequestMapping(value = "/lastQuestions", method = RequestMethod.POST)
public List<QuestionEntity> getLastQuestions(@RequestParam("count") Integer count) throws SQLException {
List<QuestionEntity> _result = _Impl.getLast(count);
return _result;
}
var _url = 'http://localhost:8080/Tabaraee/service/question/lastQuestions';
$http.post(_url,{count:10}).success(function(data, status, headers, config) {
return data;
}).error(function(data, status, headers, config) {
return data;
});
但得到错误:
"NetworkError: 400 Bad Request - http://localhost:8080/Tabaraee/service/question/lastQuestions"
回复:
<html><head><title>Apache Tomcat/7.0.57 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 400 - Required Integer parameter 'count' is not present</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>Required Integer parameter 'count' is not present</u></p><p><b>description</b> <u>The request sent by the client was syntactically incorrect.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/7.0.57</h3></body></html>
为什么?
答案 0 :(得分:2)
从angular docs
中将 count 值映射提供为字符串params - {Object。} - 字符串或对象的映射 将在url之后转到?key1 = value1&amp; key2 = value2。如果值 不是字符串,它将是JSONified。
所以你有两个选择,要么字符串化参数
$http.post(_url,JSON.stringify({count:10})).success(function(data, status, headers, config) {
return data;
}).error(function(data, status, headers, config) {
return data;
});
或者,您可以按原样保留客户端代码,在这种情况下,参数将作为JSON在请求正文中发送,因此您必须通过更改服务器端来更改服务器端以从请求正文绑定 @RequestParam 到 @RequestBody
@RequestMapping(value = "/lastQuestions", method = RequestMethod.POST)
public List<QuestionEntity> getLastQuestions(@RequestBody("count") Integer count) throws SQLException {