加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Angular2支持HATEOAS

发布时间:2020-12-17 17:08:00 所属栏目:安全 来源:网络整理
导读:在HATEOAS链接的支持下,我有一个安静的Web服务.我打电话的时候 “ http://localhost:8080/v1/bookings/1225380?lock=true” 链接我得到了以下资源URL.我想将这些Hypermedia与我的Angular2应用程序(最近升级到最终版本)集成.我发现Angular1在Angular HAL的支
在HATEOAS链接的支持下,我有一个安静的Web服务.我打电话的时候
“ http://localhost:8080/v1/bookings/1225380?lock=true”
链接我得到了以下资源URL.我想将这些Hypermedia与我的Angular2应用程序(最近升级到最终版本)集成.我发现Angular1在Angular HAL的支持下实现的资源很少(链接 – https://paulcwarren.wordpress.com/2015/04/03/role-based-spas-with-angularjs-and-spring-hateoas/,https://github.com/LuvDaSun/angular-hal/tree/master/src).但我无法找到Angular2的资源.

"links": [
  {
    "rel": "client","href": "http://localhost:8080/v1/clients/10000"
  },{
    "rel": "passengers","href": "http://localhost:8080/v1/bookings/1225380/passengers"
  },{
    "rel": "itinerary","href": "http://localhost:8080/v1/bookings/1225380/itinerary"
  },{
    "rel": "self","href": "http://localhost:8080/v1/bookings/1225380?lock=true"
  },{
    "rel": "clientBookingHistory","href": "http://localhost:8080/v1/bookings/1225380/clientBookingHistory/10000"
  }
]

解决方法

您可以为此创建一个Injectable,并使用此类而不是角度http类.在这里,您可以过滤链接,然后使用正确的链接调用http.

@Injectable() 
export class Hypermedia {

   constructor(private http: Http) { }

   get(links: any[],rel: String,body?: any,options?: RequestOptionsArgs): Observable<Response> {
    var link = null;
    var request = null;

    // Find the right link
    links.forEach(function (_link) {
        if (_link.rel === rel) {
            link = _link
            return;
        }
    });

    return this.http.get(link.href);
}

}

提供此注入器并将其添加到您需要它的构造函数中

constructor(private hypermedia:Hypermedia)

然后你可以简单地调用它,就像你通常会调用http类一样

this.hypermedia.get(myHypermediaLinksArray,myRel)

希望这可以帮助:)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读