@@ -43,7 +43,7 @@ Create a new Salesforce web service class as the entry point:
4343
4444``` java
4545@RestResource (UrlMapping = ' /*/customer/*' )
46- global with sharing class CustomerWebService {
46+ global with sharing class CustomerWebServiceDemo {
4747 @HttpGet
4848 global static void doGet () {
4949
@@ -90,7 +90,7 @@ Let's create it:
9090public class CustomerRestRouter extends libak_RestRouter {
9191 override public libak_RestRouter setRoutes () {
9292 this . routeToRestProcessorType = new Map<String, Type>{
93- ' /v1/customers' = > CustomersListProcessorV1 . class,
93+ ' /v1/customers' = > CustomersProcessorV1 . class,
9494 ' /v1/customers/:customer_sf_id' = > CustomerProcessorV1 . class
9595 };
9696 return this ;
@@ -114,13 +114,13 @@ public class CustomerRestRouter extends libak_RestRouter {
114114}
115115```
116116
117- > ** _ NOTE:_ ** We recommend to put Rest Router as an inner class in our ` CustomerWebService ` class to have clear view of our API when the developer opens the web service class.
117+ > ** _ NOTE:_ ** We recommend to put Rest Router as an inner class in our ` CustomerWebServiceDemo ` class to have clear view of our API when the developer opens the web service class.
118118
119- At this point the ` CustomerWebService ` class looks like this:
119+ At this point the ` CustomerWebServiceDemo ` class looks like this:
120120
121121``` java
122122@RestResource (UrlMapping = ' /*/customer/*' )
123- global with sharing class CustomerWebService {
123+ global with sharing class CustomerWebServiceDemo {
124124 @HttpGet
125125 global static void doGet () {
126126
@@ -139,12 +139,13 @@ global with sharing class CustomerWebService {
139139 }
140140
141141 public class CustomerRestRouter extends libak_RestRouter {
142- override public libak_RestRouter setRoutes () {
143- this . routeToRestProcessorType = new Map<String, Type>{
144- ' /v1/customers' = > CustomersListProcessorV1 . class,
145- ' /v1/customers/:customer_sf_id' = > CustomerProcessorV1 . class
146- };
147- return this ;
142+ override public libak_RestRouter setRoutes () {
143+ this . routeToRestProcessorType = new Map<String, Type>{
144+ ' /v1/customers' = > CustomersProcessorV1 . class,
145+ ' /v1/customers/:customer_sf_id' = > CustomerProcessorV1 . class
146+ };
147+ return this ;
148+ }
148149 }
149150}
150151```
@@ -156,7 +157,7 @@ Well, We've already done a good job to implement a base version of building our
156157Now it's time to implement our REST Processors.
157158
158159As you remember we agreed to have two of them:
159- - `CustomersListProcessorV1 ` for `/ v1/ customers`
160+ - ` CustomersProcessorV1 ` for ` /v1/customers `
160161- ` CustomerProcessorV1 ` for ` /v1/customers/:customer_sf_id `
161162
162163Here is a few things you have to consider about it:
@@ -178,18 +179,18 @@ Now we know good enough at this stage to prepare our Rest Processors
178179
179180``` java
180181// Here you can see there is no pagination implementation. We will add it later to show the versioning functionality.
181- public class CustomersListProcessorV1 extends libak_RestProcessor {
182+ public class CustomersProcessorV1 extends libak_RestProcessor {
182183 protected override libak_RestFramework. IRestResponse handleGet () {
183184 List<Account > accounts = [
184185 SELECT Id , Name , Phone , BillingStreet , BillingCity , BillingState , BillingPostalCode
185186 FROM Account
186187 LIMIT 100
187188 ];
188189
189- if (! accounts. isEmpty()) {
190- return new libak_JsonResponse(accounts );
190+ if (accounts. isEmpty()) {
191+ return new libak_ErrorResponse( 404 , ' Accounts are not found ' , ' ' );
191192 } else {
192- throw new libak_RestFramework . NotFoundException ( ' Accounts are not found ' );
193+ return new libak_JsonResponse(accounts );
193194 }
194195 }
195196
@@ -212,17 +213,18 @@ public class CustomerProcessorV1 extends libak_RestProcessor {
212213 ];
213214
214215 if (accounts. isEmpty()) {
215- throw new libak_RestFramework. NotFoundException (' Account not found' );
216+ return new libak_ErrorResponse(404 , ' Accounts are not found' , ' ' );
217+ } else {
218+ return new libak_JsonResponse(accounts. get(0 ));
216219 }
217- return new libak_JsonResponse(accounts. get(0 ));
218220 }
219221
220222 protected override libak_RestFramework. IRestResponse handlePut () {
221223 String accountId = this . getUriParam(' customer_sf_id' );
222224 List<Account > existingAccounts = [SELECT Id FROM Account WHERE Id = : accountId];
223225
224226 if (existingAccounts. isEmpty()) {
225- throw new libak_RestFramework . NotFoundException ( ' Account not found' );
227+ return new libak_ErrorResponse( 404 , ' Account are not found' , ' ' );
226228 }
227229
228230 Account updatedAccount = (Account )JSON . deserialize(this . request. requestBody. toString(), Account . class);
@@ -236,7 +238,7 @@ public class CustomerProcessorV1 extends libak_RestProcessor {
236238 List<Account > existingAccounts = [SELECT Id FROM Account WHERE Id = : accountId];
237239
238240 if (existingAccounts. isEmpty()) {
239- throw new libak_RestFramework . NotFoundException ( ' Account not found' );
241+ return new libak_ErrorResponse( 404 , ' Account are not found' , ' ' );
240242 }
241243
242244 delete existingAccounts. get(0 );
@@ -249,7 +251,7 @@ public class CustomerProcessorV1 extends libak_RestProcessor {
249251
250252We have good news for you we have almost everything to make our web service workable.
251253
252- The only thing which should be done is to execute our logic in the `@HttpGet `, `@HttpPost `, `@HttpPut `, and `@HttpDelete ` methods of the `CustomerWebService ` class.
254+ The only thing which should be done is to execute our logic in the ` @HttpGet ` , ` @HttpPost ` , ` @HttpPut ` , and ` @HttpDelete ` methods of the ` CustomerWebServiceDemo ` class.
253255
254256For this purpose there are a few static methods:
255257- ` handleRequest(Type routerType) `
@@ -264,7 +266,7 @@ Here is a full version of our web service:
264266
265267``` java
266268@RestResource (UrlMapping = ' /*/customers/*' )
267- global with sharing class CustomersWebService {
269+ global with sharing class CustomerWebServiceDemo {
268270 @HttpGet
269271 global static void doGet () {
270272 libak_RestFramework. handleRequest(CustomerRestRouter . class);
@@ -285,25 +287,25 @@ global with sharing class CustomersWebService {
285287 public class CustomerRestRouter extends libak_RestRouter {
286288 override public libak_RestRouter setRoutes () {
287289 this . routeToRestProcessorType = new Map<String, Type>{
288- ' /v1/customers' = > CustomersListProcessorV1 . class,
290+ ' /v1/customers' = > CustomersProcessorV1 . class,
289291 ' /v1/customers/:customer_sf_id' = > CustomerProcessorV1 . class
290292 };
291293 return this ;
292294 }
293295 }
294296
295- public class CustomersListProcessorV1 extends libak_RestProcessor {
297+ public class CustomersProcessorV1 extends libak_RestProcessor {
296298 protected override libak_RestFramework. IRestResponse handleGet () {
297299 List<Account > accounts = [
298300 SELECT Id , Name , Phone , BillingStreet , BillingCity , BillingState , BillingPostalCode
299301 FROM Account
300302 LIMIT 100
301303 ];
302304
303- if (! accounts. isEmpty()) {
304- return new libak_JsonResponse(accounts );
305+ if (accounts. isEmpty()) {
306+ return new libak_ErrorResponse( 404 , ' Accounts are not found ' , ' ' );
305307 } else {
306- throw new libak_RestFramework . NotFoundException ( ' Accounts are not found ' );
308+ return new libak_JsonResponse(accounts );
307309 }
308310 }
309311
@@ -324,17 +326,18 @@ global with sharing class CustomersWebService {
324326 ];
325327
326328 if (accounts. isEmpty()) {
327- throw new libak_RestFramework. NotFoundException (' Account not found' );
329+ return new libak_ErrorResponse(404 , ' Accounts are not found' , ' ' );
330+ } else {
331+ return new libak_JsonResponse(accounts. get(0 ));
328332 }
329- return new libak_JsonResponse(accounts. get(0 ));
330333 }
331334
332335 protected override libak_RestFramework. IRestResponse handlePut () {
333336 String accountId = this . getUriParam(' customer_sf_id' );
334337 List<Account > existingAccounts = [SELECT Id FROM Account WHERE Id = : accountId];
335338
336339 if (existingAccounts. isEmpty()) {
337- throw new libak_RestFramework . NotFoundException ( ' Account not found' );
340+ return new libak_ErrorResponse( 404 , ' Account are not found' , ' ' );
338341 }
339342
340343 Account updatedAccount = (Account )JSON . deserialize(this . request. requestBody. toString(), Account . class);
@@ -348,7 +351,7 @@ global with sharing class CustomersWebService {
348351 List<Account > existingAccounts = [SELECT Id FROM Account WHERE Id = : accountId];
349352
350353 if (existingAccounts. isEmpty()) {
351- throw new libak_RestFramework . NotFoundException ( ' Account not found' );
354+ return new libak_ErrorResponse( 404 , ' Account are not found' , ' ' );
352355 }
353356
354357 delete existingAccounts. get(0 );
0 commit comments