为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

aluckdog 2022-09-22 13:50:09
对于我的应用程序,我在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等来做到这一点,但没有一个展示它是如何为单个字符串完成的。
查看完整描述

2 回答

?
潇潇雨雨

TA贡献1833条经验 获得超4个赞

这是执行此操作的正确语法:

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



查看完整回答
反对 回复 2022-09-22
?
FFIVE

TA贡献1797条经验 获得超6个赞

您需要向订阅添加一个函数:

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


查看完整回答
反对 回复 2022-09-22
  • 2 回答
  • 0 关注
  • 89 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信