admin管理员组

文章数量:1122832

I m building a API with Springboot WebFlux. This method can downlaod file. This is the code:

 public Mono<ServerResponse> downloadAttachment(final ServerRequest request) {
        final var idPercorso = request.queryParam("idPercorso")
                .map(Integer::parseInt).orElse(null);
    }

Mono<ServerResponse> additionalCalcualtions(MapFile mapFile, ServerRequest request){
        return Mono.zip(parseUserInfo(request), request.bodyToMono(ObjectNode.class))
                .flatMap(t -> {
                    //var dto = t.getT2();
                    return silverMountainService(t.getT1()).downloadMappaPercorso(StorageType.parse(mapFile.storageType),mapFile.omniaClass, mapFile.getFileName()
                            )
                            .flatMap(fileResult -> ServerResponse.ok()
                                    .headers(h -> h.setContentDisposition(
                                            Optional.ofNullable(mapFile.getFileName())
                                                    .filter(StringUtils::isNotBlank)
                                                    .map(filename -> ContentDisposition.attachment().filename(mapFile.getFileName()).build())
                                                    .orElseGet(() -> ContentDisposition.inline().build() )
                                    ))
                                    .contentType(MediaType.APPLICATION_OCTET_STREAM)
                                    .body(BodyInserters.fromValue(fileResult.getContent()))
                            );
                })
                .onErrorResume(CustomHttpException.class, assEx -> {
                    log.error("Error({}): {}", assEx.getErrorId(), assEx.getMessage());
                    return ServerResponse.status(assEx.getHttpStatus()).bodyValue(String.format("ErrorID: %s", assEx.getErrorId()));
                })
                .onErrorResume(Exception.class, ex -> {
                    var errId = UUID.randomUUID().toString();
                    log.error(String.format("Error(%s) %s", errId, ex.getMessage()), ex);
                    return ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR).bodyValue(String.format("ErrorID: %s", errId));
                });
    }

To call this method, I m using the following link:

myUrl/downloadMappa?idPercorso=2

Now if I try to call my method with json body like this

{}

my method works correctly. If I try to call my method without json body the return me empty response.

How can I make to change my code to accept NULL as body?

本文标签: spring bootjava API GET with WebFluxStack Overflow