我正在使用 Jersey 2.17 和 android 来使用 Jersey 制作的 Web api。
我收到错误
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=application/json, type=class [Lcom.entities.Goal;, genericType=class [Lcom.entities.Goal;
我收到错误
public static Goal[] getGoals(String path){
WebTarget webResource = getClient().target(ROOT + path);
Invocation.Builder builder = webResource.request(MediaType.APPLICATION_JSON_TYPE);
return builder.get(Goal[].class); // this line gives error
}
我导入了库
compile 'org.glassfish.jersey.core:jersey-client:2.17'
我尝试添加一些 json 库,但大多数都会给我带来构建错误,例如多个 dex 等。
如果我选择 String.class 它将起作用,并且我将得到一个有效的字符串。 而且 Goal 类也有一个空的构造函数。
最佳答案
Erhannis 的评论是正确的并且对我有用。
创建 Client
时,您必须使用 ClientConfig
注册 JacksonJsonProvider
:
ClientBuilder.newClient(new ClientConfig(JacksonJsonProvider.class))
关于java - 找不到媒体类型 = application/json 的 MessageBodyReader - android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30291152/