0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Salesforce Apex】RestContext RestRequest RestResponseって結局何

Posted at

はじめに

restApiでJsonデータを受け取る機能を実装した際、
お作法的に何も考えず書いていましたが、
ちゃんと理解したくて調べたのでまとめます。

↓ この部分

お作法コード
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;

RestContext クラスについて

イメージと、IT的なちゃんとした表現両方書きます。

RestContext

向こうからのリクエストもこちらからの返答も入っている、「通信内容全体をまとめているクラス」

RestContext クラス
RestRequest オブジェクトと RestResponse オブジェクトを含みます。

RestContext.request

向こう側からのリクエストが入っている
→ クライアント(外部)から送られてきたリクエスト情報を保持

RestContext.response

こちらからの返答が入っている
→ サーバー(こちら側/Salesforce) から返すレスポンス情報を設定

RestContextからの取り出し方

RestContext.requestはRestRequest型、
RestContext.responseはRestResponse型であるため、

RestRequestとRestResponseに値を設定するお作法コード
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;

このようにしてリクエストとレスポンスを取り出し、中身を変数に入れている。

Accountクラスで例えると

Accountでの例え
    String str = Account.Name;
    Integer num = Account.NumberOfEmployees;

〇Accountの中にNameやNumberOfEmployeesの情報が入っていて、
★それぞれString型とInteger型であるため、
◇それぞれに適した型の変数を用意し、中身を格納している。

RestContextでもやっていることは同じで、
〇RestContextのなかにrequestとresponseの情報が入っていて、
★それぞれRestRequest型とRestResponse型であるため、
◇それぞれに適した型の変数を用意し、中身を格納している。

おわりに

お作法コードの意味を理解できてよかったです

参考

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?