gpt4 book ai didi

java - 在自定义提供程序的测试中找不到 MessageBodyProvider - 手动工作

转载 作者:行者123 更新时间:2023-11-30 06:13:05 25 4
gpt4 key购买 nike

我知道错误“MessageBodyProvider”未找到应用程序类型问题已经被问了很多次,但我的情况有点不同。

当我通过 Fiddler 或在浏览器中手动调用 REST API 时,REST 工作正常。所以这次测试似乎有些奇怪。我无法理解这个问题。

org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class xxxx.observer.Observer, genericType=class xxxxx.observer.Observer.
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:248)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:163)
at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1135) at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:248)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:163)
at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1135)
at org.glassfish.jersey.client.ClientRequest.doWriteEntity(ClientRequest.java:516)
at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:498)
at org.glassfish.jersey.client.internal.HttpUrlConnector._apply(HttpUrlConnector.java:384)
at org.glassfish.jersey.client.internal.HttpUrlConnector.apply(HttpUrlConnector.java:282)
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:278)
at org.glassfish.jersey.client.JerseyInvocation.lambda$invoke$0(JerseyInvocation.java:753)
at org.glassfish.jersey.internal.Errors.process(Errors.java:316)
at org.glassfish.jersey.internal.Errors.process(Errors.java:298)
at org.glassfish.jersey.internal.Errors.process(Errors.java:229)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:414)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:752)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:445)
at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:351)

我有一个Custom Json Provider (Gson)和我的测试类:

public class AppTest extends JerseyTest {
@Override
protected Application configure() {
String directory = "src/test/resources/de/xxxx";

ResourceConfig resourceConfig = new ResourceConfig(GenericExceptionMapper.class, ScenarioProtocolResource.class,
CloudlistResource.class, GsonJerseyProvider.class, CORSFilter.class, EncodingFilter.class,
GZipEncoder.class, DeflateEncoder.class);
Map<String, String> properties = new HashMap<>();

properties.put("directory", directory);
resourceConfig.setProperties(properties);

return resourceConfig;
}

在我的测试类中,我只是尝试执行 POST 请求:

Response resp = target("json/proto/observer/add/xxxxx").request(MediaType.APPLICATION_JSON).post(Entity.entity(observer, MediaType.APPLICATION_JSON));

最佳答案

您仅在服务器端的 configure() 方法中注册提供程序。您仍然需要在客户端注册它。您可以在堆栈跟踪中看到问题发生在客户端(请参阅包名称中的 client)。

您可以仅在 WebTarget 上注册提供程序,也可以重写 configureClient() 以将其注册到 Client

Response resp = target("json/proto/observer/add/xxxxx")
.register(GsonJsonProvider.class)
...

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

后者的好处是您无需在每次测试时都将其注册到 WebTarget

关于java - 在自定义提供程序的测试中找不到 MessageBodyProvider - 手动工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49820233/

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