Skip to content

Commit 2e711b1

Browse files
authored
Add request envelope to readme (#6)
* add requestEnvelope example * add highlighting * update instructions * update instructions
1 parent 06e90ed commit 2e711b1

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

README.md

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Generic framework that can be used to implement REST Controllers. Contributions
33

44
## Overview
55

6-
REST has become the standard for modern web APIs. This framework provides a standardized approach to building REST Endpoints and provides access to important status codes and a standardized response envelope that follows a popular schema.
6+
REST has become the standard for modern web APIs. This framework provides a standardized approach to building REST Endpoints and provides access to important status codes and a standardized response envelope that follows a popular schema. The framework assumes you'll be using JSON for the requests and responses since it is so simple to work with.
77

88
**Deploy to Salesforce Org:**
99
[![Deploy](https://raw.githubusercontent.com/afawcett/githubsfdeploy/master/deploy.png)](https://githubsfdeploy.herokuapp.com/?owner=Maxscores&repo=sfdc-rest-controller-framework&ref=master)
@@ -30,12 +30,12 @@ Additionally, you'll need to implement overrides and call them on the standard r
3030
@RestResource(UrlMapping='/accounts')
3131
global class AccountsRESTController extends RESTController {
3232
private static AccountsRESTController controller = new AccountsRESTController();
33-
33+
3434
@HttpGet
3535
global static void getAccounts() {
3636
RestContext.response = controller.getRecords();
3737
}
38-
38+
3939
public override RestResponse getRecords() {
4040
// will need to implement the override method to return the response, something like:
4141
Account accountToReturn = [Select Id From Account];
@@ -52,18 +52,18 @@ public with sharing class AccountsRESTControllerTest {
5252
String jsonResponse = response.responseBody.toString();
5353
return (AccountResponseEnvelope) JSON.deserialize(jsonResponse, AccountResponseEnvelope.class);
5454
}
55-
55+
5656
public class AccountResponseEnvelope {
5757
public List<String> messages;
5858
public List<String> errors;
5959
public Account data;
6060
}
61-
61+
6262
public static AccountsResponseEnvelope getAccountsResponseEnvelope(RestResponse respose) {
6363
String jsonResponse = response.responseBody.toString();
6464
return (AccountsResponseEnvelope) JSON.deserialize(jsonResponse, AccountsResponseEnvelope.class);
6565
}
66-
66+
6767
public class AccountsResponseEnvelope {
6868
public List<String> messages;
6969
public List<String> errors;
@@ -80,7 +80,7 @@ public with sharing class AccountsRESTControllerTest {
8080
public static void testGet() {
8181
Account account = new Account();
8282
insert account;
83-
83+
8484
RestRequest req = new RestRequest();
8585
req.requestURI = '/services/apexrest/accounts/' + account.Id;
8686
req.httpMethod = 'GET';
@@ -100,3 +100,23 @@ public with sharing class AccountsRESTControllerTest {
100100
}
101101
```
102102
103+
The framework also provides a suggestion for formatting incoming data. For this, you'll need to implement two pieces.
104+
1) a request data prototype (RequestLead)
105+
2) a request envelope (RequestEnvelope) that uses the request data prototype and has a constructor
106+
107+
This will allow you to deserialize the data from JSON. See the example from the TestRESTController:
108+
```java
109+
public class RequestEnvelope {
110+
public RequestLead data;
111+
112+
public RequestEnvelope(String jsonRequest) {
113+
RequestEnvelope request = (RequestEnvelope) JSON.deserialize(jsonRequest, RequestEnvelope.class);
114+
data = request.data;
115+
}
116+
}
117+
118+
public class RequestLead {
119+
public String firstName;
120+
public String lastName;
121+
}
122+
```

0 commit comments

Comments
 (0)