jettey內(nèi)嵌啟動webapp項目

jettey內(nèi)嵌啟動app:

首先來說jersey啟動代碼:

public class JettyServer {
    private void start() throws Exception {
        int port = 8080;
        Server server = new Server(port);
        ServletContextHandler context = new ServletContextHandler(
                ServletContextHandler.SESSIONS);
        context.setContextPath("/");
        server.setHandler(context);
        ServletHolder sh = new ServletHolder(ServletContainer.class);
        sh.setInitParameter(
                "com.sun.jersey.config.property.resourceConfigClass",
                "com.sun.jersey.api.core.PackagesResourceConfig");
        sh.setInitParameter("com.sun.jersey.config.property.packages",
                "com.gmobile.api"); //包名為jersey的路由Resource所在目錄,如下代碼展示
        context.addServlet(sh, "/*");
        server.start();
    }

    public void stop() throws Exception {

    }

    public static void main(String[] args) throws Exception {
        JettyServer server = new JettyServer();
        server.start();
    }
}
此處是jersey的基于注解的路由分發(fā)類:
@Path("/")
public class WelcomeResource {
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Path("/hello")
    public Viewable sayHello() throws IOException {
        return new Viewable("/index.jsp", this);
    }

    @GET
    @Path("/index")
    public String index() {
        return "hello world!!!";
    }

    @POST
    @Path("/login")
    public String login() {
        return "login";
    }

}
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,273評論 6 342
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,034評論 25 709
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,554評論 19 139
  • 青青河邊草 枝頭春意鬧 漫步紅塵里 無風柳自搖
    扌后閱讀 215評論 1 1
  • 一年前婆婆為了減輕一下家務的煩勞,請了公公家的一個親戚來做家政,就煮中餐晚餐加打掃衛(wèi)生,不住家,一天勞動時...
    嚕嚕麻麻閱讀 2,652評論 0 0

友情鏈接更多精彩內(nèi)容