javax.ws.rs.container.ContainerResponseContext.getEntityAnnotations()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(6.8k)|赞(0)|评价(0)|浏览(117)

本文整理了Java中javax.ws.rs.container.ContainerResponseContext.getEntityAnnotations()方法的一些代码示例,展示了ContainerResponseContext.getEntityAnnotations()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ContainerResponseContext.getEntityAnnotations()方法的具体详情如下:
包路径:javax.ws.rs.container.ContainerResponseContext
类名称:ContainerResponseContext
方法名:getEntityAnnotations

ContainerResponseContext.getEntityAnnotations介绍

[英]Get the annotations attached to the entity instance.

Note that the returned annotations array contains only those annotations explicitly attached to entity instance (such as the ones attached using javax.ws.rs.core.Response.ResponseBuilder#entity(Object,java.lang.annotation.Annotation[]) method as well as the ones attached to the resource method that has returned the response). The entity instance annotations array does not include annotations declared on the entity implementation class or its ancestors.

Note that container response filters invoked earlier in the filter chain may modify the entity annotations value, in which case this getter method would return the last annotations value set by a container response filter invoked earlier in the filter chain.

For example:

@Path("my-resource") 
public class MyResource { 
private final Annotations[] extras = ... ; 
@GET 
@Custom 
public String getAnnotatedMe() { 
return Response.ok().entity("Annotated me", extras).build(); 
} 
... 
}

The container response context for a response returned from the getMe() method above would contain all the annotations declared on the getAnnotatedMe() method (@GET, @Custom) as well as all the annotations from the extras field, provided this value has not been replaced by any container response filter invoked earlier.

Similarly:

@Custom 
public class AnnotatedMe { ... } 
@Path("my-resource") 
public class MyResource { 
private final Annotations[] extras = ... ; 
@GET 
public AnnotatedMe getMe() { 
return Response.ok().entity(new AnnotatedMe(), extras).build(); 
} 
... 
}

Provided that the value has not been replaced by any container response filter invoked earlier, the container response context for a response returned from the getMe() method above would contain all the annotations on the getMe() method (@GET) as well as all the annotations from the extras field. It would however not contain any annotations declared on the AnnotatedMe class.
[中]获取附加到实体实例的注释。
请注意,返回的注释数组仅包含显式附加到实体实例的注释(例如使用javax.ws.rs.core.Response.ResponseBuilder#entity(Object,java.lang.annotation.annotation[])方法附加到已返回响应的资源方法的注释)。实体实例注释数组不包括在实体实现类或其祖先上声明的注释。
请注意,在过滤器链中较早调用的容器响应过滤器可能会修改实体注释值,在这种情况下,此getter方法将返回过滤器链中较早调用的容器响应过滤器设置的最后一个注释值。
例如:

@Path("my-resource") 
public class MyResource { 
private final Annotations[] extras = ... ; 
@GET 
@Custom 
public String getAnnotatedMe() { 
return Response.ok().entity("Annotated me", extras).build(); 
} 
... 
}

从上面的getMe()方法返回的响应的容器响应上下文将包含getAnnotatedMe()方法(@GET,@Custom)上声明的所有注释以及extras字段中的所有注释,前提是此值未被先前调用的任何容器响应筛选器替换。
同样地:

@Custom 
public class AnnotatedMe { ... } 
@Path("my-resource") 
public class MyResource { 
private final Annotations[] extras = ... ; 
@GET 
public AnnotatedMe getMe() { 
return Response.ok().entity(new AnnotatedMe(), extras).build(); 
} 
... 
}

如果该值未被先前调用的任何容器响应筛选器替换,则从上述getMe()方法返回的响应的容器响应上下文将包含getMe()方法(@GET)上的所有注释以及extras字段中的所有注释。但是,它不会包含在AnnotatedMe类上声明的任何注释。

代码示例

代码示例来源:origin: hugegraph/hugegraph

@Override
public void filter(ContainerRequestContext requestContext,
          ContainerResponseContext responseContext)
          throws IOException {
  if (responseContext.getStatus() == 200) {
    for (Annotation i : responseContext.getEntityAnnotations()) {
      if (i instanceof Status) {
        responseContext.setStatus(((Status) i).value());
        break;
      }
    }
  }
}

代码示例来源:origin: org.ligoj.bootstrap/bootstrap-business

@Override
public void filter(final ContainerRequestContext requestContext, final ContainerResponseContext responseContext) {
  if (responseContext.getStatus() == Status.NO_CONTENT.getStatusCode()) {
    // No entity returned
    for (final Annotation annotation : responseContext.getEntityAnnotations()) {
      if (annotation.annotationType() == OnNullReturn404.class) {
        // Explicit management of null result -> return a 404 status code
        replaceResponse(requestContext, responseContext);
        return;
      }
    }
  }
}

代码示例来源:origin: org.apache.rave/rave-core-api

@Override
  public void filter(ContainerRequestContext containerRequestContext, ContainerResponseContext containerResponseContext) throws IOException {
    if (containerResponseContext.getStatus() == Response.Status.OK.getStatusCode()) {

      Object o = containerResponseContext.getEntity();
      JsonResponseWrapper wrapper;

      Class clazz = o.getClass();
      if (List.class.isAssignableFrom(clazz)) {
        wrapper = new JsonResponseWrapper((List) o);
      } else if (SearchResult.class.isAssignableFrom(clazz)) {
        wrapper = new JsonResponseWrapper((SearchResult) o);
      } else {
        wrapper = new JsonResponseWrapper(o);
      }

      containerResponseContext.setEntity(wrapper, containerResponseContext.getEntityAnnotations(), containerResponseContext.getMediaType());
    }
  }
}

代码示例来源:origin: org.ligoj.bootstrap/bootstrap-business

/**
 * Set the entity response to a 404 JSON entity.
 */
private void replaceResponse(final ContainerRequestContext requestContext, final ContainerResponseContext responseContext) {
  final ServerError serverError = new ServerError();
  if (requestContext.getUriInfo().getPathParameters().size() == 1) {
    // Single parameter ---> entity identifier or whatever identifying a data
    serverError.setCode("entity");
    serverError.setMessage(requestContext.getUriInfo().getPathParameters().values().iterator().next().get(0));
  } else {
    serverError.setCode("data");
  }
  responseContext.setStatus(Status.NOT_FOUND.getStatusCode());
  responseContext.setEntity(toEntity(serverError), responseContext.getEntityAnnotations(), MediaType.APPLICATION_JSON_TYPE);
}

代码示例来源:origin: icode/ameba

responseContext.getEntityClass(),
          responseContext.getEntityType(),
          responseContext.getEntityAnnotations());
} else if (responseContext.getStatus() == 415) {
  types = workersProvider.get()
          responseContext.getEntityClass(),
          responseContext.getEntityType(),
          responseContext.getEntityAnnotations());

代码示例来源:origin: apache/cxf

@Override
public void filter(ContainerRequestContext requestContext,
          ContainerResponseContext responseContext) throws IOException {
  String ct = responseContext.getMediaType().toString();
  if (requestContext.getProperty("filterexception") != null) {
    if (!"text/plain".equals(ct)) {
      throw new RuntimeException();
    }
    responseContext.getHeaders().putSingle("FilterException",
                        requestContext.getProperty("filterexception"));
  }
  Object entity = responseContext.getEntity();
  Type entityType = responseContext.getEntityType();
  if (entity instanceof GenericHandler && InjectionUtils.getActualType(entityType) == Book.class) {
    ct += ";charset=ISO-8859-1";
    if ("getGenericBook2".equals(rInfo.getResourceMethod().getName())) {
      Annotation[] anns = responseContext.getEntityAnnotations();
      if (anns.length == 4 && anns[3].annotationType() == Context.class) {
        responseContext.getHeaders().addFirst("Annotations", "OK");
      }
    } else {
      responseContext.setEntity(new Book("book", 124L));
    }
  } else {
    ct += ";charset=";
  }
  responseContext.getHeaders().putSingle("Content-Type", ct);
  responseContext.getHeaders().add("Response", "OK");
}

相关文章