setoya-blog

システム開発技術、データ分析関連でお勉強したことや、山奥生活を綴る、テンション低めなブログです。

Apexで独自のRestAPIを公開する

こんな感じでクラスの@RestResourceアノテーションでルーティングをして、メソッドに@HttpGetとか@HttpPostとかアノテーションをつければ、http methodに応じて呼ばれるメソッドが変わる。メソッド名は何でもいいみたい。

しかし、これだと1クラスで1つのURLしか定義できず、同じく1クラスでget、post、put、delete用の4つのAPIしか公開できない。何かいい方法ないんだろうか。ルーティングはメソッドごとにできればいいのに。

あとAPIとして公開するためには、クラスがglobalでないとダメ。

@RestResource(urlMapping='/Hogehoge/v1/*')
global with sharing class RestApiHogehoge {

    @HttpPut
    global static updateHogehoge() {

        RestApiHogehogeResponse response = new RestApiHogehogeResponse();
        respnse.string1 = "test1";
        respnse.string2 = "test2";
        return response;
    }

    global class RestApiHogehogeResponce {
        global String string1;
        global String string2;
    }
}

参考
Apex公式ガイド(日本語)