fun.fengwk.convention4j.springboot.starter.webflux.util.ReactiveResultUtils Maven / Gradle / Ivy
package fun.fengwk.convention4j.springboot.starter.webflux.util;
import fun.fengwk.convention4j.api.result.Result;
import fun.fengwk.convention4j.springboot.starter.result.ResultInternalInvokerUtils;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
import java.util.function.Consumer;
import static org.springframework.web.reactive.function.server.ServerResponse.status;
/**
* @author fengwk
*/
public class ReactiveResultUtils {
private ReactiveResultUtils() {
}
public static Mono adapt(ServerRequest request, Result result) {
ServerResponse.BodyBuilder respBuilder = newBuildByStatus(request, result);
respBuilder.contentType(MediaType.APPLICATION_JSON);
Mono> resMono = Mono.just(result);
return respBuilder.body(resMono, new ParameterizedTypeReference<>() {});
}
public static Mono adapt(ServerRequest request, Result result,
Consumer respConsumer) {
ServerResponse.BodyBuilder respBuilder = newBuildByStatus(request, result);
respConsumer.accept(respBuilder);
respBuilder.contentType(MediaType.APPLICATION_JSON);
Mono> resMono = Mono.just(result);
return respBuilder.body(resMono, new ParameterizedTypeReference<>() {});
}
private static ServerResponse.BodyBuilder newBuildByStatus(ServerRequest request, Result result) {
if (ResultInternalInvokerUtils.isIgnoreErrorHttpStatus(request.headers()::firstHeader)) {
return status(HttpStatus.OK);
} else {
return status(result.getStatus());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy