创建一个ResourceConfig,其行为与默认Jetty的Jersey注册相同 [英] Creating a ResourceConfig that behaves the same way as default Jetty's Jersey registering
问题描述
我有一个端点:
@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)
PS:我正在使用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
- 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;
}
}
.resources jetty.practice中的
包。
我很想知道如何在没有 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
cURL
C:\> curl http:// localhost:8080 / test -X POST -d{\hello \:\world \} - H内容类型:application / json
世界
C:\>curl http://localhost:8080/test -X POST -d "{\"hello\":\"world\"}" -H "Content-Type:application/json"
world
C:\> curl http:// localhost:8080 / test
{你好:世界}
这篇关于创建一个ResourceConfig,其行为与默认Jetty的Jersey注册相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!