init
commit
40b646c37e
|
|
@ -0,0 +1,73 @@
|
|||
# Created by .ignore support plugin (hsz.mobi)
|
||||
### Maven template
|
||||
target/
|
||||
pom.xml.tag
|
||||
pom.xml.releaseBackup
|
||||
pom.xml.versionsBackup
|
||||
pom.xml.next
|
||||
release.properties
|
||||
dependency-reduced-pom.xml
|
||||
buildNumber.properties
|
||||
.mvn/timing.properties
|
||||
### Java template
|
||||
*.class
|
||||
|
||||
# Mobile Tools for Java (J2ME)
|
||||
.mtj.tmp/
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
|
||||
### JetBrains template
|
||||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
|
||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||
|
||||
# User-specific stuff:
|
||||
.idea/workspace.xml
|
||||
.idea/tasks.xml
|
||||
.idea/dictionaries
|
||||
.idea/vcs.xml
|
||||
.idea/jsLibraryMappings.xml
|
||||
|
||||
# Sensitive or high-churn files:
|
||||
.idea/dataSources.ids
|
||||
.idea/dataSources.xml
|
||||
.idea/dataSources.local.xml
|
||||
.idea/sqlDataSources.xml
|
||||
.idea/dynamic.xml
|
||||
.idea/uiDesigner.xml
|
||||
|
||||
# Gradle:
|
||||
.idea/gradle.xml
|
||||
.idea/libraries
|
||||
|
||||
# Mongo Explorer plugin:
|
||||
.idea/mongoSettings.xml
|
||||
|
||||
## File-based project format:
|
||||
*.iws
|
||||
|
||||
## Plugin-specific files:
|
||||
|
||||
# IntelliJ
|
||||
/out/
|
||||
|
||||
# mpeltonen/sbt-idea plugin
|
||||
.idea_modules/
|
||||
|
||||
# JIRA plugin
|
||||
atlassian-ide-plugin.xml
|
||||
|
||||
# Crashlytics plugin (for Android Studio and IntelliJ)
|
||||
com_crashlytics_export_strings.xml
|
||||
crashlytics.properties
|
||||
crashlytics-build.properties
|
||||
fabric.properties
|
||||
|
||||
*.iml
|
||||
.idea
|
||||
|
|
@ -0,0 +1,70 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>cn.cloudowr</groupId>
|
||||
<artifactId>dict</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>war</packaging>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>2.5</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.mortbay.jetty</groupId>
|
||||
<artifactId>jetty-maven-plugin</artifactId>
|
||||
<version>8.1.8.v20121106</version>
|
||||
<configuration>
|
||||
<stopKey>stop</stopKey>
|
||||
<stopPort>5599</stopPort>
|
||||
<webAppConfig>
|
||||
<contextPath>/</contextPath>
|
||||
</webAppConfig>
|
||||
<scanIntervalSeconds>5</scanIntervalSeconds>
|
||||
<connectors>
|
||||
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
|
||||
<port>8080</port>
|
||||
<maxIdleTime>60000</maxIdleTime>
|
||||
</connector>
|
||||
</connectors>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.jfinal</groupId>
|
||||
<artifactId>jfinal</artifactId>
|
||||
<version>3.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jfinal</groupId>
|
||||
<artifactId>jetty-server</artifactId>
|
||||
<version>8.1.8</version>
|
||||
</dependency>
|
||||
<!--<dependency>-->
|
||||
<!--<groupId>mysql</groupId>-->
|
||||
<!--<artifactId>mysql-connector-java</artifactId>-->
|
||||
<!--<version>5.1.20</version>-->
|
||||
<!--</dependency>-->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>1.2.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mongodb</groupId>
|
||||
<artifactId>mongo-java-driver</artifactId>
|
||||
<version>3.4.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
package cn.cloudowr.dict;
|
||||
|
||||
import com.jfinal.config.*;
|
||||
import com.jfinal.template.Engine;
|
||||
import com.mongodb.MongoClient;
|
||||
import com.mongodb.MongoCredential;
|
||||
import com.mongodb.ServerAddress;
|
||||
import com.mongodb.client.MongoDatabase;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Created by lyf66 on 2017/2/16.
|
||||
*/
|
||||
public class Config extends com.jfinal.config.JFinalConfig{
|
||||
|
||||
private static MongoDatabase mongoDatabase;
|
||||
|
||||
@Override
|
||||
public void configConstant(Constants me) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configRoute(Routes me) {
|
||||
me.add("/", Controller.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configEngine(Engine me) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configPlugin(Plugins me) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configInterceptor(Interceptors me) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configHandler(Handlers me) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterJFinalStart() {
|
||||
MongoCredential mongoCredential = MongoCredential.createCredential("root", "admin", "CoWR1111".toCharArray());
|
||||
MongoClient mongoClient = new MongoClient(new ServerAddress("120.24.5.249", 3717), Arrays.asList(mongoCredential));
|
||||
mongoDatabase = mongoClient.getDatabase("dict");
|
||||
}
|
||||
|
||||
public static MongoDatabase getMongoDataBase() {
|
||||
return mongoDatabase;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,119 @@
|
|||
package cn.cloudowr.dict;
|
||||
|
||||
import com.jfinal.aop.Before;
|
||||
import com.jfinal.aop.Interceptor;
|
||||
import com.jfinal.aop.Invocation;
|
||||
import com.jfinal.render.ContentType;
|
||||
import com.mongodb.client.MongoCollection;
|
||||
import com.mongodb.client.MongoDatabase;
|
||||
import com.mongodb.client.model.Filters;
|
||||
import com.mongodb.client.model.UpdateOptions;
|
||||
import com.mongodb.client.model.Updates;
|
||||
import com.mongodb.client.result.DeleteResult;
|
||||
import com.mongodb.client.result.UpdateResult;
|
||||
import org.bson.Document;
|
||||
import org.bson.conversions.Bson;
|
||||
import org.bson.types.ObjectId;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by lyf66 on 2017/2/16.
|
||||
*/
|
||||
public class Controller extends com.jfinal.core.Controller{
|
||||
public void index() {
|
||||
renderText("2");
|
||||
}
|
||||
|
||||
@Before(IdInterceptor.class)
|
||||
public void dict() {
|
||||
String id = getPara("id");
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
public void dictList() {
|
||||
int pageNum = getParaToInt("pageNum", 0);
|
||||
int pageSize = getParaToInt("pageSize", 20);
|
||||
|
||||
MongoCollection<Document> collection = getCollection();
|
||||
List<Document> documents = new ArrayList<>();
|
||||
collection.find().limit(pageSize).skip(pageNum * pageSize).into(documents);
|
||||
|
||||
List<String> json = new ArrayList<>();
|
||||
documents.forEach(doc -> {
|
||||
json.add(doc.toJson());
|
||||
});
|
||||
renderText(Arrays.toString(json.toArray()), ContentType.JSON);
|
||||
}
|
||||
|
||||
public void addDict() {
|
||||
Map<String, String[]> paraMap = getParaMap();
|
||||
Document dict = new Document();
|
||||
paraMap.forEach((key, arr) -> {
|
||||
dict.put(key, arr[0]);
|
||||
});
|
||||
getCollection().insertOne(dict);
|
||||
renderJson(dict);
|
||||
}
|
||||
|
||||
@Before(IdInterceptor.class)
|
||||
public void updateDict() {
|
||||
String id = getPara("id");
|
||||
String action = getPara("action");
|
||||
String[] keysToUnset = getParaValues("keys");
|
||||
List<Bson> ops = new ArrayList<>();
|
||||
|
||||
if (action.equals("unset")) {
|
||||
for (String key : keysToUnset) {
|
||||
if (key.equals("id")) continue;
|
||||
ops.add(Updates.unset(key));
|
||||
}
|
||||
} else {
|
||||
Map<String, String[]> paraMap = getParaMap();
|
||||
|
||||
paraMap.forEach((key, arr) -> {
|
||||
if (!key.equals("id")) {
|
||||
ops.add(Updates.set(key, arr[0]));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
UpdateResult result = getCollection().updateOne(
|
||||
Filters.eq("_id", new ObjectId(id)),
|
||||
Updates.combine(
|
||||
ops
|
||||
),
|
||||
new UpdateOptions().upsert(true)
|
||||
);
|
||||
|
||||
renderJson("result", result.getMatchedCount() >= 1);
|
||||
}
|
||||
|
||||
@Before(IdInterceptor.class)
|
||||
public void deleteDict() {
|
||||
String id = getPara("id");
|
||||
|
||||
DeleteResult result = getCollection().deleteOne(
|
||||
Filters.eq("_id", new ObjectId(id))
|
||||
);
|
||||
renderJson("result", result.getDeletedCount() == 1);
|
||||
}
|
||||
|
||||
private MongoCollection<Document> getCollection() {
|
||||
MongoDatabase mongoDataBase = Config.getMongoDataBase();
|
||||
MongoCollection<Document> collection = mongoDataBase.getCollection("dict");
|
||||
return collection;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
package cn.cloudowr.dict;
|
||||
|
||||
import com.jfinal.aop.Interceptor;
|
||||
import com.jfinal.aop.Invocation;
|
||||
|
||||
/**
|
||||
* Created by lyf66 on 2017/2/16.
|
||||
*/
|
||||
public class IdInterceptor implements Interceptor {
|
||||
|
||||
@Override
|
||||
public void intercept(Invocation inv) {
|
||||
String id = inv.getController().getPara("id");
|
||||
if (id != null && !id.isEmpty()) {
|
||||
inv.invoke();
|
||||
} else {
|
||||
inv.getController().renderError(400);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
package cn.cloudowr.dict;
|
||||
|
||||
import com.jfinal.core.JFinal;
|
||||
|
||||
/**
|
||||
* Created by lyf66 on 2017/2/16.
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
JFinal.start("src/main/webapp", 4200, "/");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
|
||||
version="3.1">
|
||||
<filter>
|
||||
<filter-name>cn.cloudowr.dict</filter-name>
|
||||
<filter-class>com.jfinal.core.JFinalFilter</filter-class>
|
||||
<init-param>
|
||||
<param-name>configClass</param-name>
|
||||
<param-value>cn.cloudowr.dict.Config</param-value>
|
||||
</init-param>
|
||||
</filter>
|
||||
<filter-mapping>
|
||||
<filter-name>cn.cloudowr.dict</filter-name>
|
||||
<url-pattern>/*</url-pattern>
|
||||
</filter-mapping>
|
||||
</web-app>
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
dict (String id required) {
|
||||
return dict json
|
||||
}
|
||||
dictList (int pageNum default 0, int pageSize default 20) {
|
||||
return dict list json
|
||||
}
|
||||
addDict (key-value pairs) {
|
||||
return true or false
|
||||
}
|
||||
updateDict (String id required, String action optional, String[] keys optional) {
|
||||
if (actions === 'unset') {
|
||||
删除指定id的dict的指定key
|
||||
return true or false
|
||||
} else {
|
||||
覆盖指定id的dict的指定key的对应value
|
||||
return true or false
|
||||
}
|
||||
}
|
||||
deleteDict (String id required) {
|
||||
return true or false
|
||||
}
|
||||
Loading…
Reference in New Issue