gradle 的球衣测试框架 - MessageBodyProviderNotFound
jersey testframework with gradle - MessageBodyProviderNotFound
我的处境有点困难。我正在使用 jersey 来提供 REST 服务。
在我的运行时,我使用以下版本执行此操作:1.16.0:
restClasspath(
'org.codehaus.jackson:jackson-core-asl:1.9.2',
'org.codehaus.jackson:jackson-mapper-asl:1.9.2',
'com.sun.jersey:jersey-core:1.16.0',
'com.sun.jersey:jersey-server:1.16.0',
)
这是我的资源class:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import ....model.DecisionTree;
import ...model.TestDataProvider;
@Path("new_rest")
public class NewPublicAPIRestResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("ppp/{param}")
public Response getHello(@PathParam("param") String treeName) {
DecisionTree tree = TestDataProvider.createDecisionTree();
return Response.ok(tree).build();
}
}
这有效,我得到了预期的 json 响应。
现在我发现了 jersey-testframework,它提供了一个用于集成测试的内存服务器。
不幸的是,这似乎只适用于球衣 2.x,所以我为我的测试添加了一个额外的 gradle 设置:
testClasspath(
'org.glassfish.jersey.core:jersey-common:2.7',
'org.glassfish.jersey.core:jersey-server:2.7',
'org.glassfish.jersey.core:jersey-client:2.7',
'org.glassfish.hk2:hk2:2.2.0',
'org.glassfish.jersey.test-framework.providers:jersey-test-framework-provider-inmemory:2.7',
)
这是我的测试class:
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;
import ...resource.NewPublicAPIRestResource;
public class ConditionNodeTest extends JerseyTest{
@Override
protected Application configure() {
return new ResourceConfig(NewPublicAPIRestResource.class);
}
@Test
public void test() {
System.out.println(target("new_rest/ppp/param").request().get(String.class));
}
}
只要我的资源 returns String
,此设置就可以工作。但是,一旦我 return a Response
,如我的代码所示,我就会收到此错误:
javax.ws.rs.ProcessingException: Server-side request processing failed with an error.
...
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:424)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:667)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:396)
at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:296)
at ....model.ConditionNodeTest.test(ConditionNodeTest.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
...
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
...
Caused by: org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class ....model.DecisionTree, genericType=class ....model.DecisionTree.
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:247)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
at org.glassfish.jersey.server.internal.JsonWithPaddingInterceptor.aroundWriteTo(JsonWithPaddingInterceptor.java:103)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:88)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1154)
at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:571)
at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:378)
at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:368)
at org.glassfish.jersey.server.ServerRuntime.run(ServerRuntime.java:262)
... 44 more
据我所知,我缺少一些将我的 classes 解析为 json 的提供程序,但我无法确定是哪一个。我尝试在我的 class 路径中添加一些库,例如:
'org.glassfish.jersey.media:jersey-media-jaxb:2.16'
'org.glassfish.jersey.media:jersey-media-json-jackson:2.7
还有其他几个我不记得了,但没有让它工作。
知道我在这里遗漏了什么吗?
谢谢,
斯文
这是正确的
'org.glassfish.jersey.media:jersey-media-json-jackson:2.7'
但是直到 Jersey 2.9,我们仍然需要在应用程序中注册 JacksonFeature
(如前所述 )。所以就做
@Override
protected Application configure() {
return new ResourceConfig(NewPublicAPIRestResource.class)
.register(JacksonFeature.class);
}
哦,顺便说一句,Jersey 1.x 也有 Jersey 测试框架。只是 API 有点不同,但功能还是一样的。请参阅示例 here and , and here is in-memory provider。但无论如何,我建议坚持使用 Jersey 2.x。
我的处境有点困难。我正在使用 jersey 来提供 REST 服务。
在我的运行时,我使用以下版本执行此操作:1.16.0:
restClasspath(
'org.codehaus.jackson:jackson-core-asl:1.9.2',
'org.codehaus.jackson:jackson-mapper-asl:1.9.2',
'com.sun.jersey:jersey-core:1.16.0',
'com.sun.jersey:jersey-server:1.16.0',
)
这是我的资源class:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import ....model.DecisionTree;
import ...model.TestDataProvider;
@Path("new_rest")
public class NewPublicAPIRestResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("ppp/{param}")
public Response getHello(@PathParam("param") String treeName) {
DecisionTree tree = TestDataProvider.createDecisionTree();
return Response.ok(tree).build();
}
}
这有效,我得到了预期的 json 响应。
现在我发现了 jersey-testframework,它提供了一个用于集成测试的内存服务器。
不幸的是,这似乎只适用于球衣 2.x,所以我为我的测试添加了一个额外的 gradle 设置:
testClasspath(
'org.glassfish.jersey.core:jersey-common:2.7',
'org.glassfish.jersey.core:jersey-server:2.7',
'org.glassfish.jersey.core:jersey-client:2.7',
'org.glassfish.hk2:hk2:2.2.0',
'org.glassfish.jersey.test-framework.providers:jersey-test-framework-provider-inmemory:2.7',
)
这是我的测试class:
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;
import ...resource.NewPublicAPIRestResource;
public class ConditionNodeTest extends JerseyTest{
@Override
protected Application configure() {
return new ResourceConfig(NewPublicAPIRestResource.class);
}
@Test
public void test() {
System.out.println(target("new_rest/ppp/param").request().get(String.class));
}
}
只要我的资源 returns String
,此设置就可以工作。但是,一旦我 return a Response
,如我的代码所示,我就会收到此错误:
javax.ws.rs.ProcessingException: Server-side request processing failed with an error.
...
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:424)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:667)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:396)
at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:296)
at ....model.ConditionNodeTest.test(ConditionNodeTest.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
...
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
...
Caused by: org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class ....model.DecisionTree, genericType=class ....model.DecisionTree.
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:247)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
at org.glassfish.jersey.server.internal.JsonWithPaddingInterceptor.aroundWriteTo(JsonWithPaddingInterceptor.java:103)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:88)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1154)
at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:571)
at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:378)
at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:368)
at org.glassfish.jersey.server.ServerRuntime.run(ServerRuntime.java:262)
... 44 more
据我所知,我缺少一些将我的 classes 解析为 json 的提供程序,但我无法确定是哪一个。我尝试在我的 class 路径中添加一些库,例如:
'org.glassfish.jersey.media:jersey-media-jaxb:2.16'
'org.glassfish.jersey.media:jersey-media-json-jackson:2.7
还有其他几个我不记得了,但没有让它工作。
知道我在这里遗漏了什么吗?
谢谢, 斯文
这是正确的
'org.glassfish.jersey.media:jersey-media-json-jackson:2.7'
但是直到 Jersey 2.9,我们仍然需要在应用程序中注册 JacksonFeature
(如前所述
@Override
protected Application configure() {
return new ResourceConfig(NewPublicAPIRestResource.class)
.register(JacksonFeature.class);
}
哦,顺便说一句,Jersey 1.x 也有 Jersey 测试框架。只是 API 有点不同,但功能还是一样的。请参阅示例 here and