本文介绍了为什么 request.ts 中的 serializeBody 方法不转换 JSON.stringify(this.body) 以防字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将 string 值传递给 post 方法时,该值没有被转换为 JSON.所以我们无法在 Controller 中获得这个值.

When i pass string value into post method, this value was not converted JSON.So We could not get this value in Controller.

我检查了以下代码并找到了值未转换的原因.

I checked following code and find the reason why value was not converted.

链接

但我无法理解这种行为是正确的.

But I could not understand this behavior is correct.

apply(id: string): Observable<boolean> {
    return this.http.post<boolean>(
      REQUEST_URL + '/applicationrequest',
      id,
      HEADERS
    );
  }
@ResponseBody
    @RequestMapping(value = "/applicationrequest", method = RequestMethod.POST)
    public boolean apply(@RequestBody UUID id) {
        return usecase.apply(id);
    }

我想知道为什么string不应该自动转换JSON.如果使用 httpClient 的方式,我想知道正确的使用方式.谢谢.

I want to know the reason why string should not be converted JSON automatically.And if way to use httpClient, I want to know the correct way to use.Thank you.

Angular 版本是 8.1.0.Java 是 Java 8.Spring 是 5.1/Spring Boot 2.1

Angular Version is 8.1.0.Java is Java 8.Spring is 5.1 / Spring Boot 2.1

推荐答案

很简单,因为这是两个完全不同的东西.Angular(或 httpclient)无法神奇地知道端点需要或期望什么作为输入.

Simply because those are 2 totally different things. Angular (or the httpclient) can't know by magic what exactly the endpoint requires or expects as input.

作为开发人员,您可以自行生成与服务器接口匹配的 http 请求.

It's simply up to you as a developer to generate the http request that matches the server's interface.

这篇关于为什么 request.ts 中的 serializeBody 方法不转换 JSON.stringify(this.body) 以防字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 01:49