0

我正在从我的 Angular 8 Liferay 7.3 Portlet 发出一个 POST 请求,并尝试serveResource在类的方法中获取其内容MVCResourceCommand

前端 JS 如下所示:

submit() {
    let message = {
        type: "form",
        body: this.model
      };
    
    this.http.post<any>("http://my/url", message).subscribe(response => {
        console.log(JSON.stringify(response));
    })
  }

消息命中端点,因此后端配置良好。基于thisthisthis我还设置了"com.liferay.portlet.requires-namespaced-parameters=false"属性,以便命名空间不会成为问题 - 因此,基本上,检查列表中的每个点都已完成,并且应该可以访问请求正文。到目前为止,我阅读的每篇文章都在谈论使用 获取请求信息ParamUtil.getString(uploadRequest, "text");,但我不确定如果主体是 JSON 对象,这应该如何工作 - 我的意思是,如果 POST 主体看起来像这样,我应该如何检索该值:

{
    "firstVal": "abc",
    "secondVal": "def",
    "another": {
        "objectVal1": 1,
        "objectVal2": 2
    }
}
4

1 回答 1

0

多亏了这篇文章,我才来到这段代码,它将正文作为字符串检索:

String body = PortalUtil.getHttpServletRequest(resourceRequest).getReader().lines()
                .collect(java.util.stream.Collectors.joining(System.lineSeparator()));

即使我解决了我的问题,我也想知道是否有其他方法可以做到这一点。

于 2020-12-01T13:41:25.523 回答