LoginSignup
2
4

More than 3 years have passed since last update.

Apex REST 勉強メモ:RESTful Web サービス

Last updated at Posted at 2019-07-16

1.Apex REST アノテーション

6 つの新しいアノテーションが追加され、Apex クラスを RESTful Web サービスとして公開できるようにするようになりました。

image.png

【考慮事項】

  • URL 対応付けは、https://instance.salesforce.com/services/apexrest/ と相対的です。
  • ワイルドカード文字 (*) を使用することができます。
  • URL の対応付けでは、大文字と小文字は区別されます。
  • このアノテーションを使用するには、Apex クラスがグローバルとして定義されている必要があります。
  • HTTP メソッド は global static として定義する。
  • それぞれのアノテーションは、各 Apex クラスで 1 回のみ使用できます。

2.実装例

@RestResource(urlMapping='/XXXXXX/*')
global with sharing class XXXXXXRestAPI {
    @HttpGet
    global static ResponseWrapper doGet() {
        ResponseWrapper response = new ResponseWrapper();
        try {
            // 業務処理
            // ~~省略~~

            // 正常終了
            response.isSuccess = true;
        } catch(Exception e){
            // 異常終了
            response.isSuccess = false;
            response.message = 'システムエラーが発生しました。' + e.getMessage();
        }

        return response;
    }

    @HttpPost
    global static ResponseWrapper doPost(RequestWrapper request) {
        ResponseWrapper response = new ResponseWrapper();
        try {
            // 業務処理
            // ~~省略~~

            // 正常終了
            response.isSuccess = true;
        } catch(Exception e){
            // 異常終了
            response.isSuccess = false;
            response.message = 'システムエラーが発生しました。' + e.getMessage();
        }

        return response;
    }

    global class RequestWrapper {
        Account acct;
    }

    global class ResponseWrapper {
        Boolean isSuccess;
        String message;

        Account acct;
        List<Contact> contList;
    }
}

3.Apex Rest API バージョン管理場合の検討

URI Naming Rules(参考)

サンプルソース

~~準備中~~

4.Resource

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4