Apex REST API のクラスとメソッドの方法

環境
Salesforce

構文
@RestResource(urlMapping=’URL名’)
global with sharing class クラス名 {処理コード

REST APIのクラスとメソッドの書き方

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@RestResource(urlMapping='/test/url')
global with sharing class ClassName {
@HttpGet
global static void doGet()(
}
}
@RestResource(urlMapping='/test/url') global with sharing class ClassName { @HttpGet global static void doGet()( } }
@RestResource(urlMapping='/test/url')
global with sharing class ClassName {
    @HttpGet
    global static void doGet()(
    }
}

説明
アクセス修飾子は globalです。
@RestResource(urlMapping=’/url’)をつけることでREST APIのクラスとすることができます。
urlMapping=’/**’ の部分でURLを指定する

@HttpGet、@HttpPost
GETやPOSTに対応したメソッドを作成することができます。

REST APIを呼び出す方法
HTTPメソッド:GET

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
URL: https://組織ドメイン.salesforce.com/services/apexrest/test/url
URL: https://組織ドメイン.salesforce.com/services/apexrest/test/url
URL: https://組織ドメイン.salesforce.com/services/apexrest/test/url

名前空間がある場合、REST APIを呼び出す

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
https://組織ドメイン.salesforce.com/services/apexrest/名前空間/test/url
https://組織ドメイン.salesforce.com/services/apexrest/名前空間/test/url
https://組織ドメイン.salesforce.com/services/apexrest/名前空間/test/url

 

Apex

Posted by arkgame