java MessageBodyWriter 未找到 vogella 教程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26702196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MessageBodyWriter not found vogella tutorial
提问by Joe Murray
I am attempting to recreate the most excellent vogella tutorial for create REST with java, JAX-RS and Jersey.
我正在尝试重新创建最优秀的 vogella 教程,以使用 java、JAX-RS 和 Jersey 创建 REST。
I'm using eclipse Kepler with Java-EE perspective, tomcat 7.0.
我正在使用带有 Java-EE 透视图的 Eclipse Kepler,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 类,带有适当注释的 TodoResource 类并部署在 tomcat 7 上。我已按照说明将 jaxrs-ri 库导入到 WEB-INF/lib 文件夹中。
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:
带有注释的 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:
<?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:
测试.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:
对于 MediaType.APPLICATION_XML,一切都完美无缺——服务器返回:
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:
但是,对于 MediaType APPLICATION_JSON - 这是我真正需要的,我收到一个错误:
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 -
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.
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?
我的理解是 jaxrs-ri 2.13 包包含所有必需的东西,包括让我这样做的依赖项——而且我不需要添加任何类型的 JSON 提供程序。无论如何,我已经这样做了,例如,我尝试添加 gson,我已经下载了 moxy jars 并尝试将它们添加到我的 WEB-INF/lib 文件夹中并进行部署 - 一切都无济于事。我不知道我是完全陷入困境,还是缺少一些简单的东西?
回答by Paul Samsotha
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.
我的理解是 jaxrs-ri 2.13 包包含所有必需的东西,包括让我这样做的依赖项——而且我不需要添加任何类型的 JSON 提供程序。
That's actually incorrect. As stated at the Jersey User Guide 8.1. JSON
这其实是不正确的。如泽西岛用户指南 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 JSON 支持作为一组扩展模块提供,其中每个模块都包含需要注册到您的可配置实例(客户端/服务器)的功能的实现。有多种框架支持 JSON 处理和/或 JSON-to-Java 绑定。下面列出的模块通过将各个 JSON 框架集成到 Jersey 中来提供对 JSON 表示的支持。目前,Jersey 集成了以下模块来提供 JSON 支持:
MOXy- 通过 MOXy 的 JSON 绑定支持是自 Jersey 2.0 以来在您的 Jersey 应用程序中支持 JSON 绑定的默认和首选方式。当 JSON MOXy 模块位于类路径上时,Jersey 将自动发现该模块并在您的应用程序中通过 MOXy 无缝启用 JSON 绑定支持。(请参阅第 4.3 节,“自动发现功能”。)
在其他一些人中
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.
所以主要的 Jersey 下载不附带这些额外的模块。我们需要分别获取它们。话虽如此,获得所需的最简单方法jersey-media-moxy
是通过 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)
如果您不使用 Maven(查看教程,它没有使用),您将不得不对依赖项进行一些搜索。该jersey-media-moxy
工件有 16 个依赖项,但幸运的是,大多数都包含在 Jersey 发行版中。因此,在过滤掉 Jersey 发行版中已经包含的内容之后,这些是您必须自己找到的剩余 jar(我刚刚创建了一个用户库来测试)
Adding these dependencies will get the exampleup 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.
现在你有了 Eclipse,我假设它是随 Maven (m2e) 插件一起提供的。因此,获取这些依赖项的最简单方法可能是创建一个新的 Maven 项目,并添加如上所示的依赖项。构建项目后,maven 应该将所有额外的依赖项下载到本地 Maven Repo 中。只需从那里为您的主要项目抓住它们。
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.
- 泽西岛用户指南
- 我会下载Jersey 示例包,它比您正在使用的教程更新。
- 如果您不了解 Maven,我强烈建议您至少学习依赖管理的基础知识,并让构建框架为您抓取所有依赖项。示例包中的所有示例都使用 Maven,因此了解基础知识会有所帮助。
回答by Thamizharasu
Have you written any custom MessageBodyWriter for your marshalling from Java to JSON. If yes then you need to have the @Produces annotation with 'application/json' in the provider implementation. For more details refer http://h2labz.blogspot.in/2014/12/marshalling-java-to-json-in-jax-rs.html
您是否为从 Java 到 JSON 的编组编写了任何自定义 MessageBodyWriter。如果是,那么您需要在提供程序实现中使用带有“application/json”的 @Produces 注释。有关更多详细信息,请参阅http://h2labz.blogspot.in/2014/12/marshalling-java-to-json-in-jax-rs.html
回答by Harsh Maheswari
If your are using Jersey-2
如果您使用的是Jersey-2
Web.xml Configuration
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>
List of Libraries required
所需库列表
- org.eclipse.persistence.moxy-2.6.3.jar
- org.eclipse.persistence.core-2.6.3.jar
- org.eclipse.persistence.asm-2.6.3.jar
- org.eclipse.persistence.antlr-2.6.3.jar
- jersey-media-moxy-2.23.1.jar
- jersey-entity-filtering-2.23.1.jar
- org.eclipse.persistence.moxy-2.6.3.jar
- org.eclipse.persistence.core-2.6.3.jar
- org.eclipse.persistence.asm-2.6.3.jar
- org.eclipse.persistence.antlr-2.6.3.jar
- jersey-media-moxy-2.23.1.jar
- jersey-entity-filtering-2.23.1.jar
Run project it will work. Also check your JAXB classed because it will internally use xml annotation to convert pojo object to JAXB
运行项目它将起作用。还要检查您的 JAXB 分类,因为它将在内部使用 xml 注释将 pojo 对象转换为 JAXB