定义 OData 服务

ABAP RESTful 应用程序编程模型 › 开发 › 开发应用程序 › 通过访问远程服务开发 UI 服务 › 定义 OData 服务

为了能够通过 SAP Fiori UI 使用服务,您需要创建 OData 服务。

  1. 按照创建 OData 服务中所述的开发步骤进行操作。

    创建服务定义和服务绑定以公开 CDS 自定义实体,包括其服务查询实施。您只需在服务定义中包含自定义实体,其实施将隐式包括在内。

    然后,您可以使用服务绑定中的 Fiori 元素预览测试生成的 UI 服务。

  2. 按照为 Fiori 元素应用设计用户界面中描述的步骤进行操作

    使用 @UI 注释定义 SAP Fiori 应用的 UI。UI 注释在 CDS 自定义实体中维护的方式与 CDS 视图中的维护方式完全相同。

    对于此消耗场景,可以轻松标记行项目和标识注释的 元素,因为不会从远程服务检索数据元素信息。

    备注

    对于选择字段,无法在 @UI 注释中维护标签。对于具有选择字段的元素,请使用 @EndUser.label 注释。

    展开以下列表以查看差旅 CDS 视图的源代码。

    @ObjectModel.query.implementedBy:'ABAP:/DMO/CL_R_TRAVELTP_SCM_QUERY'
    @EndUserText.label: 'CE for Service Consumption Scenario'
    
    define root custom entity /DMO/R_TRAVEL_SCM_TP
    {
    
          @UI.facet              : [
              { id               : 'Travel',
              purpose            : #STANDARD,
              type               : #IDENTIFICATION_REFERENCE,
              label              : 'Travel',
              position           : 10 } ]
    
          @UI                    : {lineItem: [ { position: 10, label: 'Travel ID', importance: #HIGH } ],
                                    selectionField: [ { position: 10 }],
                                    identification:  [{ position: 10 , label: 'Travel ID' }] }
          @EndUserText.label     : 'Travel ID'
      key travel_id              : abap.numc( 8 );
    
          @UI                    : {lineItem: [ { position: 20, label: 'Agency ID',  importance: #HIGH } ],
                                    selectionField: [ { position: 20 } ],
                                    identification:  [{ position: 20, label: 'Agency ID' }] }
          @EndUserText.label     : 'Agency ID'
          agency_id              : abap.numc( 6 );
    
          @UI                    : {lineItem: [ { position: 30, label: 'Customer ID', importance: #HIGH } ],
                                    selectionField: [ { position: 30 } ],
                                    identification:[ { position: 30 , label: 'Customer ID'} ] }
          @EndUserText.label     : 'Customer ID'
          customer_id            : abap.numc( 6 );
    
          @UI                    : {lineItem: [ { position: 40, label: 'Starting Date',  importance: #MEDIUM } ],
                                    identification:[ { position: 40, label: 'Starting Date' } ] }
          begin_date             : abap.dats;
    
          @UI                    : {lineItem: [ { position: 45,   label: 'End Date', importance: #MEDIUM } ],
                                    identification:[ { position: 45,   label: 'End Date' } ] }
          end_date               : abap.dats;
    
          @UI                    : {identification:[ { position: 60, label: 'Booking Fee' } ] }
          @Semantics.amount.currencyCode: 'currency_code'
          booking_fee            : abap.dec( 17, 3 );
    
          @UI                    : {lineItem: [ { position: 70, label: 'Total Price',  importance: #MEDIUM } ],
                                    identification:[ { position: 70, label: 'Total Price' } ] }
          @Semantics.amount.currencyCode: 'currency_code'
          total_price            : abap.dec( 17, 3 );
          currency_code          : abap.cuky( 5 );
    
          @UI                    : {lineItem: [ { position: 50, label: 'Comment', importance: #MEDIUM } ],
                                    identification:[ { position: 50, label: 'Comment' } ] }
          description            : abap.char( 1024 ); //renamed element
    
          @UI                    : {lineItem: [ { position: 90, label: 'Status', importance: #HIGH } ],
                                    identification:[ { position: 90, label: 'Status' } ] }
          status                 : abap.char( 1 );
    
          @EndUserText.label     : 'Last changed on'
          last_changed_at        : timestampl;
    
          @UI                    : {lineItem: [ { position: 80, label: 'Discount %', importance: #MEDIUM } ],
                                    identification:[ { position: 80, label: 'Discount %' } ] }
          discountpct            : abap.dec( 3, 1 );
          @Semantics.amount.currencyCode: 'currency_code'
          @UI                    : {lineItem: [ { position: 72, label: 'Total Price with Discount',   importance: #MEDIUM } ],
                          identification:[ { position: 72, label: 'Total Price with Discount' } ] }
          totalpricewithdiscount : abap.dec(17,3);
          @UI.hidden: true
          calculatedetag         : abap.sstring( 1024 );
    }
    
  3. 为属性添加静态功能控件,以将其设置为只读或必需。

    唯一可修改的属性是 DISCOUNTPCT。需要使用值填充,因此将其设置为必填。将所有其他属性设置为只读。有关详细信息,请参阅功能控制。

  4. 定义 RAP 副作用,当字段 totalpricewithdiscount 发生更改时,UI 会触发字段 discountpct 的重新加载。

    展开以下列表,通过添加的相关功能控制查看行为定义的源代码。

    unmanaged implementation in class /dmo/bp_r_travel_scm_tp unique;
    strict ( 2 );
    
    define behavior for /DMO/R_TRAVEL_SCM_TP alias Travel_SCM
    
    lock master
    authorization master ( instance )
    etag master calculatedetag
    {
      update;
    
      field ( readonly ) TRAVEL_ID, AGENCY_ID, CUSTOMER_ID,
      BEGIN_DATE, END_DATE, DESCRIPTION,
      BOOKING_FEE, TOTAL_PRICE, TOTALPRICEWITHDISCOUNT,
      STATUS, CURRENCY_CODE, calculatedetag;
    
      field ( mandatory ) DISCOUNTPCT;
    
      side effects { field discountpct affects field totalpricewithdiscount; }
    
    }
    
  5. 定义的 RAP 副作用将字段 totalpricewithdiscount 添加到查询实施类中请求的元素列表中。但是,如果没有字段 total_price,则无法计算字段 totalpricewithdiscount 的值。因此,如果在删除本地和瞬态字段之前将字段 total_price 添加到请求元素列表中,如果请求字段 totalpricewithdiscount 且字段 total_price 不是列表的一部分。

    ...
    " Request Elements
    DATA(lt_req_elements) = io_request->get_requested_elements( ).
    
    IF line_exists( lt_req_elements[ table_line = 'TOTALPRICEWITHDISCOUNT' ] ) AND NOT line_exists( lt_req_elements[ table_line = 'TOTAL_PRICE' ] ).
        APPEND 'TOTAL_PRICE' TO lt_req_elements.
    ENDIF.
    
    "delete local fields out of the fields to be selected via OData Client Proxy
    DELETE  lt_req_elements  WHERE table_line = 'DISCOUNTPCT' OR
            table_line = 'TOTALPRICEWITHDISCOUNT' OR
            table_line = 'CALCULATEDETAG'.
    ...