我对 spring 非常陌生,但我设法从我的 javascript 中想出了这段代码
$scope.addMe = function(){
var params = {
date :111111,
username: 'thisGuy',
notetext: 'this is a new note that was just added'
};
$http.post(afHttp.baseUrl + "/blah/" + $scope.dog.id, {params: params})
.success(function(data) {
});
}
在 elclipe 中我有
@POST
@Path("/{id}")
public void getAddMssg(@PathParam("id") int id, @FormParam("date") int date, @FormParam("username") String username, @FormParam("notetext") String notetext) {
System.out.println("date: "+date);
System.out.println("username: "+ username);
System.out.println("notetext: "+ notetext);
System.out.println("id: "+ id);
}
当我在 eclipse 中查看我的控制台日志时,它说
date: 0
username: null
notetext: null
id: 123124
我做错了什么?我已经为此工作了一段时间但无济于事。有人可以帮忙吗?
哦,对不起,我忘了补充,我正在使用 angularjs。
最佳答案
将参数传递给该参数,而不是 {params:params}。 此外,将 Content-Type header 添加为第三个参数:
$http.post(afHttp.baseUrl + "/blah/" + $scope.dog.id, params, {
headers: { 'Content-Type': 'application/x-www-form-urlencoded'}})
在服务器端,使用@Consumes(MediaType.APPLICATION_FORM_URLENCODED) 进行@POST:
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
MediaType 的完全限定名称是 javax.ws.rs.core.MediaType,以防您需要导入它。
关于java - Spring POST 不返回有效数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19608508/