泽西岛更新实体 属性 MessageBodyWriter
Jersey Update Entity Property MessageBodyWriter
我想创建一个 Jersey 提供程序 (MessageBodyWriter) 来更新 dto 对象 属性 并继续链到 Jersey-json 默认提供程序和 return json 对象。 问题 似乎没有调用默认提供程序,因此在我注册新提供程序后,我的其余服务的输出变为空。
@Provider
public class TestProvider implements MessageBodyWriter<MyDTO>
{
@Override
public long getSize(
MyDTO arg0, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4)
{
return 0;
}
@Override
public boolean isWriteable(Class<?> clazz, Type type, Annotation[] arg2, MediaType arg3)
{
return type == MyDTO.class;
}
@Override
public void writeTo(
MyDTO dto,
Class<?> paramClass,
Type paramType, Annotation[] paramArrayOfAnnotation,
MediaType mt,
MultivaluedMap<String, Object> paramMultivaluedMap,
OutputStream entityStream) //NOPMD
throws IOException, WebApplicationException
{
dto.setDescription("text Description");
// CONTINUE THE DEFAULT SERIALIZATION PROCESS
}
}
MessageBodyWriter
不需要执行逻辑来操作实体。它的职责就是 marshal/serialize.
您正在寻找的是一个 WriterIntercptor
,其目的是准确地执行您想要执行的操作,在序列化之前操纵实体。
都解释完了here in the Jersey Doc for Inteceptors。
这是一个例子
@Provider
public class MyDTOWriterInterceptor implements WriterInterceptor {
@Override
public void aroundWriteTo(WriterInterceptorContext context)
throws IOException, WebApplicationException {
Object entity = context.getEntity();
if (entity instanceof MyDTO) {
((MyDTO)entity).setDescription("Some Description");
}
context.proceed();
}
}
您可以添加注释,以便只有特定资源 methods/classes 使用此拦截器,例如
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.ws.rs.NameBinding;
@NameBinding
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface AddDescription {
}
...
@AddDescription
@Provider
public class MyDTOWriterInterceptor implements WriterInterceptor {
...
@Path("dto")
public class MyDTOResource {
@GET
@AddDescription
@Produces(MediaType.APPLICATION_JSON)
public Response getDto() {
return Response.ok(new MyDTO()).build();
}
}
如果由于某种原因您无法更改 class(也许这就是您需要在此处设置描述的原因,谁知道呢),那么您可以在不需要的地方使用 Dynamic Binding不需要使用注释。您可以简单地做一些反思来检查方法或 class。 link有个例子。
我想创建一个 Jersey 提供程序 (MessageBodyWriter) 来更新 dto 对象 属性 并继续链到 Jersey-json 默认提供程序和 return json 对象。 问题 似乎没有调用默认提供程序,因此在我注册新提供程序后,我的其余服务的输出变为空。
@Provider
public class TestProvider implements MessageBodyWriter<MyDTO>
{
@Override
public long getSize(
MyDTO arg0, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4)
{
return 0;
}
@Override
public boolean isWriteable(Class<?> clazz, Type type, Annotation[] arg2, MediaType arg3)
{
return type == MyDTO.class;
}
@Override
public void writeTo(
MyDTO dto,
Class<?> paramClass,
Type paramType, Annotation[] paramArrayOfAnnotation,
MediaType mt,
MultivaluedMap<String, Object> paramMultivaluedMap,
OutputStream entityStream) //NOPMD
throws IOException, WebApplicationException
{
dto.setDescription("text Description");
// CONTINUE THE DEFAULT SERIALIZATION PROCESS
}
}
MessageBodyWriter
不需要执行逻辑来操作实体。它的职责就是 marshal/serialize.
您正在寻找的是一个 WriterIntercptor
,其目的是准确地执行您想要执行的操作,在序列化之前操纵实体。
都解释完了here in the Jersey Doc for Inteceptors。
这是一个例子
@Provider
public class MyDTOWriterInterceptor implements WriterInterceptor {
@Override
public void aroundWriteTo(WriterInterceptorContext context)
throws IOException, WebApplicationException {
Object entity = context.getEntity();
if (entity instanceof MyDTO) {
((MyDTO)entity).setDescription("Some Description");
}
context.proceed();
}
}
您可以添加注释,以便只有特定资源 methods/classes 使用此拦截器,例如
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.ws.rs.NameBinding;
@NameBinding
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface AddDescription {
}
...
@AddDescription
@Provider
public class MyDTOWriterInterceptor implements WriterInterceptor {
...
@Path("dto")
public class MyDTOResource {
@GET
@AddDescription
@Produces(MediaType.APPLICATION_JSON)
public Response getDto() {
return Response.ok(new MyDTO()).build();
}
}
如果由于某种原因您无法更改 class(也许这就是您需要在此处设置描述的原因,谁知道呢),那么您可以在不需要的地方使用 Dynamic Binding不需要使用注释。您可以简单地做一些反思来检查方法或 class。 link有个例子。