2016-07-30 43 views
1

我無法弄清楚如何在我的REST控件元素中設置響應代碼。在休息控制中設置響應代碼

enter image description here

這裏是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> 

需求是在某些情況下返回代碼404,我不知道如何去做。

有誰知道如何使用SSJS來做到這一點?

的Domino版本9.0.1是

回答

4

不能返回一個狀態404的doGet和doPost。響應屬性狀態由customRestService管理。 SSJS代碼只能返回JSON數據。
你可能會雖然定義自己的JSON內容,如

{ 
    "status": "error", 
    "error-message": "something not found" 
} 

和處理錯誤這樣。

作爲替代方案,您可以使用customRestService的serviceBean

 <xe:customRestService 
      contentType="application/json" 
      requestContentType="application/json" 
      serviceBean="de.leonso.demo.RestService"> 
     </xe:customRestService> 

,並設置返回代碼與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); 
     } 
    } 
+0

感謝@Knut。 我會接受它作爲答案,因爲它是我所懷疑的「一對一」。 –