泽西岛错误:找不到媒体类型 = 应用程序/json 的 MessageBodyReader

Posted

技术标签:

【中文标题】泽西岛错误:找不到媒体类型 = 应用程序/json 的 MessageBodyReader【英文标题】:Jersey Error: MessageBodyReader not found for media type=application/json 【发布时间】:2015-01-21 08:07:36 【问题描述】:

我正在尝试使用 Jersey 作为框架来实现一个小型 REST API,原则上代码运行良好,但是当我尝试对哈希表执行“GET”时,我收到以下错误:

nov 23, 2014 4:27:40 PM org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor aroundReadFrom
GRAVE: MessageBodyReader not found for media type=application/json, type=interface java.util.Map, genericType=java.util.Map<upf.dad.proyecto.New, upf.dad.proyecto.Term>.
1440 [DefaultQuartzScheduler_Worker-3] ERROR org.quartz.core.JobRunShell - Job DEFAULT.testJob3 threw an unhandled Exception: 
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=application/json, type=interface java.util.Map, genericType=java.util.Map<upf.dad.proyecto.New, upf.dad.proyecto.Term>.
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:230)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:154)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1124)
    at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:851)
    at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:810)
)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:313)
    at upf.dad.proyecto.HotTopicDetector.News(HotTopicDetector.java:110)
    at upf.dad.proyecto.ScheduledTestJob3.execute(ScheduledTestJob3.java:11)
1440 [DefaultQuartzScheduler_Worker-3] ERROR org.quartz.core.ErrorLogger - Job (DEFAULT.testJob3 threw an exception.
org.quartz.SchedulerException: Job threw an unhandled exception. [See nested exception: org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=application/json, type=interface java.util.Map, genericType=java.util.Map<upf.dad.proyecto.New, upf.dad.proyecto.Term>.]
    at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
Caused by: org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=application/json, type=interface java.util.Map, genericType=java.util.Map<upf.dad.proyecto.New, upf.dad.proyecto.Term>.
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:230)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:154)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1124)
    at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:851)
    at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:810)
    at org.glassfish.jersey.client.ClientResponse.readEntity(ClientResponse.java:368)
    at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:846)
    at org.glassfish.jersey.client.JerseyInvocation.access$600(JerseyInvocation.java:91)
    at org.glassfish.jersey.client.JerseyInvocation$3.call(JerseyInvocation.java:705)
    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:424)
    at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:701)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:417)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:313)
    at upf.dad.proyecto.HotTopicDetector.News(HotTopicDetector.java:110)
    at upf.dad.proyecto.ScheduledTestJob3.execute(ScheduledTestJob3.java:11)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
    ... 1 more

这是有问题的行:

    Client client = ClientBuilder.newClient();

    WebTarget targetGetAllNews = client.target("http://localhost:15000").path("news/getAllNews");


    Map<New, Term> NewsAll = targetGetAllNews.request(
             MediaType.APPLICATION_JSON_TYPE).get(new GenericType<Map<New, Term>>());

这就是我实现服务的方式:

@GET
@Path("/getAllNews")
@Produces(MediaType.APPLICATION_JSON)
public Map<New, Term> getAllNews() 
    return NewsCrawler.getNewAndTerm();

pom.xml(maven 依赖)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>upf.dad.proyecto</groupId>
<artifactId>JAXRS-proyecto</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- Rome RSS and Atom utilities @ http://rometools.github.io/rome/ -->        
<dependency>
        <groupId>rome</groupId>
        <artifactId>rome</artifactId>             
        <version>1.0</version>
</dependency>

<!-- jsoup html parser library @ http://jsoup.org/ -->         
<dependency>             
        <groupId>org.jsoup</groupId>             
        <artifactId>jsoup</artifactId>             
        <version>1.8.1</version>               
</dependency>
    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>2.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.6.1</version>
    </dependency>
    <dependency>
        <groupId>org.twitter4j</groupId>
        <artifactId>twitter4j-core</artifactId>
        <version>4.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.12</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-jdk-http</artifactId>
        <version>2.12</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
        <version>2.12</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>2.12</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.13</version>
    </dependency>
</dependencies>
</project>

服务器端配置:

public class NewsCrawlerRestServer 

public static void main(String[] args) throws IOException 

    URI baseUri = UriBuilder.fromUri("http://localhost/").port(15000).build();
    ResourceConfig config = new ResourceConfig(NewsCrawlerServices.class, HotTopicDetectorServices.class);
    config.register(JacksonFeature.class);
    HttpServer server = JdkHttpServerFactory.createHttpServer(baseUri, config);
    System.out.println("Server started...");

    

在客户端注册 Jackson 时出错:

org.quartz.SchedulerException: Job threw an unhandled exception. [See nested exception: javax.ws.rs.ProcessingException: Error reading entity from input stream.]
        at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
        at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
    Caused by: javax.ws.rs.ProcessingException: Error reading entity from input stream.
        at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:866)
        at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:810)
        at org.glassfish.jersey.client.ClientResponse.readEntity(ClientResponse.java:368)
        at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:846)
        at org.glassfish.jersey.client.JerseyInvocation.access$600(JerseyInvocation.java:91)
        at org.glassfish.jersey.client.JerseyInvocation$3.call(JerseyInvocation.java:705)
        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:424)
        at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:701)
        at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:417)
        at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:313)
        at upf.dad.proyecto.HotTopicDetector.News(HotTopicDetector.java:112)
        at upf.dad.proyecto.ScheduledTestJob3.execute(ScheduledTestJob3.java:11)
        at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
        ... 1 more
    Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not find a (Map) Key deserializer for type [simple type, class upf.dad.proyecto.New]
        at com.fasterxml.jackson.databind.deser.DeserializerCache._handleUnknownKeyDeserializer(DeserializerCache.java:580)
        at com.fasterxml.jackson.databind.deser.DeserializerCache.findKeyDeserializer(DeserializerCache.java:170)
        at com.fasterxml.jackson.databind.DeserializationContext.findKeyDeserializer(DeserializationContext.java:404)
        at com.fasterxml.jackson.databind.deser.std.MapDeserializer.createContextual(MapDeserializer.java:232)
        at com.fasterxml.jackson.databind.DeserializationContext.handleSecondaryContextualization(DeserializationContext.java:572)
        at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:386)
        at com.fasterxml.jackson.databind.ObjectReader._findRootDeserializer(ObjectReader.java:1380)
        at com.fasterxml.jackson.databind.ObjectReader._bind(ObjectReader.java:1228)
        at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:677)
        at com.fasterxml.jackson.jaxrs.base.ProviderBase.readFrom(ProviderBase.java:777)
        at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.invokeReadFrom(ReaderInterceptorExecutor.java:264)
        at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:234)
        at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:154)
        at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1124)
        at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:851)

新课程:

package upf.dad.proyecto;

import java.util.Date;

public class New 

private String tittle;
private String description;
private Date date;
private String link;

public New()



public New(String t, String d, Date date, String l)
    this.tittle = t;
    this.description = d;
    this.date = date;
    this.link = l;


public String getTittle() 
    return tittle;


public void setTittle(String tittle) 
    this.tittle = tittle;


public String getDescription() 
    return description;


public void setDescription(String description) 
    this.description = description;


public Date getDate() 
    return date;


public void setDate(Date date) 
    this.date = date;


public String getLink() 
    return link;


public void setLink(String link) 
    this.link = link;


@Override
public String toString() 
    return "New [tittle=" + tittle + ", description=" + description
            + ", date=" + date + ", link=" + link + "]";




术语类:

package upf.dad.proyecto;

public class Term 

private String word;

public Term()



public Term(String word)
    this.word = word;



public String getWord()
    return word;


public void setWord(String _word)
    word = _word;


@Override
public String toString() 
    return "Termino =" + word + "]";


有人可以帮我归还地图吗?

【问题讨论】:

我知道这是一个客户端问题,但你能显示服务器应用程序配置 【参考方案1】:

我看到您有两个不同的提供程序依赖项jersey-media-moxyjersey-media-json-jackson。我一直对使用 MOXy 的 Maps 有疑问。我猜他们打得不好。似乎您需要使用适配器才能使其发挥出色,如here from Blaise Doughan 所示。

话虽如此,我要求(在上面的 cmets 中)查看您的服务器端配置,因为 MOXy 将自动配置自身,并优先于 jersey-media-json-jackson。为什么,我不确定,这正是我一直所经历的。所以我很好奇服务器端是如何在响应返回客户端之前没有失败的。除非您将JacksonFeature 配置为服务器应用程序,否则您还应该使用客户端对其进行配置。 (只有这种情况,因为你也有 MOXy 依赖)。

无论如何,如果您不想麻烦地创建适配器,如上面的链接所示,只需摆脱 jersey-media-moxy 依赖项即可。 jersey-media-json-jackson 也将自动配置它的功能。您应该只使用其中一个。


顺便说一句:

@Path("/getAllNews")。路径段应该是名词,除非它们是控制器资源,通常执行一些操作,而不是正常的 CRUD 操作(例如/register)。 HTTP 方法已经包含了应该执行每个 CRUD 操作的动词。

Create - POST Retriecve - GET U更新日期 = PUT D删除 - 删除

只需要考虑一些语义:-)


更新

问题出在New 键上。杰克逊不知道如何反序列化它。既然你愿意切换key和value的类型,只要改变toStringTerm类中返回name就可以了。

【讨论】:

感谢您的回答,我用所需的信息更新了帖子。如您所见,要返回地图,我使用 Jackson 作为 JSON 提供程序,将依赖项添加到 pom.xml 文件中,并在启动 HttpServer 之前将其注册到 ResourceConfig 对象中,但仍然无法正常工作... 尝试向客户端注册它,因为问题出在客户端(因为问题出在客户端),即client.register(...)。由于您仍然拥有 MOXy,因此客户端可能已隐式注册到 MOXy。 再次感谢您的帮助,我用注册客户端时遇到的错误更新了帖子...我一直在考虑也使用适配器,但我不知道如何实现它. 请显示NewTerm 类。我创建了自己的测试,它运行良好。 您可以查看请求的课程,谢谢。能否举例说明如何使用GenericEntity

以上是关于泽西岛错误:找不到媒体类型 = 应用程序/json 的 MessageBodyReader的主要内容,如果未能解决你的问题,请参考以下文章

泽西岛的 JSON ArrayList

严重:找不到媒体类型 = 应用程序/json、类型 = 类 com.jersey.jaxb.Todo、通用类型 = 类 com.jersey.jaxb.Todo 的 MessageBodyWriter

找不到类型的响应对象的 MessageBodyWriter:媒体类型的 java.util.LinkedHashMap:application/json

找不到 MessageBodyWriter 类型的响应对象:com.sun.jersey.api.json.JSONWithPadding 媒体类型:application/x-javascript

找不到 Java 类 java.util.ArrayList 和 MIME 媒体类型 application/json 的消息正文编写器

严重:找不到 Java 类 java.util.ArrayList 和 MIME 媒体类型 application/json 的消息正文编写器