All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.jhipster.web.util.reactive.ResponseUtil Maven / Gradle / Ivy

Go to download

Server-side library used by applications created with the JHipster generator, see https://www.jhipster.tech/ for more information on JHipster

There is a newer version: 3.9.1
Show newest version
package io.github.jhipster.web.util.reactive;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.server.ResponseStatusException;
import reactor.core.publisher.Mono;

/**
 * Utility class for ResponseEntity creation in a reactive context.
 */
public interface ResponseUtil {
    /**
     * Wrap the mono into a {@link org.springframework.http.ResponseEntity} with an {@link org.springframework.http.HttpStatus#OK} status, or if it's empty, it
     * returns a {@link org.springframework.http.ResponseEntity} with {@link org.springframework.http.HttpStatus#NOT_FOUND}.
     *
     * @param            type of the response
     * @param maybeResponse response to return if present
     * @return response containing {@code maybeResponse} if present or {@link HttpStatus#NOT_FOUND}
     */
    static  Mono> wrapOrNotFound(Mono maybeResponse) {
        return wrapOrNotFound(maybeResponse, null);
    }

    /**
     * Wrap the mono into a {@link ResponseEntity} with an {@link HttpStatus#OK} status with the headers, or if it's
     * empty, throws a {@link org.springframework.web.server.ResponseStatusException} with status {@link HttpStatus#NOT_FOUND}.
     *
     * @param            type of the response
     * @param maybeResponse response to return if present
     * @param headers        headers to be added to the response
     * @return response containing {@code maybeResponse} if present or {@link HttpStatus#NOT_FOUND}
     */
    static  Mono> wrapOrNotFound(Mono maybeResponse, HttpHeaders headers) {
        return maybeResponse
            .switchIfEmpty(Mono.error(new ResponseStatusException(HttpStatus.NOT_FOUND)))
            .map(response -> ResponseEntity.ok().headers(headers).body(response));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy