users@jersey.java.net

[Jersey] Re: How about the access to the entity by Jersey Filter?

From: Kobayashi Satoshi <satoshi-k_at_iij.ad.jp>
Date: Tue, 13 Sep 2011 11:21:43 +0900

Hello

I tried in more detail.

(11/08/30 18:07), Kobayashi Satoshi wrote:
> Second, I tried ContainerResponseFilter
> The return value of ContainerResponse#getEntity() has become
> representation class object of the resource. yeah!
> But, it have no annotations that I need...

This was wrong in the method.
I obtained the result to expect by following code.

        @Override
        public ContainerResponse filter(ContainerRequest request,
                        ContainerResponse response) {
                Object entity = response.getEntity();
                Class<?> entityClass = entity.getClass();
                Field[] fields = entityClass.getDeclaredFields();
                for (Field field : fields) {
                        Annotation targetAnnotation =
field.getAnnotation(TargetAnnotation.class);
                                   ^
                                   *the annotation which wanted me*
                }
                response.setEntity(entity);
                return response;
        }

The field value of the entity can be re-written by the annotation at a
common filter.
I think that it is useful for filtering of a response!

> First, I tried ContainerRequestFilter
> The return value of ContainerRequest#getEntity() has become LinkedHashMap.
> # the representation of my API is JSON
> I was expecting to become representation class object (not
> LinkedHashMap) of the resource.
> Of course, LinkedHashMap have no annotations that I need.

But this is difficult to use ContainerRequestFilter for a validation.
Because ContainerRequest#getEntity needs a concrete class to an argument.
I would like to write abstractionally processing to a common filter.

I tried Object.class, a interface of the representation and a abstract
class of the representation to an argument.
They did not operate.
It seems that ContainerRequest#getEntity has solved a marshaller internally.
If it is right this spec may be unavoidable.

Thanks,

Satoshi