简体   繁体   English

无法从 RouterFunction 获取 Boolean 作为 BodyParameter<serverresponse> 与 BodyToMono</serverresponse>

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

I'm trying to get a boolean parameter from an Angular 6 app and Spring can't handle it.我正在尝试从 Angular 6 应用程序中获取 boolean 参数,而 Spring 无法处理它。 I got the following error:我收到以下错误:

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

That's my front end code:这是我的前端代码:

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

And that's how i handle it on back:这就是我在后面处理它的方式:

Router:路由器:

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

Handle:处理:

    @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));
}

Thank you very much and have a nice day非常感谢你,祝你有美好的一天

You have to set content-Type to 'application/json' when you call the backend server from your Angular app.当您从 Angular 应用程序调用后端服务器时,您必须将 content-Type 设置为'application/json'

something like就像是

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

And then set it to your patch request.然后将其设置为您的patch请求。

Based on the stack trace, the fronted call the backend server with content-Type = 'text/plain;charset=UTF-8' so it (the backend server) fails to parse the request body.根据堆栈跟踪,前端使用 content-Type = 'text/plain;charset=UTF-8'调用后端服务器,因此它(后端服务器)无法解析请求正文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM