我想创建一个拦截器,它从传入请求中获取数据并向底层数据库发出自定义查询,将该查询的结果返回给用户。我无法弄清楚如何将自定义查询的结果返回给用户。
这是我尝试的示例,但我在插件中创建和设置的响应文档没有返回给用户。
是否可以使用 MongoInterceptor 来做到这一点?
@RegisterPlugin(name = "exampleInterceptor",
description = "example interceptor",
interceptPoint = InterceptPoint.REQUEST_AFTER_AUTH,
priority = 100)
public class ExampleInterceptor implements MongoInterceptor {
@Override
public void handle(MongoRequest request, MongoResponse response)
throws Exception
{
String dbName = request.getDBName();
String collName = request.getCollectionName();
String[] pathInfo = request.getMappedRequestUri().split("/");
if (pathInfo.length == 3) {
String id = pathInfo[2];
BsonDocument doc = new BsonDocument();
doc.put("db", new BsonString(dbName));
doc.put("collection", new BsonString(collName));
doc.put("id", new BsonString(id));
response.setContent(doc);
}
}
@Override
public boolean resolve(MongoRequest request, MongoResponse response) {
return true;
}
}