我正在尝试在此代码中集成验证,但是验证失败,没有任何错误(只是返回了空白页):
@Component
@Slf4j
public class EthereumAccountController implements ClarityControllerMono {
private final Pipeline queryPipeline;
private final Pipeline commandPipeline;
private final Web3jService web3;
private final RequestHandler requestHandler;
public EthereumAccountController(@Qualifier("queryPipelinr") Pipeline queryPipeline, @Qualifier("commandPipelinr") Pipeline commandPipeline, Web3jService web3, RequestHandler requestHandler) {
this.queryPipeline = queryPipeline;
this.commandPipeline = commandPipeline;
this.web3 = web3;
this.requestHandler = requestHandler;
}
public Mono<ServerResponse> createAccount(ServerRequest serverRequest) {
return requestHandler.requireValidBody(body -> getJsonErrsResp("testtttt"), serverRequest, AccountRequestDTO.class);
}
}
public interface ClarityControllerMono {
default Mono<ServerResponse> getJsonSuccessResp (Object object) {
Map<String, Object> result = new LinkedHashMap<>();
result.put("status", "success");
result.put("data", object);
return ok()
.contentType(APPLICATION_JSON)
.body(BodyInserters.fromPublisher(Mono.just(toJSON(result)), String.class));
}
default Mono<ServerResponse> getJsonErrsResp (Object object) {
Map<String, Object> result = new LinkedHashMap<>();
result.put("status", "error");
result.put("message", object);
return ok()
.contentType(APPLICATION_JSON)
.body(BodyInserters.fromPublisher(Mono.just(toJSON(result)), String.class));
}
private String toJSON(Object object) {
ObjectMapper objectMapper = new ObjectMapper();
return Unchecked.function(objectMapper::writeValueAsString).apply(object);
}
}
这是代码失败的地方
@Component
public class RequestHandler {
private final Validator validator;
public RequestHandler(Validator validator) {
this.validator = validator;
}
public <BODY> Mono<ServerResponse> requireValidBody(
Function<Mono<BODY>, Mono<ServerResponse>> block,
ServerRequest request, Class<BODY> bodyClass) {
return request
.bodyToMono(bodyClass)
.flatMap(
body -> validator.validate(body).isEmpty()
? block.apply(Mono.just(body))
: ServerResponse.unprocessableEntity().build()
);
}
}
这是我在其中找到要实现的有趣解决方案的存储库(失败的部分代码) https://github.com/jeroenbellen/Validate-functional-endpoints-in-Spring/blob/master/src/main/java/foo/bar/springfunctionalwebvalidation/controller/RequestHandler.java
谢谢。
编辑
public <BODY> Mono<ServerResponse> requireValidBody(
Function<Mono<BODY>, Mono<ServerResponse>> block,
ServerRequest request, Class<BODY> bodyClass) {
Hooks.onOperatorDebug();
return request
.bodyToMono(bodyClass)
.doOnError(Throwable::printStackTrace)
.flatMap(
body -> {
log.info("Emptyness" +validator.validate(body).isEmpty() + "");
return validator.validate(body).isEmpty()
? block.apply(Mono.just(body))
: ServerResponse.unprocessableEntity().build();
}
).doOnError(Throwable::printStackTrace);
doOnError不会放任何东西。