当前位置: 首页 > news >正文

北京汽车网站建设快三直播app下载平台

北京汽车网站建设,快三直播app下载平台,seort什么意思,微信官方免费下载在上一篇文章中 #xff0c;我们研究了使用MOXy的功能来控制特定实体的数据输出级别。 这篇文章着眼于Jersey 2.x提供的抽象#xff0c;它允许您定义一组自定义的批注以具有相同的效果。 与之前一样#xff0c;我们几乎没有什么琐碎的资源可以返回Jersey将为我们转换为JSON… 在上一篇文章中 我们研究了使用MOXy的功能来控制特定实体的数据输出级别。 这篇文章着眼于Jersey 2.x提供的抽象它允许您定义一组自定义的批注以具有相同的效果。 与之前一样我们几乎没有什么琐碎的资源可以返回Jersey将为我们转换为JSON的对象请注意目前此代码中没有任何内容可以进行过滤-我不会将注释传递给 Response对象如Jersey示例中所示 import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces;Path(hello) public class SelectableHello {GETProduces({ application/json; leveldetailed, application/json; levelsummary, application/json; levelnormal })public Message hello() {return new Message();} } 在我的设计中我将定义四个注释 NoView SummaryView NormalView和DetailedView 。 所有根对象都必须实现NoView注释以防止暴露未注释的字段-您可能觉得在设计中没有必要。 所有这些类看起来都一样所以我只显示一个。 请注意创建AnnotationLiteral的工厂方法必须优先于创建动态代理以具有相同效果的工厂使用。 2.5中有代码将忽略由java.lang.reflect.Proxy对象实现的任何注释其中包括您可能从类中检索到的所有注释。 我正在为此提交修复程序。 import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;import javax.enterprise.util.AnnotationLiteral;import org.glassfish.jersey.message.filtering.EntityFiltering;Target({ ElementType.TYPE, ElementType.METHOD, ElementType.FIELD }) Retention(RetentionPolicy.RUNTIME) Documented EntityFiltering public interface NoView {/*** Factory class for creating instances of the annotation.*/public static class Factory extends AnnotationLiteralNoView implements NoView {private Factory() {}public static NoView get() {return new Factory();}}} 现在我们可以快速浏览一下Message Bean这比我以前的示例稍微复杂一点以非常简单的形式显示子图的过滤。 正如我之前说过的那样在类的根部使用NoView注释进行注释–这应该意味着privateData不会返回给客户端因为它没有特别地注释。 import javax.xml.bind.annotation.XmlRootElement;XmlRootElement NoView public class Message {private String privateData;SummaryViewprivate String summary;NormalViewprivate String message;DetailedViewprivate String subtext;DetailedViewprivate SubMessage submessage;public Message() {summary Some simple summary;message This is indeed the message;subtext This is the deep and meaningful subtext;submessage new SubMessage();privateData The fox is flying tonight;}// Getters and setters not shown }public class SubMessage {private String message;public SubMessage() {message Some sub messages;}// Getters and setters not shown } 如前所述资源类中没有代码可以处理过滤–我认为这是一个横切关注点因此我将其抽象为WriterInterceptor。 请注意如果使用的实体上没有NoView批注则会抛出该异常。 import java.io.IOException;import java.lang.annotation.Annotation;import java.util.Arrays; import java.util.LinkedHashSet; import java.util.Set;import javax.ws.rs.ServerErrorException; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Context; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.ext.Provider; import javax.ws.rs.ext.WriterInterceptor; import javax.ws.rs.ext.WriterInterceptorContext;Provider public class ViewWriteInterceptor implements WriterInterceptor {private HttpHeaders httpHeaders;public ViewWriteInterceptor(Context HttpHeaders httpHeaders) {this.httpHeaders httpHeaders;}Overridepublic void aroundWriteTo(WriterInterceptorContext writerInterceptorContext) throws IOException,WebApplicationException {// I assume this case will never happen, just to be sureif (writerInterceptorContext.getEntity() null) {writerInterceptorContext.proceed();return;}else{Class? entityType writerInterceptorContext.getEntity().getClass();String entityTypeString entityType.getName();// Ignore any Jersey system classes, for example wadl//if (entityType String.class || entityType.isArray() || entityTypeString.startsWith(com.sun) || entityTypeString.startsWith(org.glassfish)) {writerInterceptorContext.proceed();return;}// Fail if the class doesnt have the default NoView annotation // this prevents any unannotated fields from showing up//else if (!entityType.isAnnotationPresent(NoView.class)) {throw new ServerErrorException(Entity type should be tagged with NoView annotation entityType, Response.Status.INTERNAL_SERVER_ERROR);}}// Get hold of the return media type://MediaType mt writerInterceptorContext.getMediaType();String level mt.getParameters().get(level);// Get the annotations and modify as required//SetAnnotation current new LinkedHashSet();current.addAll(Arrays.asList(writerInterceptorContext.getAnnotations()));switch (level ! null ? level : ) {default:case detailed:current.add(com.example.annotation.DetailedView.Factory.get());case normal:current.add(com.example.annotation.NormalView.Factory.get());case summary:current.add(com.example.annotation.SummaryView.Factory.get());}writerInterceptorContext.setAnnotations(current.toArray(new Annotation[current.size()]));//writerInterceptorContext.proceed();} } 最后您必须手动启用EntityFilterFeature为此您可以在Application类中简单地注册它 import java.lang.annotation.Annotation;import javax.ws.rs.ApplicationPath;import org.glassfish.jersey.message.filtering.EntityFilteringFeature; import org.glassfish.jersey.server.ResourceConfig;ApplicationPath(/resources/) public class SelectableApplication extends ResourceConfig {public SelectableApplication() {packages(...);// Set entity-filtering scope via configuration.property(EntityFilteringFeature.ENTITY_FILTERING_SCOPE, new Annotation[] {NormalView.Factory.get(), DetailedView.Factory.get(), NoView.Factory.get(), SummaryView.Factory.get()});register(EntityFilteringFeature.class);}} 一旦一切就绪并运行应用程序将像以前一样响应 GET .../hello Accept application/json; leveldetailed or application/json {message : This is indeed the message,submessage : {message : Some sub messages},subtext : This is the deep and meaningful subtext,summary : Some simple summary }GET .../hello Accept application/json; levelnormal {message : This is indeed the message,summary : Some simple summary }GET .../hello Accept application/json; levelsummary {summary : Some simple summary } 这是直接使用MOXy批注的更好选择–使用自定义批注应该更容易将应用程序移植到过度实现中即使您必须提供自己的过滤器也是如此。 最后值得探讨的是Jersey扩展它允许基于角色的筛选 我认为这在安全方面很有用。 参考 通过改变内容类型来选择返回的详细程度第二部分来自我们的JCG合作伙伴 Gerard Davison位于Gerard Davison的博客博客中。 翻译自: https://www.javacodegeeks.com/2014/02/selecting-level-of-detail-returned-by-varying-the-content-type-part-ii.html
http://www.lebaoying.cn/news/116241.html

相关文章:

  • 游戏网站开发目的网站购物车怎么做
  • 南京电商网站建设公司排名网站建设业务流程
  • 中国产品网百度seo综合查询
  • 最成功的个人网站广东手机网站建设价格低
  • 泉州自助建站系统wordpress相似推荐
  • 网站定制功能免费适合个人主页
  • 网站开发人员需要具备的能力wordpress里的发消息给我
  • 石家庄建设网站哪家好建站系统模板
  • 网站编辑怎么做的简述网站的创建流程
  • 蛋糕 网站 模板html5素材网站
  • 什么网站可以做期刊封面友联互换
  • 贵州网站建设吧公司网站的推广
  • 网站邮箱登陆代码网站建设两年免费维护
  • 淮安住房与城乡建设部网站网站付费怎么做
  • 三视觉设计网站黄山市住房城乡建设厅网站
  • 织梦网站主页大田县建设资讯网站
  • 免费的网站后台iis默认网站在哪里
  • 常州中小企业网站制作平台搭建大概多少钱
  • 公司建设电商型网站的作用查询网站流量的网址
  • lisp 网站开发汕头新闻头条最新消息
  • 网站内容规划怎么写网络公司推广公司
  • 安徽元鼎建设工程 网站网站建设整改落实情况
  • 响应式网站布局网站建设公司销售经理职责
  • 申请免费网站注册宿州做网站公司
  • 网站备案为什么这么慢网站产品页面
  • 软件定制一条龙成都做网站优化价格
  • 网络平台推广有哪些渠道网站改版的seo注意事项
  • 如何做钓鱼网站扫码支付个人商城网站开发免费
  • 自己做网站给自己淘宝引流wordpress 微商网站
  • 杭州网站推广怎样做世界工厂网怎么拿货