I am using Spring boot as backend and Angular 7 as frontend in an application. I have to send map as response. For instance, say:
Map<String, Object> additionalResponse = new HashMap<>() {
{
put("key1","value2");
put("key2","value2");
}
};
I pass this map via response DTO to angular which is:
public class SearchResponseDto implements Serializable {
private Map<String, Object> additionalResponse;
}
Now on angular, I am using it as:
export class SearchResponseDto {
additionalResponse: Map<string, any>;
}
I can get value from additionalResponse but when I try to use get function in additionalResponse, it is giving me undefined value. What should I do to use additionalResponse as Map on angular?
Edit: I am using the backend response in Angular as:
fetchData() {
fetchResponse().
.subscribe(
response => {
this.response = response;
},
errorResponse => {
},
);
}
fetchResponse(): Observable<SearchResponseDto> {
return this.http.post<SearchResponseDto>(
<api-endpoints>
);
}
And, trying to use as this.response.additionalResponse.get('key1') and I am getting value as undefined but I am getting value from this.response.additionalResponse
On Postman, I am getting response as:
{
"additionalResponse": {
"key1": "value2",
"key2": "value2"
}
}
curl/Insomnia/Postman? If the data is serialized as JSON, it should "simply work".