2014-11-02 55 views
3

我正在嘗試重新創建用於使用java,JAX-RS和Jersey創建REST的最優秀的vogella教程。MessageBodyWriter未找到vogella教程

我使用eclipse Kepler和Java-EE透視圖,tomcat 7.0。

我已經創建了Todo類,TodoResource類以及相應的註釋並部署在tomcat 7上。我已按照指示將jaxrs-ri庫導入到WEB-INF/lib文件夾中。

Todo類:

package com.vogella.jersey.jaxb.model; 

import javax.xml.bind.annotation.XmlRootElement; 

@XmlRootElement 
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; 
    } 


} 

TodoResource與註釋:

package com.vogella.jersey.jaxb.model; 

import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.MediaType; 

@Path("/todo") 
public class TodoResource { 
    // This method is called if XMLis request 
    @GET 
    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) 
    public Todo getXML() { 
    Todo todo = new Todo(); 
    todo.setSummary("This is my first todo"); 
    todo.setDescription("This is my first todo"); 
    return todo; 
    } 

    // This can be used to test the integration with the browser 
    @GET 
    @Produces({ MediaType.TEXT_XML }) 
    public Todo getHTML() { 
    Todo todo = new Todo(); 
    todo.setSummary("This is my first Todo"); 
    todo.setDescription("This is my first Todo"); 
    return todo; 
    } 

} 

的web.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 
    <display-name>com.vogella.jersey.first</display-name> 
    <servlet> 
    <servlet-name>Jersey REST Service</servlet-name> 
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> 
    <init-param> 
     <param-name>jersey.config.server.provider.packages</param-name> 
     <param-value>com.vogella.jersey.jaxb</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>Jersey REST Service</servlet-name> 
    <url-pattern>/rest/*</url-pattern> 
    </servlet-mapping> 
</web-app> 

我也創建了客戶的指示。

Test.java:

package com.vogella.jersey.first.client; 

import java.net.URI; 
import javax.ws.rs.client.Client; 
import javax.ws.rs.client.ClientBuilder; 
import javax.ws.rs.client.WebTarget; 
import javax.ws.rs.core.MediaType; 
import javax.ws.rs.core.Response; 
import javax.ws.rs.core.UriBuilder; 

import org.glassfish.jersey.client.ClientConfig; 
import org.glassfish.jersey.client.ClientResponse; 
import org.glassfish.jersey.message.internal.MediaTypes; 

public class Test { 

    public static void main(String[] args) { 



    ClientConfig config = new ClientConfig(); 

    Client client = ClientBuilder.newClient(config); 

    WebTarget target = client.target(getBaseURI()); 


    System.out.println(target.path("rest").path("todo").request() 

      .accept(MediaType.APPLICATION_XML).get(Response.class) 

      .toString()); 

    System.out.println(target.path("rest").path("todo").request() 

      .accept(MediaType.APPLICATION_JSON).get(Response.class) 

      .toString()); 


    } 

    private static URI getBaseURI() { 

     return UriBuilder.fromUri("http://localhost:8080/com.vogella.jersey.jaxb").build(); 

    } 
} 

一切完美的MediaType.APPLICATION_XML - 服務器返回:

InboundJaxrsResponse{ClientResponse{method=GET, uri=http://localhost:8080/com.vogella.jersey.jaxb/rest/todo, status=200, reason=OK}} 

然而,對於的MediaType APPLICATION_JSON - 這是我真正需要的,我得到一個錯誤:

InboundJaxrsResponse{ClientResponse{method=GET, uri=http://localhost:8080/com.vogella.jersey.jaxb/rest/todo, status=500, reason=Internal Server Error}} 

Tomcat清楚地顯示了我的問題 - 它似乎對我來說,不知道怎麼回事JSON響應 -

Nov 02, 2014 11:59:19 AM org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo 
SEVERE: MessageBodyWriter not found for media type=application/json, type=class com.vogella.jersey.jaxb.model.Todo, genericType=class com.vogella.jersey.jaxb.model.Todo. 

我的理解是,JAXRS裏2.13軟件包中包含所需的一切,包括依賴關係,讓我做到這一點 - 而且我也不需要添加任何類型的JSON提供程序。無論如何,我已經這麼做了,我嘗試添加gson,例如,我已經下載了moxy jar並嘗試將它們添加到我的WEB-INF/lib文件夾中並部署 - 都無濟於事。我不知道我是否完全脫離雜草,或者我錯過了簡單的事情?

回答

5

My understanding is that the jaxrs-ri 2.13 bundle includes everything required including dependencies to let me do this - and that I don't need to add any kind of JSON provider.

這實際上是不正確的。截至Jersey User Guide 8.1. JSON

Jersey JSON support comes as a set of extension modules where each of these modules contains an implementation of a Feature that needs to be registered into your Configurable instance (client/server). There are multiple frameworks that provide support for JSON processing and/or JSON-to-Java binding. The modules listed below provide support for JSON representations by integrating the individual JSON frameworks into Jersey. At present, Jersey integrates with the following modules to provide JSON support:

  • MOXy - JSON binding support via MOXy is a default and preferred way of supporting JSON binding in your Jersey applications since Jersey 2.0. When JSON MOXy module is on the class-path, Jersey will automatically discover the module and seamlessly enable JSON binding support via MOXy in your applications. (See Section 4.3, 「Auto-Discoverable Features」.)

  • Among a few others

陳述所以主要澤西下載不來與這些額外的模塊。我們需要分別獲得它們。話雖如此,獲得所需jersey-media-moxy最簡單的方法是通過Maven。

<dependency> 
    <groupId>org.glassfish.jersey.media</groupId> 
    <artifactId>jersey-media-moxy</artifactId> 
    <version>2.13</version> 
</dependency> 

如果你不使用Maven(其中翻翻the tutorial,事實並非如此),你將不得不做的依賴性一些搜索。 jersey-media-moxy工件有16個依賴關係,但幸運的是,大部分都包含在澤西島發行版中。所以過濾掉了已經包含在新澤西發行後,這些剩下的罐子,你必須找到你自己(我剛剛創建了一個用戶庫來測試)

enter image description here

添加這些依賴關係會讓the example啓動並運行。測試和添加這些後按預期工作。

現在你有了Eclipse,我認爲它帶有Maven(m2e)插件。因此,獲取這些依賴關係的最簡單方法是創建一個新的Maven項目,並添加上面顯示的依賴關係。在構建項目之後,maven應該將所有額外的依賴項下載到本地Maven Repo中。只需從那裏抓住你的主要項目。


其他資源/ Notes的

  • Jersey User Guide
  • 我將下載Jersey Example package,這更是最新的,則教程中所使用。
  • 如果您不瞭解Maven,我強烈建議至少學習依賴管理的基礎知識,讓構建框架爲您抓取所有的依賴關係。示例包中的所有示例都使用Maven,所以它將有助於瞭解基礎知識。
+0

非常感謝 - 您的答案非常重要。我會花一些時間學習Maven,因爲你提到 - 這很容易,因爲我使用eclipse,它肯定看起來很光滑。再次感謝。 – 2014-11-03 22:29:28

0

如果您正在使用新澤西州2

web.xml配置

<servlet-name>Jersey REST Service</servlet-name> 
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servletclass> 

    <init-param> 
     <param-name>jersey.config.server.provider.packages</param-name> 
     <param-value>com.oracle.restful</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 

庫列表需要

  1. org.eclipse.persistence .moxy-2.6.3.jar
  2. org.eclipse.persistence.core-2.6.3.jar
  3. org.eclipse.persistence.asm-2.6.3.jar
  4. org.eclipse.persistence.antlr-2.6.3.jar
  5. 球衣-media-MOXY-2.23.1.jar
  6. 球衣實體 - 過濾 - 2.23.1.jar

運行項目,將工作。還請檢查您的JAXB分類,因爲它會在內部使用xml註釋將POJO對象轉換爲JAXB

相關問題