簡體   English   中英

無法從 RouterFunction 獲取 Boolean 作為 BodyParameter<serverresponse> 與 BodyToMono</serverresponse>

[英]Can't get Boolean as BodyParameter from RouterFunction<ServerResponse> with BodyToMono

我正在嘗試從 Angular 6 應用程序中獲取 boolean 參數,而 Spring 無法處理它。 我收到以下錯誤:

org.springframework.web.server.UnsupportedMediaTypeStatusException: 415 UNSUPPORTED_MEDIA_TYPE "Content type 'text/plain;charset=UTF-8' not supported for bodyType=java.lang.Boolean"

這是我的前端代碼:

  updateConfirmationDate(reportInfo: number, isItTrue: boolean): Observable<Boolean> {
    const url = this.url;
    return this.httpClient.patch(url, isItTrue).pipe(first(), catchError(err => console.log(err)));
  }

這就是我在后面處理它的方式:

路由器:

    public RouterFunction<ServerResponse> router() {
        return RouterFunctions.route()
                .path(apiPrefix, builder -> builder
                        .PATCH("/patchBooleanEndpoint", diagnosticHandler::updateBooleanEndpoint)
                     )
                )
                .build();
    }

處理:

    @NonNull
public Mono<ServerResponse> updateMyBoolean(ServerRequest request) {
    final Long id = Long.parseLong(request.pathVariable("id"));

    return request.bodyToMono(Boolean.class)
            .flatMap(myBoolean -> service.updateBooleanById(id, myBoolean))
            .flatMap(savedBoolean ->
                    status(HttpStatus.OK)
                            .contentType(MediaType.APPLICATION_JSON)
                            .body(Mono.just(savedBoolean), Boolean.class));
}

非常感謝你,祝你有美好的一天

當您從 Angular 應用程序調用后端服務器時,您必須將 content-Type 設置為'application/json'

就像是

const headers = new HttpHeaders()
    .set("Content-Type", "application/json");

然后將其設置為您的patch請求。

根據堆棧跟蹤,前端使用 content-Type = 'text/plain;charset=UTF-8'調用后端服務器,因此它(后端服務器)無法解析請求正文。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM