我是Angularjs的新手,请耐心等待, 我有一个要求,我必须从AngularJS中的controller.js传递一个Object到工厂,它调用一个rest服务,并将该对象param传递给Spring Controller。我怎样才能做到这一点?我正在尝试的以下方法是给出@@ Spring控制器。
这是我的Controller.js:
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
}
function showPosition(position) {
$scope.PostLoc = {latitude: null, longitude: null };
var lat = position.coords.latitude;
var longi = position.coords.longitude;
$scope.PostLoc.latitude = lat;
$scope.PostLoc.longitude = longi;
$scope.allresultsfinal = PostFactory.alllocresults.query({tag: $scope.PostLoc});
}
这是我的服务工厂:
alllocresults: $resource('/ngdemo/web/posts/loc/:tag', {}, {
query: {method: 'GET', isArray: true, params: {tag: '@tag'} },
create: {method: 'POST'}
}),
我的Spring控制器签名:
@RequestMapping(value = "/loc/{tag}", method = RequestMethod.GET, produces = "application/json")
public
@ResponseBody
MatchResult resultsByUserLocation(@QueryParam(value = "tag") PostLoc tag) {
请帮助我解决我所缺少的问题,如何在弹簧控制器中获取物体?
非常感谢任何回复
答案 0 :(得分:1)
您在URL上传递路径变量,而不是查询参数,因此在方法签名中声明:
@RequestMapping(value = "/loc/{tag}", method = RequestMethod.GET, produces = "application/json")
public
@ResponseBody
MatchResult resultsByUserLocation(@PathVariable("tag") PostLoc tag) {
// ...
这假定(可能不正确)PostLoc
扩展java.lang.String
。请注意,您不能将对象传递给URL路径变量,只能传递给字符串。如果你想传入一个对象,你必须在请求体上执行此操作,或者只是传递一些字符串ID并从后端的模型中获取PostLoc
。