Jersey更新实体属性MessageBodyWriter

时间:2015-02-23 17:16:08

标签: java rest jersey jax-rs jersey-2.0

我想创建一个Jersey提供程序(MessageBodyWriter)来更新dto对象属性并继续链接到Jersey-json默认提供程序并返回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
    }
}

1 个答案:

答案 0 :(得分:2)

MessageBodyWriter不需要执行逻辑来操纵实体。它的责任就是编组/序列化。

您正在寻找的是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();
    }  
}

您可以添加注释,以便只有某些资源方法/类使用此拦截器,例如

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();
    }
}

如果由于某种原因你无法改变课程(也许这就是你需要在这里设置描述的原因,谁知道),那么你可以使用Dynamic Binding,你不需要使用注释。你可以简单地做一些反思来检查方法或类。该链接有一个例子。