如何在JerseyTest中将我的实体marhsall变为JSON?

时间:2016-02-23 00:03:34

标签: java json rest jersey jersey-test-framework

我有一些看起来像这样的测试代码......

    @Test
    public void testCreateWasteApprovalApplication() {
        final ManualWasteApprovalApplicationDto dto = new ManualWasteApprovalApplicationDto();
        dto.setDate(Instant.now().getMillis());
        dto.setWasteGeneratorName("wasteGeneratorName");
//... set a bunch more properties
        dto.setFacilityGroup("facgroup12");
        final Entity<ManualWasteApprovalApplicationDto> entity = Entity.entity(dto, MediaType.APPLICATION_JSON_TYPE);
        Response response = target("wasteapproval/1").request(MediaType.APPLICATION_JSON).put(entity);
        assertNotNull(response);
    }

当我尝试在Jersey Test中运行时,我看到以下堆栈跟踪。

org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class com.tervita.portal.wasteapproval.controller.ManualWasteApprovalApplicationDto, genericType=class com.tervita.portal.wasteapproval.controller.ManualWasteApprovalApplicationDto.

    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:227)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:149)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1139)
    at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:495)
    at org.glassfish.jersey.client.HttpUrlConnector._apply(HttpUrlConnector.java:270)
    at org.glassfish.jersey.client.HttpUrlConnector.apply(HttpUrlConnector.java:182)
    at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:227)
    at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:655)
    at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:652)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:422)
    at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:652)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:412)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.put(JerseyInvocation.java:306)
    at com.tervita.portal.wasteapproval.service.WasteApprovalApplicationControllerTest.testCreateWasteApprovalApplication(WasteApprovalApplicationControllerTest.java:54)
    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.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)

这是一种在运行泽西岛测试时将java对象编组为json的有效技术吗?我是否必须注册某种JSON编组器?

我在现有的Jersey服务中调用的方法有这种方法签名...

@POST
@Path("{id}")
public ManualWasteApprovalApplication updateApplication(@PathParam("id") Long id, ManualWasteApprovalApplicationDto dto) {

1 个答案:

答案 0 :(得分:2)

如果您还没有它,则需要JSON提供程序。只需使用杰克逊。

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>${jersey2.version}</version>
</dependency>

这需要在服务器中注册客户端。如果你使用的是比2.8更新的泽西版(即2.9+),那么你不需要注册任何东西,它应该自动注册。如果您使用的是旧版本,则需要注册JacksonFeature

对于客户

@Override
public void configureClient(ClientConfig config) {
    config.register(JacksonFeature.class);
}

对于服务器

@Override
public Application configure() {
    return new ResourceConfig(...).register(JacksonFeature.class);
}

如果您不使用Maven,请查看this post