lyf666 2017-02-16 17:26:12 +08:00
parent 0545e638e4
commit 0d24781dfc
2 changed files with 22 additions and 12 deletions

View File

@ -26,19 +26,29 @@ public class Controller extends com.jfinal.core.Controller{
renderText("2");
}
@Before(IdInterceptor.class)
public void dict() {
String id = getPara("id");
Map<String, String[]> paraMap = getParaMap();
List<Bson> ops = new ArrayList<>();
paraMap.forEach((key, arr) -> {
if ("id".equals(key)) {
ops.add(Filters.eq("_id", new ObjectId(arr[0])));
} else {
ops.add(Filters.eq(key, arr[0]));
}
});
MongoCollection<Document> collection = getCollection();
Document dict = collection.find(
Filters.eq("_id", new ObjectId(id))
).limit(1).first();
if (dict != null) {
renderJson(dict.toJson());
} else {
renderError(404);
}
List<Document> documents = new ArrayList<>();
collection.find(
Filters.and(ops)
).into(documents);
List<String> json = new ArrayList<>();
documents.forEach(doc -> {
json.add(doc.toJson());
});
renderText(Arrays.toString(json.toArray()), ContentType.JSON);
}
public void dictList() {

View File

@ -1,5 +1,5 @@
dict (String id required) {
return dict json
dict (key-value pairs) {
return dict list json
}
dictList (int pageNum default 0, int pageSize default 20) {
return dict list json