栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

创建与默认Jetty的Jersey注册行为相同的ResourceConfig

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

创建与默认Jetty的Jersey注册行为相同的ResourceConfig

我不知道你是怎么工作的

ServletHolder holder = new ServletHolder(new ServletContainer());

我无法简单地实例化一个实例来生成一个可行的示例

ServletContainer()
。尽管我正准备将其与以下代码一起使用

public class TestJerseyServer {    public static void main(String[] args) throws Exception {        ResourceConfig config = new ResourceConfig();        config.packages("jetty.practice.resources");        ServletHolder jerseyServlet   = new ServletHolder(new ServletContainer(config));        Server server = new Server(8080);        ServletContextHandler context      = new ServletContextHandler(server, "/");        context.addServlet(jerseyServlet, "/*");        server.start();        server.join();    }}

使用所有依赖项(不包括

com.sun.jersey:jersey-json
,因为不需要)。没有其他配置。资源类

@Path("test")public class TestResource {    @GET    @Produces(MediaType.APPLICATION_JSON)    public Response getTest() {        Hello hello = new Hello();        hello.hello = "world";        return Response.ok(hello).build();    }    @POST    @Consumes(MediaType.APPLICATION_JSON)    public Response postHello(Hello hello) {        return Response.ok(hello.hello).build();    }    public static class Hello {        public String hello;    }}

jetty.practice.resources
包装中。

我很好奇,看看您如何在没有

ResourceConfig


我应该提到的另一件事是

jersey-container-servlet-core
应该将其切换为
jersey-container-servlet
。前者用于2.5容器支持,但推荐用于3.x容器。以我的例子来说,它没有任何作用


卷曲

C:>curl http://localhost:8080/test -X POST -d "{"hello":"world"}" -H"Content-Type:application/json"

world

C:>curl http://localhost:8080/test

{"hello":"world"}



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/573934.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号