Joe*_*ray 4 java json tomcat jax-rs jersey-2.0
我正在尝试使用java,JAX-RS和Jersey重新创建用于创建REST的最优秀的vogella教程.
我正在使用带有Java-EE透视的eclipse Kepler,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;
}
}
Run Code Online (Sandbox Code Playgroud)
带注释的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;
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
我也按照指示创建了客户端.
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();
}
}
Run Code Online (Sandbox Code Playgroud)
一切都适用于MediaType.APPLICATION_XML - 服务器返回:
InboundJaxrsResponse{ClientResponse{method=GET, uri=http://localhost:8080/com.vogella.jersey.jaxb/rest/todo, status=200, reason=OK}}
Run Code Online (Sandbox Code Playgroud)
但是,对于MediaType APPLICATION_JSON - 这是我实际需要的,我收到一个错误:
InboundJaxrsResponse{ClientResponse{method=GET, uri=http://localhost:8080/com.vogella.jersey.jaxb/rest/todo, status=500, reason=Internal Server Error}}
Run Code Online (Sandbox Code Playgroud)
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.
Run Code Online (Sandbox Code Playgroud)
我的理解是jaxrs-ri 2.13包包含了所有必需的东西,包括让我这样做的依赖 - 而且我不需要添加任何类型的JSON提供程序.无论如何,我已经这样做了,我已经尝试添加gson,例如,我已经下载了moxy jar并试图将它们添加到我的WEB-INF/lib文件夹并进行部署 - 所有都无济于事.我不知道我是不是完全在杂草中,或者我是否遗漏了一些简单的东西?
我的理解是jaxrs-ri 2.13包包含了所有必需的东西,包括让我这样做的依赖 - 而且我不需要添加任何类型的JSON提供程序.
这实际上是错误的.正如Jersey用户指南8.1所述.JSON
Jersey JSON支持作为一组扩展模块提供,其中每个模块都包含需要注册到可配置实例(客户端/服务器)的功能实现.有多个框架为JSON处理和/或JSON到Java绑定提供支持.下面列出的模块通过将各个JSON框架集成到Jersey中来提供对JSON表示的支持.目前,Jersey集成了以下模块以提供JSON支持:
MOXy - 通过MOXy的JSON绑定支持是自Jersey 2.0以来在Jersey应用程序中支持JSON绑定的默认和首选方式.当JSON MOXy模块位于类路径上时,Jersey将自动发现模块并通过MOXy在应用程序中无缝启用JSON绑定支持.(参见第4.3节"自动发现功能".)
其中一些
所以主要的泽西岛下载不附带这些额外的模块.我们需要单独获得它们.话虽这么说,获得要求的最简单方法jersey-media-moxy是通过Maven.
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.13</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
如果您没有使用Maven(查看教程,但没有),您将不得不搜索依赖项.该jersey-media-moxy工件有16个依赖项,但幸运的是,大多数都包含在Jersey发行版中.因此,在筛选出已经包含在Jersey发行版中的内容之后,这些是你必须自己找到的剩余罐子(我刚创建了一个用户库来测试)

添加这些依赖项将使示例启动并运行.添加这些后,测试并按预期工作.
现在你有了Eclipse,我认为它带有Maven(m2e)插件.因此,获取这些依赖项的最简单方法是创建一个新的Maven项目,并添加上面显示的依赖项.在构建项目之后,maven应该将所有额外的依赖项下载到您当地的Maven Repo中.只需从那里抓住他们的主要项目.
其他资源/说明
| 归档时间: |
|
| 查看次数: |
9935 次 |
| 最近记录: |