【问题标题】:Errors while trying to make a Restful WebService with Jersey尝试使用 Jersey 创建 Restful WebService 时出错
【发布时间】:2013-04-20 21:00:57
【问题描述】:

我正在按照this 教程创建一个 CRUD RESTful WebService。

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

服务器似乎工作得非常好,因为我按照作者的建议对其进行了测试,并且一切都按预期工作。

然而,客户让我很难受。当我运行它时,我总是得到这个错误:

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)

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

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

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 网站上找到...


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

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

【问题讨论】:

  • 尝试创建 CRUD? :](我一直很喜欢那个。)
  • 是的!但是今天让我很难过:p

标签: java rest jersey webservice-client


【解决方案1】:

可能最简单的方法是使用 Jersey 的 POJO 支持功能。要启用它,请将 JSONConfiguration.FEATURE_POJO_MAPPING 初始化参数添加到 web.xml 的 servlet 配置中:

<servlet>
    <servlet-name>jersey-serlvet</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
                  ...
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
                  ...
    <load-on-startup>1</load-on-startup>
</servlet>

除了已有的罐子之外,您可能还需要添加几个罐子。检查此链接以获取依赖项列表:https://jersey.java.net/nonav/documentation/latest/chapter_deps.html#d4e1828

【讨论】:

    【解决方案2】:

    由于您收到 500 错误,这意味着代码在服务器端出错。您必须查看服务器端的日志才能找出问题所在。

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

    here

    编辑:

    因此,您映射到该 HTTP 请求的方法的返回类型似乎是 JSON,但您尚未注册 JSON 消息编写器供 Jersey 使用。

    【讨论】:

    • 嗯,我确实注意到代码中的错误与 Json 消息一致。但我只是按照教程进行操作:D 我该如何解决?
    • 尝试添加 jersey-json.jar 库
    • @DonP Google jersey-json.jar 这是第一个链接。
    【解决方案3】:

    原因:com.sun.jersey.api.MessageException:消息正文编写器 对于 Java 类 java.util.ArrayList 和 Java 类型 java.util.List 和 MIME 媒体类型 未找到应用程序/json

    我认为您的 es.unican.cityInfo.model.Todo 类缺少以下注释 @XmlRootElement

    package de.vogella.jersey.jaxb.model;
    
    import javax.xml.bind.annotation.XmlRootElement;
    
        @XmlRootElement // never forget me
        // JAX-RS supports an automatic mapping from JAXB annotated class to XML and JSON
        // Isn't that cool?
        public class Todo {
          private String summary;
          private String description;
          public String getSummary() {
            return summary;
          }
          public void setSummary(String summary) {
            this.summary = summary;
          }
          public String getDescription() {
            return description;
          }
          public void setDescription(String description) {
            this.description = description;
          }
    
    
        } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多