一、实现ErrorWebExceptionHandler 接口
import com.alibaba.fastjson.JSONObject;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import reactor.netty.ByteBufMono;
@Component
@Order(-1)
public class FilterExceptionHandler implements ErrorWebExceptionHandler {
@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
ServerHttpResponse response = exchange.getResponse();
response.setStatusCode(HttpStatus.OK);
ResultVO resultVO = ResultVO.failMessageInSuccess(ex.getMessage());
DataBuffer dataBuffer = response.bufferFactory()
.allocateBuffer().write(JSONObject.toJSONString(resultVO).getBytes());
response.getHeaders().setContentType(MediaType.APPLICATION_STREAM_JSON);
return response.writeAndFlushWith(Mono.just(ByteBufMono.just(dataBuffer)));
}
}
二、在需要抛出异常的地方返回Mono.error(new Exception("错误消息"))
return Flux.just(1, 2, 3).collectList().flatMap(list -> {
if (list.size() <= 4) {
return Mono.error(new Exception("集合太小!"));
}
return Mono.just(list);
}).flatMap(r -> ok().body(fromValue(r)));
三、测试
postman发送请求,得到结果:
{
"code": "500",
"data": {},
"message": "集合太小!",
"success": false
}
|