如何从 REST 后端在角度前端中仅接收一个字符串

对于我的应用程序,我在Java后端计算一些值。我唯一想做的就是在我的Angular前端接收这个字符串,但是我在可观察类型上挣扎。


这是我的后端REST调用,它使用弹簧启动/泽西岛注释:


@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";

}

现在我唯一想做的就是在我的Angular前端得到这个字符串。但是我不明白这如何与可观察量一起使用,以下是我所拥有的:


public getFromHttpClient(url: string, json: any): Observable<string> {

    return this.httpClient.get<string>(`${this.basePath}/${url}/${json.id}`);

}

但是,当我尝试评估这一点时,它不起作用:


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

最简单的方法是什么?我做错了什么?我找不到类似的例子,每个人总是只展示如何使用Arrais等来做到这一点,但没有一个展示它是如何为单个字符串完成的。


aluckdog
浏览 92回答 2
2回答

潇潇雨雨

这是执行此操作的正确语法:this.getFromHttpClient("auftrag/calculate",&nbsp;this.myObject).subscribe((response)&nbsp;=>&nbsp;let&nbsp;m:&nbsp;string&nbsp;=&nbsp;response;);

FFIVE

您需要向订阅添加一个函数:let&nbsp;m:&nbsp;string; this.getFromHttpClient("auftrag/calculate",&nbsp;this.myObject).subscribe((result:&nbsp;string)&nbsp;=>&nbsp;{m&nbsp;=&nbsp;result;});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java