Within the database exist two distinct entities: 'person' and 'name'. These entities share a many-to-many relationship with the properties 'from' and 'to'.
Person PersonName Name
---------- ------------- -----------
id: number from: number id: number
to: number firstname: string
lastname: string
The Spring-Boot backend is responsible for sending a json-array containing all persons to the Angular frontend.
An example of a Json Object representing a Person would look like this:
{
id: 1,
names: [
{
from: 1733,
to: 1735,
name: {
id: 1,
firstname: John,
lastname: Doe
}
}, ...
]
The structure of my Angular service is as follows:
export class PersonService {
constructor(private http: HttpClient) { }
getAll(): Observable<any> {
return this.http.get('//localhost:8080/person')
};
}
I've created classes for each entity and their relationships. For instance, the 'name' class takes on the following form:
export class Name {
id: number;
firstname: string;
lastname: string;
}
How can I convert the Json-Array into instances of the respective Typescript classes? The objective here is ensuring that the getAll() function returns an Observable of type Person[].
Despite making the change from
this.http.get('//localhost:8080/person')
to this.http.get<Person[]>('//localhost:8080/person')
, it fails to yield a valid Person array.