1

I encountered a problem which is very challenging to my Angular level. Could you give a help please?

In Spring Data REST the entity Worker has a @OneToMany bidirectional relationship with the entity TempworkEvent, shown below under _links. I would like to access TempworkEvent objects and their attributes through this relationship.

{
  "id" : 3,
  "name" : "Nadja Miller",
  "profession" : "Experte/in Anästhesiepflege",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/api/workers/3"
    },
    "worker" : {
       "href" : "http://localhost:8080/api/workers/3"
    },
    "tempworks" : {
       "href" : "http://localhost:8080/api/workers/3/tempworks"
    },
    "tempworkEvents" : {
       "href" : "http://localhost:8080/api/workers/3/tempworkEvents"
    }
}

In Angular the Worker entity successfully:

getWorkers(): Observable<Worker[]> {
   return this.httpClient.get<GetResponseWorkers>(this.workersUrl).pipe(
      map(response => response._embedded.workers)
   );
}

(...)

interface GetResponseWorkers {
  _embedded: {
    workers: Worker[];
  }
}

The challenge is how to access the Worker's tempworkEvents object and its attributes through _links:

"tempworkEvents" : {
   "href" : "http://localhost:8080/api/workers/3/tempworkEvents"
}

2 Answers2

2

One of:

  1. Make an additional network request to get the TempworkEvents

  2. Don't have a TempworkEvent JPA repository, and Spring Data Rest will include the tempworkEvents data in the workers response

  3. or, use a Projection, like:

in entities/projections/MyWorkerProjection.java:

@Projection(name="foo", types={Worker.class})
public interface MyWorkerProjection {
  Long getId();
  String getName();
  String getProfession();
  List<TempworkEvent> getTempworkEvents();
}

Then call http://localhost:8080/api/workers/3?projection=foo

If you want to have "auto" projections, see: Spring Data REST: projection representation of single resource

You may want to create another interface for TempworkEvent, if you want to project its data as well as its links

Neil McGuigan
  • 46,580
  • 12
  • 123
  • 152
0

let obj = {
  "id" : 3,
  "name" : "Nadja Miller",
  "profession" : "Experte/in Anästhesiepflege",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/api/workers/3"
    },
    "worker" : {
       "href" : "http://localhost:8080/api/workers/3"
    },
    "tempworks" : {
       "href" : "http://localhost:8080/api/workers/3/tempworks"
    },
    "tempworkEvents" : {
       "href" : "http://localhost:8080/api/workers/3/tempworkEvents"
    }
  }
}
console.log('tempworkEvents',obj._links.tempworkEvents);

You can access the object like above.

rohithpoya
  • 865
  • 7
  • 20
  • Thanks rohithpoya. But that way I would be accessing the URI, I would like to access the the object itself in the same call without having to make another backend request. – Murilo de Melo Reis Apr 01 '22 at 10:04
  • 2
    If you don't want to make a second backend request, you need to change your backend response to include the properties you need. – Will Alexander Apr 01 '22 at 10:50