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"); renderText("2");
} }
@Before(IdInterceptor.class)
public void dict() { 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(); MongoCollection<Document> collection = getCollection();
Document dict = collection.find( List<Document> documents = new ArrayList<>();
Filters.eq("_id", new ObjectId(id)) collection.find(
).limit(1).first(); Filters.and(ops)
if (dict != null) { ).into(documents);
renderJson(dict.toJson());
} else { List<String> json = new ArrayList<>();
renderError(404); documents.forEach(doc -> {
} json.add(doc.toJson());
});
renderText(Arrays.toString(json.toArray()), ContentType.JSON);
} }
public void dictList() { public void dictList() {

View File

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