簡體   English   中英

嘗試使用Jersey創建Restful WebService時出錯

[英]Errors while trying to make a Restful WebService with Jersey

我按照教程創建了一個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 ,我在網站上發現...


這里建議的是來自服務器的日志,當我運行客戶端時:

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

可能簡單的方法是使用Jersey的POJO支持功能。 要啟用它,請將JSONConfiguration.FEATURE_POJO_MAPPING init參數添加到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>

除了你已經擁有的jar之外,你可能還需要添加幾個jar。 檢查此鏈接以獲取依賴項列表: https//jersey.java.net/nonav/documentation/latest/chapter_deps.html#d4e1828

由於您收到500錯誤,這意味着代碼在服務器端出錯。 您將不得不查看服務器端的日志以找出問題所在。

HTTP狀態代碼旨在為您提供有關請求的信息。

看到這里

編輯:

因此,映射到該HTTP請求的方法上的返回類型似乎是JSON,但您尚未為Jersey注冊JSON消息編寫器以供使用。

引起:com.sun.jersey.api.MessageException:Java類java.util.ArrayList的消息體編寫器,Java類型java.util.List和MIME媒體類型application / 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;
      }


    } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM