Angular2 support with HATEOAS

走远了吗. 提交于 2019-12-21 02:44:23

问题


I have a restful web service with the support of HATEOAS links. When I call "http://localhost:8080/v1/bookings/1225380?lock=true" link I got following resource URLs. I want to integrate these Hypermedia with my Angular2 application (recently upgraded to final version). I found few resources which were implemented with Angular1 with the support of Angular HAL (links - https://paulcwarren.wordpress.com/2015/04/03/role-based-spas-with-angularjs-and-spring-hateoas/, https://github.com/LuvDaSun/angular-hal/tree/master/src). But I am unable to found a resource for 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"
  }
]

回答1:


You can create an Injectable for this and use this class instead of the angular http class. Here you filter the links and than call http with the right link.

@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);
}

}

Provide this injector and add it to the constructor where you need it

constructor(private hypermedia:Hypermedia)

Then you can simply call it like you normally would call the http class

this.hypermedia.get(myHypermediaLinksArray,myRel)

Hope this helps:)



来源:https://stackoverflow.com/questions/39696760/angular2-support-with-hateoas

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!