如何为json响应实现MessageBodyWriter方法writeTo? [英] how to implement MessageBodyWriter method writeTo for json response?
问题描述
如何配置 writeTo
方法,以便将详细信息转换为json响应。
How should I configure writeTo
method so that it convert detail to json response .
@Override
public void writeTo(Detail detail, Class<?> type, Type genericType, Annotation[] annotation, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException
{
try {
JAXBContext jaxbContext = JAXBContext.newInstance(detail.getClass());
// serialize the entity myBean to the entity output stream
jaxbContext.createMarshaller().marshal(detail, entityStream);
} catch (JAXBException jaxbException) {
jaxbException.printStackTrace();
throw new ProcessingException(
"Error serializing a "+ detail +" to the output stream", jaxbException);
}
}
但是现在我得到 XML
来自它的响应。
Yet now I am getting XML
response from it.
我的资源代码是:
@POST
@Produces({MediaType.APPLICATION_JSON})
@Path("testDetail")
public TestDetail testDetail()
{
TestDetail testDetail = new TestDetail();
return testDetail;
}
推荐答案
你为什么要写自己的 MessageBodyWriter
JSON?不要重新发明轮子。
Why are you writing your own MessageBodyWriter
for JSON? Don't reinvent the wheel.
使用一个与Jersey集成的JSON提供程序,它将为您提供 MessageBodyWriter
实施。在撰写本文时,Jersey集成了以下模块以提供JSON支持:
Use one JSON provider that integrates with Jersey and it will provide you a MessageBodyWriter
implementation. At time of writing, Jersey integrates with the following modules to provide JSON support:
- MOXy
- Java API for JSON Processing (JSON-P)
- Jackson
- Jettison
请参阅下面使用Jackson作为Jersey 2.x的JSON提供者所需的步骤:
See below the steps required to use Jackson as a JSON provider for Jersey 2.x:
添加Jackson模块依赖项
要使用Jackson 2.x作为您的JSON提供程序,您需要添加 jersey-media-json-jackson
模块到 pom.xml
file:
To use Jackson 2.x as your JSON provider you need to add jersey-media-json-jackson
module to your pom.xml
file:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.23.1</version>
</dependency>
使用Jackson 1.x它看起来像:
To use Jackson 1.x it'll look like:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson1</artifactId>
<version>2.23.1</version>
</dependency>
注册Jackson模块
除了添加上述依赖项外,您还需要注册 JacksonFeature
(或 Jackson1xature
//jersey.java.net/apidocs/2.23.1/jersey/javax/ws/rs/core/Application.html\"rel =nofollow> 应用程序
/ ResourceConfig
子类:
Besides adding the dependency mentioned above, you need to register JacksonFeature
(or Jackson1Feature
for Jackson 1.x) in your Application
/ResourceConfig
sub-class:
@ApplicationPath("/api")
public class MyApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(JacksonFeature.class);
return classes;
}
}
@ApplicationPath("/api")
public class MyApplication extends ResourceConfig {
public MyApplication() {
register(JacksonFeature.class);
}
}
如果你没有 申请
/ ResourceConfig
子类,你可以注册 web.xml 中的jersey / jackson / JacksonFeature.htmlrel =nofollow> JacksonFeature
部署描述符。特定资源,提供者和功能完全限定的类名称可以在逗号分隔值中提供 jersey.config.server.provider.classnames
初始化参数。
If you don't have an Application
/ResourceConfig
sub-class, you can register the JacksonFeature
in your web.xml
deployment descriptor. The specific resource, provider and feature fully-qualified class names can be provided in a comma-separated value of jersey.config.server.provider.classnames
initialization parameter.
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.jackson.JacksonFeature</param-value>
</init-param>
MessageBodyWriter
JacksonJsonProvider
。
有关详细信息,请查看Jersey 文档,关于对常见媒体类型表示的支持。
For more details, check the Jersey documentation about support for common media type representations.
这篇关于如何为json响应实现MessageBodyWriter方法writeTo?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!