【问题标题】:How to send a string with quotemarks in the RequestBody?如何在 RequestBody 中发送带引号的字符串?
【发布时间】:2019-11-18 10:05:45
【问题描述】:

我有以下 API 方法:

@PatchMapping("/{id}")
  public ResponseEntity<?> partialProjectUpdate(@PathVariable long id, @RequestBody EntryStatus status) throws DailyEntryNotFoundException {
    return dailyEntryService.partialDailyEntryUpdate(id, status);
  }

EntryStatus 是一个枚举:

public enum EntryStatus {
  OPEN,
  PROGRESS,
  CHECKED,
  BOOKED,
  UNAVAILABLE;

  private static Map<String, EntryStatus> namesMap = new HashMap<String, EntryStatus>(3);

  static {
    namesMap.put("OPEN", OPEN);
    namesMap.put("PROGRESS", PROGRESS);
    namesMap.put("CHECKED", CHECKED);
    namesMap.put("BOOKED", BOOKED);
    namesMap.put("UNAVAILABLE", UNAVAILABLE);
  }

  @JsonCreator
  public static EntryStatus forValue(String value) {
    return namesMap.get(value);
  }

  @JsonValue
  public String toValue() {
    for (Map.Entry<String, EntryStatus> entry : namesMap.entrySet()) {
      if (entry.getValue() == this)
        return entry.getKey();
    }

    return null; // or fail
  }
}

我在打字稿中这样调用方法:

partialUpdateDailyEntry(dailyEntry: DailyEntry, status): Observable<any> {
    const statusName: string = status.name;
    return this.http.patch(BASE_URL + dailyEntry.id, statusName, this.authService.setHeaders('application/json'))
      .pipe(
        catchError(this.handleService.error)
      );
  }

statusName 是一个字符串,但问题是它通过 JSON 发送时不带引号。 RequestBody 是例如 OPEN 而不是 "OPEN" 这给了我以下错误:

JSON parse error: Unrecognized token 'OPEN': was expecting ('true', 'false' or 'null').

如前所述,这是由于字符串发送时不带引号引起的。

我可以通过手动将引号添加到statusName 来解决这个问题,如下所示:

const statusName: string = '"' + status.name + '"';

但这不是正确的解决方案,有更好的方法吗?

【问题讨论】:

    标签: java json angular rest


    【解决方案1】:

    试试

    @JsonFormat(shape = JsonFormat.Shape.STRING)
    public enum EntryStatus{
        OPEN,
        PROGRESS,
        CHECKED,
        BOOKED,
        UNAVAILABLE;
    }
    

    【讨论】:

    • 我得到了同样的错误。它仍然期望在RequestBody 中有一个String,所以RequestBody 仍然需要有引号而它没有。
    【解决方案2】:

    也许你可以把

    namesMap.put("OPEN", OPEN);

    作为

    namesMap.put("\"OPEN\"", OPEN);

    【讨论】:

      【解决方案3】:

      您正在添加要发送的标头 JSON,但 "OPEN" 不是有效的 JSON 值。

      你应该改变你的标题:

      this.authService.setHeaders('text/plain')
      

      或更改发送方式:

      this.http.patch(BASE_URL + dailyEntry.id, { status: statusName});
      

      并更改您的 java 后端来处理以接收对象并读取状态

      或者在发送之前对其进行字符串化:

      const statusName: string = JSON.stringify(status.name);
      

      【讨论】:

      • 但是,如果我将 "OPEN" 在正文中作为 JSON 发送到像 this 这样的邮递员,它可以工作,那么它不应该是一个有效的 JSON 值吗?
      • @M.Dietz 如果它是一个 json 所以你必须用双引号发送它
      • @M.Dietz Postman 可能会发送"OPEN",我猜角度HttpClient 仅在传递的正文参数不是字符串时使用JSON.stringify。你告诉HttpClient你要发送一个JSON,但是你没有;)。我添加了您可以尝试的第三个选项,这会添加引号
      猜你喜欢
      • 2022-01-19
      • 2015-10-04
      • 1970-01-01
      • 1970-01-01
      • 2023-01-13
      • 2017-10-08
      • 2023-04-01
      • 1970-01-01
      • 2018-05-06
      相关资源
      最近更新 更多