2017-10-10 7 views
1

현재 UI5 tutorial을 따라 잡고 있으며 step 27 : Mock Server Configuration에 머물러 있습니다.로컬 데이터가있는 모의 서버 UI5 : "rootUri"가 작동하지 않습니다.

문제는 모의 서버의 rootUri 구성입니다. 튜토리얼에 따라 Northwind OData service을 사용하고 송장에 대해 dataSourcemanifest.json으로 구성했습니다.

이제 온라인 서비스 대신 로컬 모의 데이터를 사용하기 위해 튜토리얼에서 설명한대로 필요한 파일을 만들었습니다. mockServer.html을 실행하면 서버가 서비스 요청을 로컬 모의 데이터로 리디렉션하지 않고 웹 보안 문제로 인해 요청을 보내고 실패합니다. 나는 다음과 같은 rootUri를 사용하는 경우

, 모의 서버로 리디렉션하지 않고 서비스 요청이 실패 :

// Snippet from mockserver.js 
var oMockServer = new MockServer({ 
    rootUri: "/destinations/northwind/V2/Northwind/Northwind.svc/" 
}); 

Failed to load https://services.odata.org/V2/Northwind/Northwind.svc/ $metadata?sap-language=DE: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' https://webidetesting9515320-s0015250556trial.dispatcher.hanatrial.ondemand.com ' is therefore not allowed access. The response had HTTP status code 501.

Another question on Stackoverflow는 "와일드 카드"루트 URI하지만,이를 사용하여 모의 서버를 보여줍니다 또한 실패 : 나는 모의 서버 구성 작업을 할 수

// Snippet from mockserver.js 
var oMockServer = new MockServer({ 
    rootUri: "/" 
}); 

수있는 유일한 방법은 rootUri으로 동일한 URL을 사용하는 것입니다 내가 DataSource에의 URI 내가 조롱 원하는대로 manifest.json을 작성 S :

// Snippet from mockserver.js 
var oMockServer = new MockServer({ 
    rootUri: "https://services.odata.org/V2/Northwind/Northwind.svc/" 
}); 

위의 코드는 작동하지만, 웹 IDE이 나쁜 관행이라고 주장한다 :

error: Fiori Architectural Guidelines: ESLint(sap-no-hardcoded-url): Hardcoded (non relative) URL found. (img)

내 질문은 지금 : 모의 서버가 상대방 rootUri과 함께 의도 된 방식으로 실행되도록하려면 어떻게해야합니까?


웹 애플리케이션/manifest.json을 (발췌)

{ 
    "_version": "1.1.0", 
    "sap.app": { 
    "_version": "1.1.0", 
    "id": "sap.ui.tutorial.walkthrough", 
    "type": "application", 
    "i18n": "i18n/i18n.properties", 
    "title": "{{appTitle}}", 
    "description": "{{appDescription}}", 
    "applicationVersion": { 
     "version": "1.0.0" 
    }, 
    "dataSources": { 
     "invoiceRemote": { 
     "uri": "https://services.odata.org/V2/Northwind/Northwind.svc/", 
     "type": "OData", 
     "settings": { 
      "odataVersion": "2.0" 
     } 
     } 
    } 
    }, 
... 

웹 애플리케이션/테스트/mockServer.html

<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
     <meta charset="utf-8"> 
     <title>Hello World App - Test Page</title> 
     <script src="/resources/sap-ui-core.js" 
       id="sap-ui-bootstrap" 
       data-sap-ui-theme="sap_belize" 
       data-sap-ui-libs="sap.m" 
       data-sap-ui-compatVersion="edge" 
       data-sap-ui-preload="async" 
       data-sap-ui-resourceroots='{ 
        "sap.ui.tutorial.walkthrough": "../" 
       }'></script> 

     <script type="text/javascript"> 
      sap.ui.getCore().attachInit(function() { 
       sap.ui.require([ 
        "sap/ui/tutorial/walkthrough/localService/mockserver", 
        "sap/m/Shell", 
        "sap/ui/core/ComponentContainer" 
       ], function(mockserver, Shell, ComponentContainer) { 
        mockserver.init(); 

        new Shell({ 
         app: new ComponentContainer({ 
          name: "sap.ui.tutorial.walkthrough", 
          height: "100%" 
         }) 
        }).placeAt("content") 
       }); 
      }); 
     </script> 
    </head> 
    <body class="sapUiBody" id="content"> 
    </body> 
</html> 

웹 애플리케이션/LOCALSERVICE/mockserver.js

sap.ui.define([ 
    "sap/ui/core/util/MockServer" 
], function (MockServer) { 
    "use strict"; 

    return { 
     init: function() { 
      // create 
      var oMockServer = new MockServer({ 
       rootUri: "https://services.odata.org/V2/Northwind/Northwind.svc/" 
      }); 
      var oUriParameters = jQuery.sap.getUriParameters(); 
      // configure 
      MockServer.config({ 
       autoRespond: true, 
       autoRespondAfter: oUriParameters.get("serverDelay") || 1000 
      }); 
      // simulate 
      var sPath = jQuery.sap.getModulePath("sap.ui.tutorial.walkthrough.localService"); 
      oMockServer.simulate(sPath + "/metadata.xml", sPath + "/mockdata"); 
      // start 
      oMockServer.start(); 
     } 
    }; 
}); 

웹 애플리케이션/LOCALSERVICE/metadata.xml

<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx"> 
    <edmx:DataServices m:DataServiceVersion="1.0" m:MaxDataServiceVersion="3.0" 
         xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"> 
     <Schema Namespace="NorthwindModel" xmlns="http://schemas.microsoft.com/ado/2008/09/edm"> 
      <EntityType Name="Invoice"> 
       <Key> 
        <PropertyRef Name="ProductName"/> 
        <PropertyRef Name="Quantity"/> 
        <PropertyRef Name="ShipperName"/> 
       </Key> 
       <Property Name="ShipperName" Type="Edm.String" Nullable="false" MaxLength="40" FixedLength="false" 
          Unicode="true"/> 
       <Property Name="ProductName" Type="Edm.String" Nullable="false" MaxLength="40" FixedLength="false" 
          Unicode="true"/> 
       <Property Name="Quantity" Type="Edm.Int16" Nullable="false"/> 
       <Property Name="ExtendedPrice" Type="Edm.Decimal" Precision="19" Scale="4"/> 
      </EntityType> 
     </Schema> 
     <Schema Namespace="ODataWebV2.Northwind.Model" xmlns="http://schemas.microsoft.com/ado/2008/09/edm"> 
      <EntityContainer Name="NorthwindEntities" m:IsDefaultEntityContainer="true" p6:LazyLoadingEnabled="true" 
          xmlns:p6="http://schemas.microsoft.com/ado/2009/02/edm/annotation"> 
       <EntitySet Name="Invoices" EntityType="NorthwindModel.Invoice"/> 
      </EntityContainer> 
     </Schema> 
    </edmx:DataServices> 
</edmx:Edmx> 
+0

자습서의 26 단계를 확인하십시오. 원격 OData 서비스에 대한 액세스와 관련하여 "참고"및 "주의"절이 있습니다. –

+0

자습서의 "참고"및 "주의"상자는 브라우저에서 CORS 처리의 효과에 대해 설명합니다. 원격 데이터 대신 로컬을 사용한다는 목적으로 모의 서버를 만듭니다. CORS는 더 이상 문제가 아니며 "메모"및 "주의"상자는 관련이 없습니다. 내가 여기서 뭔가를 놓치고 있니? –

+0

"Walkthrough - Step 27"에서 다운로드 한 것과 정확히 일치하는 파일로 시도하십시오. 그런 다음 다운로드 한 것과 똑같이 모의 모드로 실행해야합니다. –

답변

1

이는 모의 서버의 rootUri 정의에 올 때 조심하는 몇 가지 규칙이 있습니다.

rootUri

  • 슬래시 ("/")
  • 이 모델에 할당 된 URI와 일치해야합니다로 종료하는 상대해야한다.

그것은 step 27에서 언급 :

(The rootUri) matches the URL of our data source in the descriptor file.

을뿐만 아니라 주제 Mock Server: Frequently Asked Questions에 :

The root URI has to be relative and requires a trailing '/'. It also needs to match the URI set in OData/JSON models or simple XHR calls in order for the mock server to intercept them.


그래서 그것은 문제가되지 않는 rootUri는 다음과 같이 정의하는 방법 위에 언급 된 세 가지 요구 사항을 충족하는 한 오래. 그렇기 때문에 rootUri: "/"과 같은 임의의 URI도 사용할 수 있지만과 uri의 dataSource가 동일한 경우에만 이어야합니다. 모의 서버를 실행해야 다음과 같이 rootUri 값을 변경하여 경우

:

var oMockServer = new MockServer({ 
    rootUri: "/destinations/northwind/V2/Northwind/Northwind.svc/" 
}); 

그리고 각각 앱 설명에를 ... : 장 27의

"dataSources": { 
    "invoiceRemote": { 
    "uri": "/destinations/northwind/V2/Northwind/Northwind.svc/", 
    "type": "OData", 
    "settings": { 
     "odataVersion": "2.0" 
    } 
    } 
} 
+0

죄송합니다, 지금까지 답변을 확인할 시간을 찾지 못했습니다. 기본적으로 이것은 모의 서버가 의도 한대로 작동하도록하고 여러분의 대답을 받아 들일 것으로 표시합니다. ** 그러나 비 모크 서버 버전을 이제 실행할 수 없습니다. '리소스를로드하지 못했습니다 : 서버가 404 상태로 응답했습니다 (찾을 수 없음) "/destinations/northwind/V2/Northwind/Northwind.svc/" 나는이 서비스가 서버에 등록되어 있습니다. 그러나 서버 외부의 실제 및 기존 서비스로 실제 상황에서 모의 ​​서버 작업을 보는 데 매우 관심이 있습니다. –

+0

@DanielKneip 안녕하세요, 문제 없습니다. 제 대답을 수락 해 주셔서 감사합니다. 404의 이유는 경로를 알 수 없기 때문입니다. 경로가 모의 서버가 아닌 시나리오에서 작동하게하려면 SAP Cloud Platform (https://ui5.sap.com/#/topic/3a16c7a2f1e944deb000db49e5ece6be)에 해당 [대상]을 등록하고 [neo-app.json ] (https://help.sap.com/viewer/65de2977205c403bbc107264b8eccf4b/Cloud/en-US/aed1ffa3f3e741b3a4573c9e475aa2a4.html) 프로젝트에 따라 ** 또는 ** 경로를 https : //cors-anywhere.herokuapp로 바꿉니다. .com/services.odata.org/V2/Northwind/Northwind.svc /'모든 곳. – boghyon

+0

@ DanielKneip 첫 번째 접근법에 대해 [위에서 언급 한] (https://stackoverflow.com/questions/46664672/ui5-mock-server-with-local-data-rooturi-not-working/47181847#comment82587427_47181847); 이 항목에서는 neo-app.json을 편집하는 방법에 대해서도 설명합니다. https://ui5.sap.com/#/topic/672301f4f47640a8b2bc817d2ce0f512.html – boghyon

0

일부 세부 사항 튜토리얼의 내용은 오해의 소지가 있습니다.

  1. MockServer rootUri

    의 manifest.json에서 데이터 소스의 uri 매개 변수와 일치해야합니다.

  2. 데이터 소스의 URI를 자습서에 제공된 MockServer의 (잘못된) rootUri으로 변경하는 대신 실제로 MockServer의 rootUri을 외부 소스의 URI로 변경해야합니다.

    var oMockServer = new MockServer({ 
         rootUri: "https://services.odata.org/V2/Northwind/Northwind.svc/" 
        }); 
    

이 그 외부 URI에 대한 모든 호출을 차단하고 로컬 그들에게 응답하는 MockServer을 생성합니다 : webapp/localService/mockserver.js에서이 수정 된 블록을 사용합니다. 그리고이 구조를 사용하면 실질적으로 /webapp/test/mockServer.html을 통해 MockServer에 액세스하고 라이브 데이터 서버를 /webapp/index.html 통해 액세스 할 수 있습니다.

힌트 : 당신은 가장 가능성 때문에 여전히 동일 출처 정책 제한 (SOP)에 문제가 /webapp/index.html를 사용하여 원래 (외부) 데이터 소스에 액세스하는 것
. Chrome을 사용하면 SOP없이 브라우저의 두 번째 인스턴스 (!)를 실행하면 멋지게 해결할 수 있습니다. 이는 표준 인스턴스 WITH SOP의 다른 열려있는 브라우저 창과 동시에 수행 할 수 있으므로 열려있는 브라우저 창을 모두 닫을 필요가 없습니다. 자세한 내용은 this answer을 참조하십시오.

+0

자습서 단계에서 실수가 없습니다. 이 연습에서는 프로젝트가 [대상 정의] 할 수있는 웹 IDE/SCP를 기반으로한다고 가정합니다 (https://stackoverflow.com/questions/46664672/ui5-mock-server-with-local-data-rooturi-not -working/47181847 # comment82587427_47181847). 그리고 질문 작성자는 Web IDE를 사용한다고 언급합니다. 그래서이 경우,'/ destinations/northwind/V2/Northwind/Northwind.svc /'URI는 모의 서버를 시작하지 않아도 완전히 유효하며 작동합니다. SOP를 우회 할 필요가 없습니다. – boghyon

+0

@ 보현 : 내가 틀렸다면 나를 바로 잡아주세요. 나는 Walkthrough를 따라 갔고 같은 장에 갇혔다. 대상 등록은 자습서의 어느 곳에서도 설명되지 않습니다. 27 장에서는 주어진 변경 사항으로 모의 서버와 생명선을 두 개의 서로 다른 항목 URL을 사용하여 번갈아 사용할 수 있다고 주장합니다. 목적지는 어디에도 정의/등록되어 있지 않습니다. – Jpsy

+0

연습 자체가 대상 등록에 관해 많이 언급하지 않는 것이 맞지만 [동일한 단계] (https://ui5.sap.com/#/topic/bae9d90d2e9c4206889368f04edab508)부터 언급 한 내용이 잘못되었다는 것은 잘못되었습니다. "URI [...]는 SAP Web IDE 용으로 구성된 대상을 가리 킵니다 (이전 단계 참조).이 대상을 사용할 수 있다고 가정합니다." 이전 단계에는 [this] (https://ui5.sap.com/#/topic/3a16c7a2f1e944deb000db49e5ece6be)를 가리키는 [this] (https://ui5.sap.com/#/topic/5bb388fc289d44dca886c8fa25da466e)에 대한 링크가 포함되어 있습니다.) additaionally. 모든 것이 있습니다 – boghyon