jersey
  1. jersey
  2. JERSEY-2287

void resource method does not go through interceptor

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Works as designed
    • Affects Version/s: 2.4.1
    • Fix Version/s: None
    • Component/s: core
    • Labels:
      None
    • Environment:

      TOmcat 7

      Description

      I have one interceptor to rewrite the entity to give a common return structure

      @Override
      	public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
      		Object entity = context.getEntity();
                      
                      // this object has common stuff such as timestamp, user, status, etc.
      		RSRet ret = new RSRet();
      		if (entity instanceof RSRet || entity instanceof String) {
      			context.proceed();
      			return;
      		}
      		if (entity != null) {
      			if (entity.getClass().isArray()) {
      				ret.setList((Object[]) entity);
      			} else if (List.class.isAssignableFrom(entity.getClass())) {
      				List l = (List) entity;
      				ret.setList(l.toArray());
      			} else {
      				ret.setData(entity);
      			}
      		}
      		context.setEntity(ret);
      		context.proceed();
      
      	}
      
      

      but the resource method with void return skips this interceptor.

        Activity

        Hide
        Miroslav Fuksa added a comment -

        Hi,

        interceptors are executed only if the request/response contains entity. The primary intention of interceptors is to modify the entity stream (for example to ZIP the outgoing entity body). This is achieved mostly by wrapping the entity stream in the interceptor body. In your case when you don't return an entity ContainerResponseFilters are more appropriate. Filters are executed always even if there is no entity.

        Mira

        Show
        Miroslav Fuksa added a comment - Hi, interceptors are executed only if the request/response contains entity. The primary intention of interceptors is to modify the entity stream (for example to ZIP the outgoing entity body). This is achieved mostly by wrapping the entity stream in the interceptor body. In your case when you don't return an entity ContainerResponseFilters are more appropriate. Filters are executed always even if there is no entity. Mira
        Hide
        Miroslav Fuksa added a comment -

        I am closing the issue as interceptor should not really be executed for message contexts without any entity.

        Show
        Miroslav Fuksa added a comment - I am closing the issue as interceptor should not really be executed for message contexts without any entity.

          People

          • Assignee:
            Unassigned
            Reporter:
            jasonzhang2002gmailcom
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: