创建一个与默认 Jetty 的 Jersey 注册行为方式相同的 ResourceConfig [英] Creating a ResourceConfig that behaves the same way as default Jetty's Jersey registering

查看:37
本文介绍了创建一个与默认 Jetty 的 Jersey 注册行为方式相同的 ResourceConfig的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个端点:

@POST
@Path("/test")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public String canaryTest(String JSON) {
    return JSON;
}

当我使用 Jersey 在 Jetty 中注册时

When I register it in Jetty using Jersey

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

似乎一切正常.但如果我尝试明确指定默认配置,它会停止工作(从端点返回媒体类型错误).即使只是将 ResourceConfig 的默认实例传递给 ServletContainer,它也会停止工作.

everything seems to work fine. But in case I try to specify explictly the default config, it stops working (returning a media type error from the endpoint). Even by just passing a default instance of a ResourceConfig to the ServletContainer, it stops working.

ResourceConfig config = new ResourceConfig();
//config.property(x,defaultX)
//config.property(y,defaultY)
ServletHolder holder = new ServletHolder(new ServletContainer(config));

我想手动和显式地模拟默认配置行为,所以我在这里要问的是我应该如何配置 ResourceConfig 以使行为保持工作(即要设置哪些属性)

I'd like to emulate the default configuration behavior manually and explicitly, so what I am asking here is how should I configure ResourceConfig so the behavior keeps working (i.e, what properties to set)

P.S:我使用的是 Jetty 9.2.6.v20141205 和 Jersey 2.14.Maven 中的依赖关系:

P.S: i'm using Jetty 9.2.6.v20141205 and Jersey 2.14. Dependencies in Maven:

  • org.eclipse.jetty.jetty-server org.eclipse.jetty.jetty-servlet
  • org.eclipse.jetty.jetty-servlets
  • org.glassfish.jersey.containers.jersey-container-servlet-core
  • com.sun.jersey.jersey-json
  • org.glassfish.jersey.media.jersey-media-json-jackson

推荐答案

我不知道你是怎么做到的

I don't know how you got this to work

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

我无法简单地实例化 ServletContainer() 来生成一个工作示例.虽然我正要让它与下面的代码一起工作

I could not produce a working example simply instantiating the ServletContainer(). Though I was about to get it to work with the following code

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,因为它不是必需的.没有其他配置.资源类

Using all your dependencies, excluding the com.sun.jersey:jersey-json, as it's not needed. No other configuration. The resource class

@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

I'm curious to see how you got it to work without the ResourceConfig

我应该提到的另一件事是 jersey-container-servlet-core 应该为 jersey-container-servlet 切换.前者用于 2.5 容器支持,但后者推荐用于 3.x 容器.以我为例,它没有任何效果

Another thing I should mention is that jersey-container-servlet-core should be switched out for jersey-container-servlet. The former is for 2.5 container support, but the latter is recommended for 3.x containers. It not have any effect though, with my example

卷曲

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

世界

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

{"hello":"world"}

这篇关于创建一个与默认 Jetty 的 Jersey 注册行为方式相同的 ResourceConfig的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆