Search code examples
javaangularrestjerseyobservable

How can I receive just a string in Angular frontend from REST-Backend


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.


Solution

  • This is the proper syntax to do it:

    this.getFromHttpClient("auftrag/calculate", this.myObject).subscribe((response) => let m: string = response;);