MessageBodyWriter找不到vogella教程 [英] MessageBodyWriter not found vogella tutorial

查看:89
本文介绍了MessageBodyWriter找不到vogella教程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用java,JAX-RS和Jersey重新创建用于创建REST的最优秀的vogella教程。



我正在使用eclipse Kepler和Java-EE透视图,tomcat 7.0。



我创建了Todo类,带有相应注释的TodoResource类并部署在tomcat 7上。我已将jaxrs-ri库导入WEB-INF / lib文件夹按指示。



Todo class:

  package com.vogella.jersey.jaxb 。模型; 

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Todo {
private String summary;
private字符串描述;
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)
公共类TodoResource {
//如果XML请求
@GET
@Produces,则调用此方法{MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
public Todo getXML(){
Todo todo = new Todo();
todo.setSummary(这是我的第一个待办事项);
todo.setDescription(这是我的第一个待办事项);
返回todo;
}

//这可以用来测试与浏览器的集成
@GET
@Produces({MediaType.TEXT_XML})
public Todo getHTML(){
Todo todo = new Todo();
todo.setSummary(这是我的第一个Todo);
todo.setDescription(这是我的第一个Todo);
返回todo;
}

}

web.xml:

 <?xml version =1.0encoding =UTF-8?> 
< web-app xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexmlns =http://java.sun.com/xml/ns/javaeexsi :schemaLocation =http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsdid =WebApp_IDversion =3.0 >
< display-name> com.vogella.jersey.first< / display-name>
< servlet>
< servlet-name> Jersey REST服务< / 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服务< / 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 static void main(String [] args){



ClientConfig config = new ClientConfig();

客户端客户端= 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());


}

私有静态URI getBaseURI(){

返回UriBuilder.fromUri(http:// localhost:8080 / com.vogella.jersey.jaxb)建立();

}
}

一切都适用于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 =内部服务器错误}} 

Tomcat清楚地向我显示了这个问题 - 在我看来它不知道如何返回JSON响应 -

  2014年11月2日上午11:59:19 org.glassfish.jersey.message.internal.WriterInterceptorExecutor $ TerminalWriterInterceptor aroundWriteTo 
SEVERE:找不到媒体类型= application / json的MessageBodyWriter,类型= class com.vogella.jers ey.jaxb.model.Todo,genericType = class com.vogella.jersey.jaxb.model.Todo。

我的理解是jaxrs-ri 2.13包中包含了所有必需的内容,包括让我这样做的依赖项 - 并且我不需要添加任何类型的JSON提供程序。无论如何我已经这样做了,我已经尝试添加gson,例如,我已经下载了moxy jar并试图将它们添加到我的WEB-INF / lib文件夹并进行部署 - 但都无济于事。我不知道我是不是完全没杂草,或者我错过了一些简单的东西?

解决方案


我的理解是jaxrs-ri 2.13包中包含了所有必需的内容,包括允许我这样做的依赖项 - 而且我不需要添加任何类型的JSON提供程序。


这实际上是不正确的。如。在添加这些之后测试并按预期工作。



现在你有了Eclipse,我认为它带有Maven(m2e)插件。因此,获取这些依赖项的最简单方法是创建一个新的Maven项目,并添加上面显示的依赖项。在构建项目之后,maven应该将所有额外的依赖项下载到您当地的Maven Repo中。只需从那里为你的主项目抓住它们。






其他资源/注释




  • Jersey用户指南

  • 我会下载泽西示例包,这比你正在使用的教程更新。

  • 如果你不认识Maven,我强烈建议至少学习依赖管理的基础知识,让构建框架为您获取所有依赖项。示例包中的所有示例都使用Maven,因此有助于了解基础知识。


I am attempting to recreate the most excellent vogella tutorial for create REST with java, JAX-RS and Jersey.

I'm using eclipse Kepler with Java-EE perspective, tomcat 7.0.

I have create the Todo class, the TodoResource class with the appropriate annotations and deployed on tomcat 7. I have imported the jaxrs-ri libs into the WEB-INF/lib folder as instructed.

Todo class:

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 with annotations:

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>

I have also created the client as instructed.

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();

  }
}

Everything works perfectly for the MediaType.APPLICATION_XML - the server returns:

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

However, for the MediaType APPLICATION_JSON - which is what I actually need, I get an error:

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

Tomcat clearly shows me the problem - it seems to me it doesn't know how to return a JSON response -

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.

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. I have done so anyway, I have tried adding gson for example, I have downloaded the moxy jars and attempting to add them to my WEB-INF/lib folder and deploy - all to no avail. I don't know if I'm completely out in the weeds, or if I'm missing something simple?

解决方案

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.

That's actually incorrect. As stated at the 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

So the main Jersey download doesn't come with these extra modules. We need to obtain them separately. That being said, the easiest way to get the required jersey-media-moxy is through Maven.

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

If you're not using Maven (which looking through the tutorial, it doesn't), you're going to have to do some searching for the dependencies. The jersey-media-moxy artifact has 16 dependencies, but Fortunately, most are contained within the Jersey distribution. So after filtering out what was already included in the Jersey distro, these are the remaining jars you will have to find on your own (I just created a User Library to test out)

Adding these dependencies will get the example up and running. Tested and works as expected after adding these.

Now you have Eclipse, which I assume came with the Maven (m2e) plugin. So maybe the easiest way to get these dependencies is to create a new Maven project, and add the dependency shown above. After you build the project, maven should download all the extra dependencies into your local Maven Repo. Just grab them from there for your main project.


Other Resources/Notes

  • Jersey User Guide
  • I would download the Jersey Example package, which is more up to date then the tutorial you are using.
  • If you don't know Maven, I would strongly suggest learning at least the basic of dependency management, and let the build framework grab all the dependencies for you. Also all the examples in the examples package uses Maven, so it would help to know the basics.

这篇关于MessageBodyWriter找不到vogella教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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