Dmy*_*kyi 1 xpages xpages-ssjs
我不知道如何在 REST 控制元素中设置响应代码。
这是REST控制的代码。
<xe:restService id="restProfile" pathInfo="profile">
<xe:this.service>
<xe:customRestService
doGet="#{javascript:REST_PROFILE.doGet()}"
contentType="application/json"
doPost="#{javascript:REST_PROFILE.doPost(reqVar)}"
requestContentType="application/json" requestVar="reqVar">
</xe:customRestService>
</xe:this.service>
</xe:restService>
Run Code Online (Sandbox Code Playgroud)
要求在某些情况下返回代码 404,但我不知道该怎么做。
有人知道如何使用 SSJS 做到这一点吗?
Domino的版本是9.0.1
您无法使用 doGet 和 doPost 返回状态 404。响应属性状态由 customRestService 管理。SSJS代码只能返回JSON数据。
您可以定义自己的 JSON 内容,例如
{
"status": "error",
"error-message": "something not found"
}
Run Code Online (Sandbox Code Playgroud)
但是并以这种方式处理错误。
作为替代方案,您可以使用 customRestService 的serviceBean。
<xe:customRestService
contentType="application/json"
requestContentType="application/json"
serviceBean="de.leonso.demo.RestService">
</xe:customRestService>
Run Code Online (Sandbox Code Playgroud)
并response.setStatus(status)在那里设置返回代码:
public class RestService extends CustomServiceBean {
@Override
public void renderService(CustomService service, RestServiceEngine engine) throws ServiceException {
try {
HttpServletRequest request = engine.getHttpRequest();
HttpServletResponse response = engine.getHttpResponse();
response.setHeader("Content-Type", "application/json; charset=UTF-8");
response.setContentType("application/json");
response.setHeader("Cache-Control", "no-cache");
response.setCharacterEncoding("utf-8");
String method = request.getMethod();
int status = 200;
if (method.equals("GET")) {
status = ...
} else {
...
}
response.setStatus(status);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
Run Code Online (Sandbox Code Playgroud)