HashMapのエントリを空にするサポートされているルートを定義するJSON設定ファイルの表現です。例:反復処理は、以下の方法が「ルート」(クラス名、クラスメソッド)を取得
{
"^/?(?<interface>threads)/?$": {
"GET": "list",
"POST": "create"
},
"^/?(?<interface>threads)/(?<id>\\d+)/?$": {
"GET": "get",
"POST": "reply",
"PUT": "edit",
"PATCH": "edit",
"DELETE": "delete"
}
}
EDIT
try {
JsonParser parser = JSONFactory.createJsonParser(in);
JsonNode root = JSONMapper.readTree(parser);
Iterator base = root.getFieldNames();
Iterator node;
String match, method;
HashMap<Pattern, HashMap<String, String>> routesMap = new HashMap();
while (base.hasNext()) {
match = base.next().toString();
if (match != null) {
node = root.get(match).getFieldNames();
HashMap<String, String> methods = new HashMap();
while (node.hasNext()) {
method = node.next().toString();
if (method != null) {
methods.put(method, root.get(match).get(method).getTextValue());
}
}
if (!methods.isEmpty()) {
routesMap.put(Pattern.compile(match), methods);
}
}
}
if (!routesMap.isEmpty()) {
hasRoutes = true;
routes = routesMap.entrySet();
}
// Help garbage collection
parser = null;
root = null;
base = null;
node = null;
match = null;
method = null;
routesMap = null;
} catch (Exception ex) {
}
EDIT 2、質問&のinit()メソッド内のプロパティ:JSONファイルの内容からサーブレットにアクセスするいくつかの理由
public final static JsonFactory JSONFactory = new JsonFactory();
public final static ObjectMapper JSONMapper = new ObjectMapper();
public static Router router;
private final Class self = getClass();
private final ClassLoader loader = self.getClassLoader();
public void init(ServletConfig config) throws ServletException {
super.init(config);
router = new Router(self.getResourceAsStream("/v1_0/Routes.json"), JSONFactory, JSONMapper);
}
最初の時間が経過した後のHashMapは空です値。 x.size()はゼロを返します。
これはPHPアプリケーションを初心者から書き直したものですので、問題が何か平凡なものであれば事前にお詫び申し上げます。
完全なソース: - Router source - Route source
どのように設定/キャッシュされていますか?コード内の何も何も削除しません。 –
@Dave Newton、JSONファイルの表現をHashSetに「キャッシュ」する手順を追加しました。 –
私がここで見つけることができなかったことは、どこにどのようにキャッシュしているかです。それはサーブレットのinit()フェーズにキャッシュされ、後続のフェーズで使用されますか? – kosa