Slim3ã«ãããæ¡å¼µåä»ãURIã®ãããã³ã°
CoolãªURIã§JSONãè¿ãURLã¯ã©ã表ç¾ããã
å
é¨çã«ã¯ã/user/get?id=12345&type=jsonãã¨ãªããããªURIï¼idã12345ã®ã¦ã¼ã¶æ
å ±ã®JSONå½¢å¼ï¼ããCool URIã§ã¯ã©ã®ããã«è¡¨ç¾ããã®ãé©åãæ©ãã§ãããã/user/json/12345ããã/user/12345/jsonãã¯ãªããéãæ°ããããã
ãããããWebãæ¯ããæè¡ -HTTPãURIãHTMLãããã¦REST (WEB+DB PRESSãã©ã¹ã·ãªã¼ãº)ãã«ãã®çããæ¸ãã¦ãã£ãã
ä¾ãã°ï¼ã¤ã®ãªã½ã¼ã¹ãHTMLã¨ããã¹ãã¨JSONã§è¡¨ç¾ã§ããå ´åã«ã¯ãããããã.htmlãã.txtãã.jsonãã¨ããæ¡å¼µåãä»ãã¦ããã®è¡¨ç¾ãåããã¨è¯ãã§ãããã
ãWebãæ¯ããæè¡ã p.062
å è¿°ã®ä¾ã§è¨ãã°ãã/user/12345.jsonãã¨ãªãããªãã»ã©ç¢ºãã«ããã¯ãã£ãããããã¨ãããã¨ã§ãtxtã¨jsonã®æ¡å¼µåã«å¯¾å¿ããURIã®å®è£ ããSlim3ã§è©¦ããã
URIãããã³ã°ã¯RouterImpl#addRouting()ã§è¨å®
ã¾ããSlim3ã§ã®URLãããã³ã°ã¯ãRouterImplãå®è£ ããAppRouterå ã®addRouting(from, to)ã§ä¸è¨ã®ããã«è¨å®ããã
public AppRouter() { addRouting("/entry/delete/{key}", "/entry/delete?key={key}"); }
â»æ³¨æç¹ã¨ãã¦ãfromã¨toã«ã¯ããããã²ã¨ã¤ä»¥ä¸ã®{xxx}ãå«ããå¿ è¦ãããã¿ãããã²ã¨ã¤ãå«ã¾ãªãã¨æ£ãããããã³ã°ãè¡ãããªãã£ãã
ãã ããæ¡å¼µåãå«ãã URIã¯ãSlim3ã¯ããã©ã«ãã§éçãªã¯ã¨ã¹ãã¨ã¿ãªãã®ã§ããããã³ã°ãè¡ãããªããããã§ãAppRouterã«ããã«æãå ããå¿ è¦ãããã
æ¡å¼µåãå«ãURIã¯IsStatic()ããªã¼ãã¼ã©ã¤ããã
æ¡å¼µåã¤ãURIãéçãªã¯ã¨ã¹ãã¨è¦ãªãããã®ãåé¿ããã«ã¯ãRouterImpl#IsStatic()ããªã¼ãã¼ã©ã¤ããã¦ãå¦çãè¨è¿°ãããæ¡å¼µåã.jsonã®URIãéçã¨ã¿ãªããªãããã«ããã«ã¯ãä¸è¨ã®ãããªã³ã¼ããæ¸ãã°è¯ãã
@Override public boolean isStatic(String path) throws NullPointerException { boolean isStatic = super.isStatic(path); if("json".equals(RequestUtil.getExtension(path)) || "txt".equals(RequestUtil.getExtension(path))) { return false; } else { return isStatic; } }
ãã®ããã§ãaddRouting()ã¯ä¸è¨ã®ããã«è¨å®ã
public AppRouter() { addRouting( "/entry/{id}\\.{extension}", "/entry/get?id={id}&extension={extension}"); }
ä¸è¨ã«ãããããã³ã°ãããGetControllerã¯ä»¥ä¸ã®ãããªæãã
public class GetController extends Controller { @Override protected Navigation run() throws Exception { String extension = request.getParameter("extension"); String id = request.getParameter("id"); if ("json".equals(extension)) { response.setContentType("application/json"); // responseã«JSONãæ¸ãè¾¼ãå¦ç } else if("txt".equals(extension)) { response.setContentType("text/plain"); // responseã«ããã¹ãã®å 容ãæ¸ãè¾¼ãå¦ç } else { // ã¨ã©ã¼ } return null; } }
ããã§ãã/user/12345.jsonããªãjsonå½¢å¼ã§ãã/user/12345.txtããªãããã¹ãå½¢å¼ã§id=12345ã®ã¦ã¼ã¶æ å ±ãåå¾ã§ããã対å¿ããæ¡å¼µåãå¢ããããAppRouter#IsStatic()ã¨GetControtter#run()ã®åå²ã«è¿½å ããã°ããã
æ¡å¼µåã«ãã£ã¦Controllerãåããå ´å
run()ã®ä¸ã§åå²ãããæ¡å¼µåã«ãã£ã¦ãããã³ã°ããã³ã³ããã¼ã©èªä½ãåãæ¿ããããªããä¸è¨ã®ããã«addRouting()ããã°ã§ããã
addRouting("/entry/{id}\\.{extension}", "/entry/get{extension}?id={id}");
ãã®ããã§ãGetjsonControllerãGettxtControllerãå®è£ ããã°ããããã®æ¹æ³ã ã¨run()å ã§åå²ããªãã®ã§ã³ã¼ãããã£ãããããã©ããããã³ã°ã®é½åä¸ãã³ã³ããã¼ã©ã®ã¯ã©ã¹åãPascalå½¢å¼ï¼GetJsonControllerã¨ãGetTxtControllerï¼ã«ã§ããªãã®ãæããã(Getã¤ããªããã°ãããã ãã©ï¼
次ã¯ãhttpã¡ã½ããã«å¿ãã¦ãããã³ã°å ã®Controllerãåãæ¿ããå®è£ ã«ã¤ãã¦è©¦è¡é¯èª¤ããäºå®ãController#isGet()ã¨ãã使ããã«ã
åèãã¼ã¸
addRoutingã«æ¡å¼µåãã¤ããURLãæå®ããã¨ãã®æåã«ã¤ã㦠- Slim3 User Japan | Google ã°ã«ã¼ã
Routerã«ããURL mapping - Slim3 User Japan | Google ã°ã«ã¼ã
RESTfulãªWebãµã¤ãã¨æ¡å¼µåãå«ãURLã«ã¤ã㦠- æªå±±æ£å¹¸ã®ããã¤ã©é£¼è²è¨