How should I configure writeTo
method so that it convert detail to json response .
我应该如何配置writeTo方法,以便将细节转换为json响应。
@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);
}
}
Yet now I am getting XML
response from it.
然而现在我从中获得了XML响应。
MY resource code is:
我的资源代码是:
@POST
@Produces({MediaType.APPLICATION_JSON})
@Path("testDetail")
public TestDetail testDetail()
{
TestDetail testDetail = new TestDetail();
return testDetail;
}
2 个解决方案
#1
2
Why are you writing your own MessageBodyWriter
for JSON? Don't reinvent the wheel.
你为什么要为JSON编写自己的MessageBodyWriter?不要重新发明*。
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:
使用一个与Jersey集成的JSON提供程序,它将为您提供MessageBodyWriter实现。在撰写本文时,Jersey集成了以下模块以提供JSON支持:
- MOXy
- Java API for JSON Processing (JSON-P)
- Jackson
- Jettison
用于JSON处理的Java API(JSON-P)
Using Jackson as a JSON provider
See below the steps required to use Jackson as a JSON provider for Jersey 2.x:
请参阅下面使用Jackson作为Jersey 2.x的JSON提供程序所需的步骤:
Adding Jackson module dependencies
添加Jackson模块依赖项
To use Jackson 2.x as your JSON provider you need to add jersey-media-json-jackson
module to your pom.xml
file:
要将Jackson 2.x用作JSON提供程序,您需要将jersey-media-json-jackson模块添加到您的pom.xml文件中:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.23.1</version>
</dependency>
To use Jackson 1.x it'll look like:
要使用Jackson 1.x,它看起来像:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson1</artifactId>
<version>2.23.1</version>
</dependency>
Registering Jackson module
注册杰克逊模块
Besides adding the dependency mentioned above, you need to register JacksonFeature
(or Jackson1Feature
for Jackson 1.x) in your Application
/ResourceConfig
sub-class:
除了添加上面提到的依赖项之外,还需要在Application / ResourceConfig子类中注册JacksonFeature(或者Jackson1Feature for Jackson 1.x):
@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);
}
}
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.
如果您没有Application / ResourceConfig子类,则可以在web.xml部署描述符中注册JacksonFeature。特定资源,提供程序和功能完全限定的类名称可以以逗号分隔的值jersey.config.server.provider.classnames初始化参数提供。
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.jackson.JacksonFeature</param-value>
</init-param>
The MessageBodyWriter
provided by Jackson is JacksonJsonProvider
.
Jackson提供的MessageBodyWriter是JacksonJsonProvider。
For more details, check the Jersey documentation about support for common media type representations.
有关更多详细信息,请查看Jersey文档,了解对常见媒体类型表示的支持。
#2
-1
You shouldn't need to be writing your own serialization code. You are probably missing the jersey-media-json-jackson
artifact dependency. Look through this example.
您不应该编写自己的序列化代码。您可能缺少jersey-media-json-jackson工件依赖项。看看这个例子。
#1
2
Why are you writing your own MessageBodyWriter
for JSON? Don't reinvent the wheel.
你为什么要为JSON编写自己的MessageBodyWriter?不要重新发明*。
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:
使用一个与Jersey集成的JSON提供程序,它将为您提供MessageBodyWriter实现。在撰写本文时,Jersey集成了以下模块以提供JSON支持:
- MOXy
- Java API for JSON Processing (JSON-P)
- Jackson
- Jettison
用于JSON处理的Java API(JSON-P)
Using Jackson as a JSON provider
See below the steps required to use Jackson as a JSON provider for Jersey 2.x:
请参阅下面使用Jackson作为Jersey 2.x的JSON提供程序所需的步骤:
Adding Jackson module dependencies
添加Jackson模块依赖项
To use Jackson 2.x as your JSON provider you need to add jersey-media-json-jackson
module to your pom.xml
file:
要将Jackson 2.x用作JSON提供程序,您需要将jersey-media-json-jackson模块添加到您的pom.xml文件中:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.23.1</version>
</dependency>
To use Jackson 1.x it'll look like:
要使用Jackson 1.x,它看起来像:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson1</artifactId>
<version>2.23.1</version>
</dependency>
Registering Jackson module
注册杰克逊模块
Besides adding the dependency mentioned above, you need to register JacksonFeature
(or Jackson1Feature
for Jackson 1.x) in your Application
/ResourceConfig
sub-class:
除了添加上面提到的依赖项之外,还需要在Application / ResourceConfig子类中注册JacksonFeature(或者Jackson1Feature for Jackson 1.x):
@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);
}
}
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.
如果您没有Application / ResourceConfig子类,则可以在web.xml部署描述符中注册JacksonFeature。特定资源,提供程序和功能完全限定的类名称可以以逗号分隔的值jersey.config.server.provider.classnames初始化参数提供。
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.jackson.JacksonFeature</param-value>
</init-param>
The MessageBodyWriter
provided by Jackson is JacksonJsonProvider
.
Jackson提供的MessageBodyWriter是JacksonJsonProvider。
For more details, check the Jersey documentation about support for common media type representations.
有关更多详细信息,请查看Jersey文档,了解对常见媒体类型表示的支持。
#2
-1
You shouldn't need to be writing your own serialization code. You are probably missing the jersey-media-json-jackson
artifact dependency. Look through this example.
您不应该编写自己的序列化代码。您可能缺少jersey-media-json-jackson工件依赖项。看看这个例子。