我知道对于应用程序类型问题未找到错误“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)
我有一个自定义 Json 提供程序(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));