当前位置:网站首页>Chapter 14 Manually create a REST service (2)
Chapter 14 Manually create a REST service (2)
2022-08-02 12:01:00 【yaoxin521123】
文章目录
第十四章 手动创建 REST 服务(二)
指定数据格式
可以定义 REST Service to handle the data of different format,例如 JSON、XML、文本或 CSV. REST Calls can be through the in HTTP 请求中指定 ContentType Element to specify that it expected to send data in the form of,并且可以通过在 HTTP 请求中指定 Accept Elements to request returns data format.
在 DocServer 示例中,GetNamespaces() 方法检查 REST Call whether or not to use the following request JSON 数据:
If $Get(%request.CgiEnvs("HTTP_ACCEPT"))="application/json"
本地化 REST 服务
REST Service returns can be any string value localization,In order to store multiple servers in different language versions of the string.然后,When the service receives the contain HTTPAccept-Language 标头的 HTTP 请求时,The service will use the corresponding version of the string to respond.
本地化 REST 服务:
- 在实现代码中,Don't include hard coded text string,而是使用
$$$Text宏的实例,为宏参数提供如下值:
- 默认字符串
- (可选)The string belongs to the domain of(The string when grouped into field,本地化更易于管理)
- (可选)默认字符串的语言代码
例如,而不是这个:
set returnvalue="Hello world"
set returnvalue=$$$TEXT("Hello world","sampledomain","en-us")
- 如果省略
$$$TextThe domain parameters of macro,则还要在RESTService class containsDOMAIN类参数.例如:
Parameter DOMAIN = "sampledomain"
- 编译代码.当这样做时,编译器会在消息字典中为
$$$Text宏的每个唯一实例生成条目.
消息字典是全局的,因此可以在管理门户中轻松查看(例如).有一些类方法可以帮助完成常见任务.
- 开发完成后,Export of all of the domain in the domain or message dictionary.
结果是一个或多个 XML 消息文件,其中包含原始语言的文本字符串.
Send the documents to the translator,请求翻译版本.
When I received the translated
XMLThe message file,Import them to the export of the original file space of the same name.
译文和原文在消息词典中并存.
- 在运行时,
REST服务根据HTTP Accept-LanguageHeaders to choose to return to the text.
通过REST 使用 Web 会话
有关介绍,Please refer to the book in front of“将 Web 会话与 REST 结合使用”.
要使 REST Services can be more REST 调用使用单个 Web 会话,请在 REST Service class will be UseSession 参数设置为 1:
Parameter UseSession As Integer = 1;
支持 CORS
有关介绍,Please refer to the book in front of“CORS 简介”.请注意,When, in accordance with the description in this appendix manually create Web 服务时,支持 CORS Detailed information will be slightly different.
修改 REST 服务以使用 CORS
要指定 REST 服务支持 CORS,请按如下方式修改 REST 服务类,然后重新编译它.
- 为
HandleCorsRequest参数指定一个值.
要为所有调用启用 CORS 标头处理,请将 HandleCorsRequest 参数指定为 1:
Parameter HandleCorsRequest = 1;
或者,要为某些调用启用 CORS 标头处理,但不是调用,请将 HandleCorsRequest 参数指定为“”(空字符串):
Parameter HandleCorsRequest = "";
(如果 HandleCorsRequest 为 0,则对所有调用禁用 CORS 标头处理.在这种情况下,如果 REST 服务接收到带有 CORS 标头的请求,The service will reject the request.这是默认设置.)
- 如果将
HandleCorsRequest参数指定为“”,请编辑URLMap XData块以指示哪些调用支持CORS.具体来说,Should any supportCORS的<Route>,添加以下属性名称和值:
Cors="true"
或者在 <Route> 元素中指定 Cors="false" 以禁用 CORS 处理.
如果 REST Service class will REST 请求转发到另一个 REST 服务类,则 CORS Processing behavior by containing matching with a given request <Route> Element class determine.
覆盖 CORS 标头处理
重要提示:默认 CORS 标头处理不适用于处理机密数据的 REST 服务.
默认的 CORS Do any filtering processing don't,只是将 CORS Head passed to an external server and returns a response.You may want to limit access to the source domain to allow list or limit allowed to request method.可以通过覆盖 REST 服务类中的 OnHandleCorsRequest() 方法来做到这一点.
请注意,与 UrlMap 中的 <Route> All elements matching URL Requests are using class defined in a single OnHandleCorsRequest() 方法进行处理.If you need for different REST URL 请求使用不同的 OnHandleCorsRequest() 方法实现,则应使用 Forward Sends a request to the other REST 服务类.
变量:Access query parameters
将参数传递给 REST Service is the recommended way to them as used to invoke services URL Part of the path to pass(例如,/myapi/someresource/parametervalue).但是,在某些情况下,Transfer parameters as query parameters may be more convenient(例如,/myapi/someresource?parameter=value).在这种情况下,可以使用 %request Variable to retrieve parameter values.在 REST 服务中,%request 变量是 %CSP.Request 的一个实例,它包含整个 URL 查询.To retrieve a given query the value of the parameter,请使用以下语法:
$GET(%request.Data(name,1),default)
其中 name Is the name of the query parameters,default Is to return to the default value of.或者,如果同一个 URL Multiple copies of the same query parameters,请使用以下语法:
$GET(%request.Data(name,index),default)
其中 index Is to retrieve a copy of the index of digital.
边栏推荐
猜你喜欢
随机推荐
Likou 209 - String with the Minimum Length - Sliding Window Method
QAbstractScrollArea、QScrollArea
今日睡眠质量记录85分
Learning Experience Sharing Seven: YOLOv5 Code Chinese Comments
NVIDIA NeMo Metrics 轻量性能采集系统
服务器间传输文件
Coroutines and Lifecycle in Kotlin
Jest 测试框架 beforeEach 的设计原理解析
看我如何用多线程,帮助运营小姐姐解决数据校对系统变慢!
WPF 实现窗体抖动效果
pyqt5连接MYSQL数据库问题
细学常用类,集合类,IO流
云原生(三十) | Kubernetes篇之应用商店-Helm介绍
CAN总线的AUTOSAR网络管理
基于threejs的商品VR展示平台的设计与实现思路
How to connect TDengine through DBeaver?
go语言的接口
19、商品微服务-srv层实现
翻译英语的软件-免费翻译软件-各种语言互相翻译
借小程序容器打造自有App小程序生态









