尝试使用Jersey制作Restful Web服务时发生错误 [英] Errors while trying to make a Restful WebService with Jersey

查看:105
本文介绍了尝试使用Jersey制作Restful Web服务时发生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照教程创建CRUD RESTful Web服务.

I am following this tutorial to create a CRUD RESTful WebService.

此类WebService的教程从第7段开始.我创建了服务器和客户端.

The tutorial for this type of WebService starts in paragraph 7. I create the Server and the Client.

正如我按照作者建议的那样进行测试,服务器似乎工作正常,一切正常.

The Server seems to work perfectly fine as i do what the author suggests to test it and everything works as expected.

但是,客户让我很难受.当我运行它时,我总是会收到此错误:

However , the Client gives me a hard time. When i run it i always get this error:

201
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><todoes><todo><id>3</id><summary>Blabla</summary></todo><todo><description>Read complete http://www.vogella.com</description><id>2</id><summary>Do something</summary></todo><todo><description>Read http://www.vogella.com/articles/REST/article.html</description><id>1</id><summary>Learn REST</summary></todo></todoes>
Exception in thread "main" com.sun.jersey.api.client.UniformInterfaceException: GET http://localhost:8080/CRUDserver/rest/todos returned a response status of 500 Internal Server Error
    at com.sun.jersey.api.client.WebResource.handle(WebResource.java:686)
    at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
    at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:507)
    at es.unican.cityInfo.client.Tester.main(Tester.java:34)

我无法理解的问题到底是什么?有什么想法吗?

What exactly is the problem i cant understand ? Any ideas ?

我唯一能做的假设是基于建立客户的开始时作者的这一句话:

The only assumption i can make , is based in this phrase of the author , in the beginning of building the client :

Create a new Java project called de.vogella.jersey.todo.client. Create a lib folder and place all jersey libs in this folder. Add the jars to the classpath of the project.

他说将所有 jersey libs 放在一个文件夹中.但是这些库是什么??? 我为客户拥有的唯一东西是2个jar文件:jersey-client.jarjersey-core.jar,这些文件是我在jersey网站上发现的...

He says to place all jersey libs inside a folder. However what are these libs??? The only thing i have for the client are 2 jar files : jersey-client.jar and jersey-core.jar which i found on the site of jersey...

这里建议的是当我运行客户端时来自服务器的日志:

As suggested here is the log from the server , when i run the client :

Apr 26, 2013 5:46:19 PM com.sun.jersey.spi.container.ContainerResponse write
SEVERE: A message body writer for Java class java.util.ArrayList, and Java type java.util.List<es.unican.cityInfo.model.Todo>, and MIME media type application/json was not found
Apr 26, 2013 5:46:19 PM com.sun.jersey.spi.container.ContainerResponse write
SEVERE: The registered message body writers compatible with the MIME media type are:
*/* ->
  com.sun.jersey.core.impl.provider.entity.FormProvider
  com.sun.jersey.core.impl.provider.entity.StringProvider
  com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
  com.sun.jersey.core.impl.provider.entity.FileProvider
  com.sun.jersey.core.impl.provider.entity.InputStreamProvider
  com.sun.jersey.core.impl.provider.entity.DataSourceProvider
  com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
  com.sun.jersey.core.impl.provider.entity.ReaderProvider
  com.sun.jersey.core.impl.provider.entity.DocumentProvider
  com.sun.jersey.core.impl.provider.entity.StreamingOutputProvider
  com.sun.jersey.core.impl.provider.entity.SourceProvider$SourceWriter
  com.sun.jersey.server.impl.template.ViewableMessageBodyWriter
  com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
  com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General

Apr 26, 2013 5:46:19 PM com.sun.jersey.spi.container.ContainerResponse logException
SEVERE: Mapped exception to response: 500 (Internal Server Error)
javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class java.util.ArrayList, and Java type java.util.List<es.unican.cityInfo.model.Todo>, and MIME media type application/json was not found
    at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:285)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1479)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1391)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1381)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:662)
Caused by: com.sun.jersey.api.MessageException: A message body writer for Java class java.util.ArrayList, and Java type java.util.List<es.unican.cityInfo.model.Todo>, and MIME media type application/json was not found
    ... 24 more

推荐答案

由于出现500错误,这意味着代码在服务器端出现错误.您将不得不查看服务器端的日志以找出问题所在.

Since you are getting a 500 error, this means the code is erroring on the server side. You will have to look at the log on the server side to figure out what went wrong.

HTTP状态代码旨在为您提供有关请求的信息.

HTTP Status Codes are meant to give you information about the request.

请参见此处

因此,看来映射到该HTTP请求的方法的返回类型为JSON,但尚未注册供Jersey使用的JSON消息编写器.

So it appears that your return type on the method being mapped to that HTTP request is JSON, but you have not registered a JSON message writer for Jersey to use.

这篇关于尝试使用Jersey制作Restful Web服务时发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆