我正在尝试使用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...");
}
}
在客户端中注册杰克逊时出错:
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 + "]";
}
}
有人能帮我退回地图吗?
我发现您有两个不同的提供商依赖关系< code>jersey-media-moxy和< code > jersey-media-JSON-Jackson 。我一直对MOXy的< code >地图有问题。我猜他们打得不好。似乎你需要使用一个适配器来使它播放得更好,就像Blaise Doughan在这里看到的那样。
话虽如此,我请求(在上面的评论中)查看您的服务器端配置,因为MOXy将自动配置自己,并优先于jersey-media-json-jackson
。为什么,我不确定,这正是我一直经历的。所以我很好奇为什么服务器端甚至在响应返回给客户端之前就没有失败。除非您将JacksonFeature
与服务器应用程序混淆,否则您也应该将其与客户端一起配置。(仅限这种情况,因为您也有MOXy依赖项)。
无论如何,如果您不想经历创建适配器的麻烦,如上面的链接所示,只需摆脱泽西 - 媒体 - moxy
依赖性即可。泽西-媒体-json-杰克逊
也将自动配置它的功能。您应该只使用其中一个。
顺便说一句:
< code > @ Path("/getAllNews ")。路径段应该是名词,除非它们是控制器资源,通常执行一些动作,而不是正常的CRUD操作(例如< code>/register)。HTTP方法已经包含了每个CRUD操作应该执行的动词。
只需要考虑一些语义:-)
更新
问题出在< code>New键上。Jackson不知道如何反序列化这个。既然您希望切换键和值的类型,只需更改让< code>toString返回< code>Term类中的< code>name即可。
问题内容: 在尝试找出我的问题之后,我终于决定问您如何解决我的问题。我见过不同的人有相同的问题,我尝试了所有建议他们做的事情,但没有任何帮助解决我的问题。所以基本上我有一个使用Jersey进行构建的RESTful服务。对于我的客户,我想返回JSON格式的对象。我通读了不同的教程,并决定使用jersey- json-1.8库是有意义的。我像往常一样将所有内容添加到我的项目中,并尝试运行它,但是每次调
问题内容: 我正在尝试简单的Jersey + JSON示例,但出现以下错误 我放入以下jar文件以获得适当的结果 为什么会出现此类错误?错误日志在这里: web.xml JsonExample.java 和Json服务 如果我做错了事,请提出建议。 问题答案: 此问题已通过jersey-bundle-1.8.jar修复
我现在正在尝试泽西,按照这个链接在netbean中设置了一个网络服务。我有我的实体类和REST类。它可以从javafx2客户端添加、编辑、删除、请求对象(在本例中是用户对象)。 然而,现在我尝试向我的网络服务添加一个新方法来进行一些简单的身份验证。首先,我在Users.java文件中添加了一个新的命名查询(Users.login): 之后,我将以下代码添加到我的UsersFacadeREST.ja
我正在尝试遵循Jersey文档,以便在发生错误时启用非200响应(https://Jersey.java.net/documentation/latest/representations.html#D0E3586) 我的代码看起来像: 不幸的是,这会产生以下错误: [2015-02-01T16:13:02.157+0000][glassfish 4.1][严重][][org.glassfish.j
我在使用JAX-RS服务作为JSON时遇到了一些问题。
下面在我的restful资源类中定义了这两个方法。我在用泽西。但是当我试图运行一个单元时,它说错误,它们有相同的媒体类型。我是不是漏掉了什么? 严重:在资源和/或提供程序类中检测到以下错误和警告:严重:产生媒体类型冲突。资源方法public javax.ws.rs.core.response com.thomsonreuters.codes.sourcedocweb.resource.docume