繁体   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