Reputation: 1247
For my application I calculate some Value in my Java backend. The only thing I want to do is to receive this String in my Angular frontend, however I struggle with the Observable type.
This is my backend REST Call it uses Spring Boot/Jersey annotations:
@GET
@Path("/calculate/{auftragId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public String calculateAwWithJSON(@PathParam("auftragId") String auftragId) {
restLog("receiving put request /put/auftragId ->> " + auftragId);
for (AuftragREST anAuftragRepoList : auftragRepoList) {
if (anAuftragRepoList.getId().replace(" ", "").equals(auftragId)) {
restLog("successfully calculated AW the requested data!");
return ObjektprotokollCalculator.calculateAW(anAuftragRepoList).toString();
}
}
return "0";
}
Now the only thing I want to do is get that string in my Angular frontend. However I do not understand how this works with Observables, here is what I have:
public getFromHttpClient(url: string, json: any): Observable<string> {
return this.httpClient.get<string>(`${this.basePath}/${url}/${json.id}`);
}
However when I try to assing this it doesn't work:
let m: string = this.getFromHttpClient("auftrag/calculate", this.myObject).subscribe();
What's the easiest way to do this? What am I doing wrong? I couldn't find a similar example, everyone always just shows how to do it with Arrays and such but none show how it's done for just a single string.
Upvotes: 1
Views: 464
Reputation: 187
This is the proper syntax to do it:
this.getFromHttpClient("auftrag/calculate", this.myObject).subscribe((response) => let m: string = response;);
Upvotes: 2
Reputation: 2121
You need to add a function to the subscribe:
let m: string;
this.getFromHttpClient("auftrag/calculate", this.myObject).subscribe((result: string) => {m = result;});
Upvotes: 0