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

MessageBodyProvider not found in Test for Custom provider - Manually works

我知道 "MessageBodyProvider" not found for application type 问题已经问了很多次了,但我的情况有点不同。

当我通过 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[=10=](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) 和我的测试 class:

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;
    }

在我的测试中 class 我只是尝试做一个 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 注册它。