I have a simple resource , it return a Uni < Response >
@POST
@Path("/test2")
public Uni<Response> test2(SampleEntity test) {
List<SampleEntity> recordList = new ArrayList<>();
recordList.add(new SampleEntity("cat", 12));
recordList.add(new SampleEntity("dog", 14));
return Uni.createFrom().item(Response.ok(recordList).build());
}
and I have a sample Interceptor, Used to log the request/response as json
@AroundInvoke
public Object resourceLog(InvocationContext context) throws Exception {
timer.restart();
log.info(StrUtil.format("request->[{}]", JSONUtil.toJsonStr(context.getParameters())));
Object response = context.proceed();
log.info(StrUtil.format("response->[{}]", response));
return response;
}
When I send a json request :
{
"name": "cat",
"age": 12
}
The log printed out is :
2021-04-09 10:29:34,529 INFO [cof.gei.int.ResourceInterceptor] (vert.x-eventloop-thread-4) request->[[{"name":"cat","age":12}]]
2021-04-09 10:29:34,529 INFO [cof.gei.int.ResourceInterceptor] (vert.x-eventloop-thread-4) response->[io.smallrye.mutiny.operators.uni.builders.UniCreateFromKnownItem@a0122dd]
The question I want to ask is: How to print out the json content of a response when the interceptor intercepts a response that is a Uni< Response >
This is my current solution :
Object response = context.proceed();
if(response instanceof Uni uni) {
uni.subscribe().with(body -> log.info(StrUtil.format("response->Uni:[{}]", JSONUtil.toJsonStr(body))));
}else {
log.info(StrUtil.format("response->[{}]", JSONUtil.toJsonStr(response)));
}
But I think it's ugly.