0

I am not able to find a way to use angularjs's $http.get to pass filter params.

Url is:

http://localhost:8080/template/users/query;username=abcd;firstName=ding...

RestController is:

@RequestMapping(value={"/users/{query}"}, method=RequestMethod.GET)
public ResponseEntity<List<User>> queryUsers(@MatrixVariable(pathVar="query") Map<String, List<String>> filters) {
  ....
}

When I use the above Url directly in the browser it is working fine. However when I try to call the same using Angularjs's $http.get it is not working.

Angularjs code:

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 I call this method: I get the error as

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""

Even I tried to pass the query string as it is mentioned in the above URL, but still the same error. I am sure it is not even entering inside the RestController method.

How to solve this?

SVGK Raju
  • 49
  • 6

1 Answers1

0

Using get,you can not pass JSON.

You should prepare object if you want to passed it a query string.

var queryStr = {};
for (var key in userObj.search) {
    if (userObj.search[key]) {
        queryStr[key] = userObj.search[key];
    }
}

And your HTTP#GET call should be like this :

$http.get('users/query', {params: queryStr}).then(...

OR

$http({
    url: 'users/query', 
    method: "GET",
    params: queryStr
 });
RIYAJ KHAN
  • 15,032
  • 5
  • 31
  • 53
  • I modified the code as you mentioned, but still the same problem. – SVGK Raju Sep 14 '16 at 16:33
  • I see the error Exception: TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context. at Function.remoteFunction (:3:14)] in Chrome dev tools – SVGK Raju Sep 16 '16 at 04:39
  • This looks like an issue with the URL where Angularjs puts ? and & when params used. But with spring controller's MatrixVariable it expects ; and ,. Matrix variable does not work with ? and &. As of now I am not sure how to tell Angularjs to pass ; and , in the URL. If anybody know please let me know. – SVGK Raju Sep 17 '16 at 02:40