Java angularjs$http.post和spring restful请求错误
我在服务器中有一个方法(使用spring restful):Java angularjs$http.post和spring restful请求错误,java,angularjs,spring,spring-mvc,Java,Angularjs,Spring,Spring Mvc,我在服务器中有一个方法(使用spring restful): 但获取错误: "NetworkError: 400 Bad Request - http://localhost:8080/Tabaraee/service/question/lastQuestions" 答复: <html><head><title>Apache Tomcat/7.0.57 - Error report</title><style><!--H1 {f
但获取错误:
"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>
Apache Tomcat/7.0.57-错误报告HTTP Status 400-所需整数参数“count”不存在
类型状态报告消息所需整数参数“count”不存在说明客户端发送的请求在语法上不正确。
ApacheTomcat/7.0.57
为什么?从角度文档中以字符串形式提供您的计数值映射
params–{Object.}–字符串或对象的映射
将在url后变为?key1=value1&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)
公共列表getLastQuestions(@RequestBody(“count”)整数计数)引发SQLException{
正如错误消息所说,所需的整数参数“count”不存在
我不熟悉spring,但您正在将count作为请求体发送,但试图将其作为参数接收。请求体和请求参数是不同的。重新启动,使用$http.post(_url,{count:10})对于发送参数“count”@O.R,请尝试将请求发送到http://localhost:8080/Tabaraee/service/question/lastQuestions?count=10
使用$http.post(_url,{count:10})作为发送参数“count”
<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>
$http.post(_url,JSON.stringify({count:10})).success(function(data, status, headers, config) {
return data;
}).error(function(data, status, headers, config) {
return data;
});
@RequestMapping(value = "/lastQuestions", method = RequestMethod.POST)
public List<QuestionEntity> getLastQuestions(@RequestBody("count") Integer count) throws SQLException {