java Jersey JSON 序列化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26518996/
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
Jersey JSON serialization
提问by KGolbang
I set up an eclipse WebApp project and placed Jersey and Hymanson JARs in the WEB-INF/lib directory. I want to use JSON serialization but didn't manage to fix this error:
我设置了一个 Eclipse WebApp 项目并将 Jersey 和 Hymanson JAR 放在 WEB-INF/lib 目录中。我想使用 JSON 序列化,但未能修复此错误:
MessageBodyWriter not found for media type=application/json, type=class com.rest.Greeting, genericType=class com.rest.Greeting. I already googled much but all the solutions are updated or don't solve my issue. Here is the structure of my project:
The WEB-INF/libfolder contains following JARs:
在WEB-INF / lib目录文件夹包含以下JAR:
aopalliance-repackaged-2.3.0-b10.jar
asm-debug-all-5.0.2.jar
cglib-2.2.2.jar
datafactory-0.8.jar
hk2-api-2.3.0-b10.jar
hk2-locator-2.3.0-b10.jar
hk2-utils-2.3.0-b10.jar
Hymanson-annotations-2.4.0.jar
Hymanson-core-2.4.0.jar
Hymanson-databind-2.4.0.jar
Hymanson-jaxrs-base-2.4.0.jar
Hymanson-jaxrs-json-provider-2.4.0.jar
Hymanson-module-jaxb-annotations-2.4.0.jar
javassist-3.18.1-GA.jar
javax.annotation-api-1.2.jar
javax.inject-2.3.0-b10.jar
javax.servlet-api-3.0.1.jar
javax.ws.rs-api-2.0.1.jar
jaxb-api-2.2.7.jar
jersey-client.jar
jersey-common.jar
jersey-container-servlet-core.jar
jersey-container-servlet.jar
jersey-guava-2.13.jar
jersey-server.jar
org.osgi.core-4.2.0.jar
osgi-resource-locator-1.0.1.jar
persistence-api-1.0.jar
validation-api-1.1.0.Final.jar
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>Rest Test</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Greeting</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.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Greeting</servlet-name>
<url-pattern>/greet/*</url-pattern>
</servlet-mapping>
</web-app>
Greeting.java:
问候.java:
@XmlRootElement
@Path("/greeting")
public class Greeting {
private String greeting;
public Greeting() {
this.setGreeting("hello");
}
@GET
@Produces(MediaType.TEXT_XML)
public Greeting sayHello() {
return new Greeting();
}
public String getGreeting() {
return greeting;
}
public void setGreeting(String greeting) {
this.greeting = greeting;
}
}
The WebApp project is run in an embedded Tomcat v7 server (view "Servers" in eclipse). The request:
WebApp 项目在嵌入式 Tomcat v7 服务器中运行(在 Eclipse 中查看“服务器”)。 请求:
http://localhost:8080/com.rest/greet/greeting
As long as I use MediaType.TEXT_XMLthere is no error and the content looks like this:
只要我使用MediaType.TEXT_XML就没有错误,内容如下所示:
<greeting>
<greeting>hello</greeting>
</greeting>
When I change MediaType to "MediaType.APPLICATION_JSON"then the following exception (as described above) is thrown. I already noticed that it depends on the annotation "@XmlRootElement" but there is not something like "JsonRootElement" and the Hymanson/genson APIs claim that it works out of the box (due to the auto registration mechanism). Do you have any idea how to fix it?
当我将 MediaType 更改为“MediaType.APPLICATION_JSON”时,会引发以下异常(如上所述)。我已经注意到它取决于注释“@XmlRootElement”,但没有像“JsonRootElement”这样的东西,Hymanson/genson API 声称它开箱即用(由于自动注册机制)。你知道如何解决它吗?
Every help is very very appreciated! thank you golbie
每一个帮助都非常非常感谢!谢谢你
回答by Lukasz Wiktor
All you need is to register HymansonJsonProvider
. There are many ways to achieve that:
您所需要的只是注册HymansonJsonProvider
。有很多方法可以实现:
- Register the HymansonJsonProvider explicitly in web.xml:
- 在 web.xml 中显式注册 HymansonJsonProvider:
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>com.fasterxml.Hymanson.jaxrs.json.HymansonJsonProvider</param-value>
</init-param>
or
或者
- Register your class extending
javax.ws.rs.core.Application
in the web.xml
- 注册您
javax.ws.rs.core.Application
在 web.xml 中扩展的类
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.rest.MyApplication</param-value>
</init-param>
and then do all the configuration in the application class:
然后在应用程序类中进行所有配置:
package com.rest;
import org.glassfish.jersey.server.ResourceConfig;
import com.fasterxml.Hymanson.jaxrs.json.HymansonJsonProvider;
public class MyApplication extends ResourceConfig {
public MyApplication() {
packages("com.rest");
register(HymansonJsonProvider.class);
}
ResourceConfig
is a subclass of javax.ws.rs.Application
and gives you some helper methods that makes the registration easy.
ResourceConfig
是 的子类,javax.ws.rs.Application
并为您提供一些使注册变得容易的辅助方法。
or
或者
- Use automatic registration. Just add dependency to
jersey-media-json-Hymanson
- 使用自动注册。只需添加依赖
jersey-media-json-Hymanson
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-Hymanson</artifactId>
<version>2.13</version>
</dependency>
But be careful. It will register more than you need:
不过要小心。它将注册比您需要的更多:
- HymansonJaxbJsonProvider,
- JsonParseExceptionMapper,
- JsonMappingExceptionMapper
- HymansonJaxbJsonProvider,
- JsonParseExceptionMapper,
- JsonMappingExceptionMapper
Take a look at the source codeto see what it does.
查看源代码以了解它的作用。
回答by mendieta
Have you tried adding this to your web.xml?
您是否尝试将其添加到您的 web.xml 中?
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
I believe this tutorial shows what you are trying yo do http://examples.javacodegeeks.com/enterprise-java/rest/jersey/json-example-with-jersey-Hymanson/
我相信本教程显示了您正在尝试做的事情 http://examples.javacodegeeks.com/enterprise-java/rest/jersey/json-example-with-jersey-Hymanson/
Another one from mkyong http://www.mkyong.com/webservices/jax-rs/json-example-with-jersey-Hymanson/
另一个来自 mkyong http://www.mkyong.com/webservices/jax-rs/json-example-with-jersey-Hymanson/