0

I'm trying to display the content of HashMap received from my controller, however I get an empty map.

this is my controller:

@PostMapping(value = "getAllUsers")
public HashMap<String, Object> getAllUsers(@RequestBody String s)
{
    List<User> list = userRepository.getAll(s);

    HashMap<String, Object> userList = new HashMap<String, Object>();
    userList.put("list", list);
    userList.put("total", list.size());

    return userList;
}

and this is my angular code:

 getUsers() {

    this.userService.getUsers("canada").subscribe((data) => {
        console.log(data); // this returns an empty object
        data.forEach((value: string, key: string) => { // returns an error which is forEach is not a function
            console.log(key, value);
        });
        this.userList = data; // empty
        console.log(this.userList);
    });
}

this is my api service:

let URL = "api/v1";
getUsers(country: String): Observable<any> {
    return this.http.post(URL + "getAllUsers", country);
}

I want to know how to manipulate and iterate the HashMap received from my controller. Thank you in advance.

4
  • 1
    Try to http.get() instead of http.post() first read about how http protocols works then use them. Commented Nov 18, 2019 at 10:36
  • 1
    Is your URL complete? What you did show was "api/v1/getAllUsers". Did you check the response of the call in your network tab? Commented Nov 18, 2019 at 11:13
  • 1
    Are you sure, List<User> list = userRepository.getAll(s); has data for value canada? Also, forEach on data won't work at front end as its not of type Array, you need to iterate it over data.list. Commented Nov 18, 2019 at 11:50
  • yes there is data for the value of canada, i can get the total but not the data itself Commented Nov 18, 2019 at 14:13

3 Answers 3

1

Typescript is assuming the response as an Object of type any instead of a Map, so Try this:

Object.entries(data).forEach((value: string, key: string) => { 
      console.log(key, value);
});
Sign up to request clarification or add additional context in comments.

3 Comments

I don't think that's a problem. You can still declare any and get the data from the REST endpoint. The problem here is that there is nothing returned from the REST controller.
but you cannot apply forEach method on any
and there is no such rule that you cannot retrieve data from a POST request, it is just standard to use these methods for specific purpose but their is no such limitation
1

The point here is not HashMap iteration but your are getting empty response data even here.

 this.userService.getUsers("canada").subscribe((data) => {
       console.log(data);         // this returns an empty object

Add @ResponseBody

 @PostMapping(value = "getAllUsers")
 @ResponseBody
 public HashMap<String, Object> getAllUsers(@RequestBody String s)

Now coming to iteration once you get the response which is not empty there are multiple ways to iterate map.

  Object.keys(data).forEach(key => {

    console.log(key, data[key]);
  });

=> Better to use Http convention of using get to fetch data over post. Although data will still be received in Post but prefer get().

1 Comment

yes as you said the problem is in the controller side, I get empty data but I get the total, thanks
0

You are performing a POST request but in your use case you should use the GET http method in order to retrieve a resource. POST http method is used to add a resource. You have to change your controller to use @GetMapping()

@GetMapping("/getAllUsers")
public HashMap<String, Object> getAllUsers(@PathVariable String s)
{
    List<User> list = userRepository.getAll(s);

    HashMap<String, Object> userList = new HashMap<String, Object>();
    userList.put("list", list);
    userList.put("total", list.size());

    return userList;
}

You should also change your parameter to that controller from @RequestBody to @PathVariable

@PathVariable annotation is used to receive bind the URI variable to the method parameter.

@RequestBody annotation binds the content sent in (POST / PUT) request body with the annotated variable. (Generally, you can only use @RequestBody for the requests which can have 'body' content e.g. POST or PUT.)

You can read about HTTP Methods here.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.