gpt4 book ai didi

java - 如何在 JerseyTest 中将我的实体编码为 JSON?

转载 作者:行者123 更新时间:2023-11-30 08:40:37 25 4
gpt4 key购买 nike

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

    @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)

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

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

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

最佳答案

如果您还没有,则需要一个 JSON 提供程序。只需使用 Jackson。

<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,请查看 this post

关于java - 如何在 JerseyTest 中将我的实体编码为 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35566372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com