如何在 JerseyTest 中将我的实体编组为 JSON?
How can I make my entity marhsall to JSON in JerseyTest?
我有一些看起来像这样的测试代码...
@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 测试中尝试 运行 时,我看到了以下堆栈跟踪。
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.call(JerseyInvocation.java:655)
at org.glassfish.jersey.client.JerseyInvocation.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.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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.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) {
如果您还没有,则需要 JSON 提供商。就用杰克逊吧。
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey2.version}</version>
</dependency>
这需要同时向服务器和客户端注册。如果您使用的 Jersey 版本高于 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,请查看
我有一些看起来像这样的测试代码...
@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 测试中尝试 运行 时,我看到了以下堆栈跟踪。
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.call(JerseyInvocation.java:655)
at org.glassfish.jersey.client.JerseyInvocation.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.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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.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) {
如果您还没有,则需要 JSON 提供商。就用杰克逊吧。
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey2.version}</version>
</dependency>
这需要同时向服务器和客户端注册。如果您使用的 Jersey 版本高于 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,请查看