jettey内嵌启动webapp项目

jettey内嵌启动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的基于注解的路由分发类:
@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";
    }

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,234评论 6 342
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 177,743评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,349评论 19 139
  • 青青河边草 枝头春意闹 漫步红尘里 无风柳自摇
    扌后阅读 214评论 1 1
  • 一年前婆婆为了减轻一下家务的烦劳,请了公公家的一个亲戚来做家政,就煮中餐晚餐加打扫卫生,不住家,一天劳动时...
    噜噜麻麻阅读 2,625评论 0 0

友情链接更多精彩内容