Salman Kazmi
Salman Kazmi

Reputation: 3248

Passing RequestParams in the URL with AngularJS to Spring MVC backend

I have a URL mapping on my Spring REST controller that expects the URL to have three optional request parmeters. I have used @RequestParam annotation to define three optional parameters in the method that I defined below. The URL works fine when I use a browser to hit the same.

The problem is when I try to hit the same url with my AngularJS service. I have worked with PathVariables in the past but am not sure how to pass these RequestParam variables into the URL.

Angular Service:

$resource(configuration.get('wsUrlPrefix') + 'abc/xyz/:page/:size/:sortData', {}, serviceConfig)

The URL turns out to be something like this:

http://localhost:8080/abc/xyz//10?sortdata=%7B%22orderType%22:null,%22oEDistrictNbr%22:null,%22orderNumber%22:null,%22mainLine%22:null%7D

and the backend is not able to recognize it.

Upvotes: 0

Views: 415

Answers (1)

Ovidiu Dolha
Ovidiu Dolha

Reputation: 5413

This is because your sortdata parameter is an object, e.g. like this:

{ 
  orderType: null,
  eEDistrictNbr:null
  ..
}

So when this is encoded it will become like in your question (escaped chars, etc)

One way to fix it is to make sure your parameters are always strings or numbers, by deconstructing your object into simple primitives, e.g.

$resource(configuration.get('wsUrlPrefix') + 'abc/xyz/:page/:size/:sortDataOrderType/:sortDataEDistricNr/...', {}, serviceConfig)

Upvotes: 1

Related Questions