I’m struggling to figure out how to use $http.get in AngularJS to pass filter parameters.
The URL is:
http://localhost:8080/template/users/query;username=abcd;firstName=ding...
The RestController looks like this:
@RequestMapping(value={"/users/{query}"}, method=RequestMethod.GET)
public ResponseEntity<List<User>> queryUsers(@MatrixVariable(pathVar="query") Map<String, List<String>> filters) {
....
}
When I access the above URL directly in my browser, everything works fine. However, when I try to call it using $http.get in AngularJS, it fails.
This is the AngularJS code I'm using:
this.searchUser = function() {
var queryStr = '';
for (var key in userObj.search) {
if (userObj.search[key]) {
queryStr += ";" + key + "=" + userObj.search[key];
}
}
console.log('URL ', 'users/query' + queryStr);
if (queryStr === "") {
alert("No filters specified");
return false;
}
$http.get('users/query', angular.toJson(userObj.search)).then(function successCallback(response) {
if (response.data.errorCode) {
console.log(response);
alert(response.data.message);
} else {
console.log('Successfully queried for users ', response.data);
userObj.users = response.data;
}
}, function errorCallback(response) {
console.log('Error ', response);
alert(response.statusText);
});
};
When calling this method, I receive the following error:
errorCode: 400
message: "Failed to convert value of type [java.lang.String] to required type [long]; nested exception is java.lang.NumberFormatException: For input string: "query""
I’ve even tried passing the query string as specified in the URL above, but the same error persists. It seems like the request isn’t reaching the RestController method at all.
How can I resolve this issue?