REST 서비스에서 요청 전달

5130 단어 cspxmlpracticejson
REST 프레임워크의 유용한 기능 중 하나는 디스패치 클래스가 요청 접두사를 식별하고 이를 다른 디스패치 클래스로 전달하는 기능입니다. URL 맵을 모듈화하는 이러한 접근 방식은 코드 가독성을 개선하고 별도의 인터페이스 버전을 쉽게 유지 관리할 수 있게 하며 특정 사용자만 액세스할 수 있는 API 호출을 보호하는 수단을 제공합니다.

개요



Caché 인스턴스에 REST 서비스를 설정하려면 전용 CSP 애플리케이션을 정의하고 들어오는 요청을 처리하는 관련 디스패치 클래스를 생성해야 합니다. 디스패치 클래스는 %CSP.REST를 확장하고 URL 맵이 포함된 XData 블록을 포함합니다. 이것은 특정 요청이 수신될 때 호출할 메소드를 시스템에 알려줍니다.

예를 들어:
XData UrlMap [ XMLNamespace = "http://www.intersystems.com/urlmap" ]
{
<Routes>
  <Route Url="/orders" Method="GET" Call="GetOrders"/>
  <Route Url="/orders" Method="POST" Call="NewOrder"/>
</Routes>
}

The <Route> elements define the various requests that the service will handle. A GET request for the resource "/orders" will call into the classmethod "GetOrders". A POST request made for the same resource will instead call into the "NewOrder" method.

It’s important to note that the CSP application name is not considered as part of the requested resource name in our URL map. Consider a request made to the address:

http://localhost:57772/csp/demo/orders

If our CSP application is called "/csp/demo", then the only segment of the request handled by the dispatch class is what comes after the application name. In this case, that is "/orders".

요청 전달

Rather than call a method inside the dispatch class, the other option for your URL map is to forward all requests matching a particular prefix to a different dispatch class.

This is done using the <Map> element in the UrlMap section. The element contains two attributes, Prefix and Forward. If the request URL matches one of the prefixes, we send the request to the specified dispatch class for further processing.

For example:

XData UrlMap [ XMLNamespace = "http://www.intersystems.com/urlmap" ]
{
&lt;Routes>
  &lt;Map Prefix="/shipping" Forward="Demo.Service.Shipping"/>
  &lt;Route Url="/orders" Method="GET" Call="GetOrders"/>
  &lt;Route Url="/orders" Method="POST" Call="NewOrder"/>
&lt;/Routes>
}

A GET or POST request for "/orders" will be handled directly by this class. However, requests matching the prefix "/shipping" will be redirected to the dispatch class Demo.Service.Shipping, which has its own URL map:

XData UrlMap [ XMLNamespace = "http://www.intersystems.com/urlmap" ]
{
&lt;Routes>
  &lt;Route Url="/track/:id" Method="GET" Call="TrackShipment"/>
&lt;/Routes>
}

URL 라우팅 분석

To demonstrate how each component of the requested URL factors into what method is finally called, we will break down a request for the following address:

http://localhost:57772/csp/demo/shipping/track/123



http://
요청에 사용된 프로토콜입니다.


로컬 호스트:57772
우리가 연결하는 서버.

/csp/demo/shipping/track/123
요청 중인 리소스입니다.

/csp/데모
CSP 응용 프로그램 이름입니다. 응용 프로그램에 대해 디스패치 클래스가 정의되어 요청을 거기로 라우팅합니다.

/배송/트랙/123
첫 번째 디스패치 클래스로 전송된 리소스 세그먼트입니다.

/배송
URL 맵의 요소와 일치하는 접두사. Demo.Service.Shipping 클래스로 리디렉션합니다.

/트랙/123
두 번째 디스패치 클래스로 전송된 리소스 세그먼트. "/track/:id"경로와 일치합니다. TrackShipment(123) 메서드를 호출합니다.


동기 부여



  • 소스 제어 — REST API를 여러 클래스로 분리하면 각 클래스의 전체 크기가 줄어들어 소스 제어 기록을 간결하고 읽기 쉽게 유지하는 데 도움이 됩니다.

  • 버전 관리 — 여러 버전의 API를 동시에 지원하는 간단한 방법은 전달을 사용하는 것입니다. 단일 디스패치 클래스는/v1 또는/v2 접두사와 일치하는 요청을 해당 API 버전을 구현하는 디스패치 클래스로 전달할 수 있습니다. 새로운 IDE인 Atelier의 핵심인 REST API는 동일한 버전 관리 체계를 사용합니다.

  • 보안 — 예를 들어 관리자만 특정 유형의 요청을 수행할 수 있도록 API에 특정 사용자에게 제한된 경로가 필요한 경우 이러한 경로를 자체 클래스에서 분리한 다음 특정 접두사를 사용하여 요청을 전달하는 것이 좋습니다. 두 번째 디스패치 클래스가 OnPreDispatch 메서드를 정의하는 경우 해당 코드는 각 요청을 처리하기 전에 실행됩니다. 서비스는 이를 사용하여 사용자의 권한을 확인하고 처리를 계속할지 또는 요청을 취소할지 결정할 수 있습니다.
  • 좋은 웹페이지 즐겨찾기