|
| 1 | +# F-Commons REST Framework |
| 2 | +## Purpose |
| 3 | +The purpose of this artifact is to handle any kind of uncaught exceptions in an HTTP Request, handle it, and |
| 4 | +return a standardized response to the client. |
| 5 | + |
| 6 | +A secondary purpose is to give a easy to access request context which contains the `HttpServletRequest`, |
| 7 | +`HttpServletResponse`, and client time-zone. |
| 8 | + |
| 9 | +## How To Use |
| 10 | +### Add the Maven Dependency |
| 11 | +```xml |
| 12 | +<dependency> |
| 13 | + <groupId>com.fd</groupId> |
| 14 | + <artifactId>fcommons-rest</artifactId> |
| 15 | + <version>${fcommons.version}</version> |
| 16 | +</dependency> |
| 17 | +``` |
| 18 | + |
| 19 | +### Extend Your Rest Controller from `FinopsifyRestController` |
| 20 | +In your Rest Controllers, just extend from `FRestController` and use its `success()` or `success(data)` |
| 21 | +methods for returning response to client. |
| 22 | + |
| 23 | +```java |
| 24 | +import com.fcommons.rest.FRestController; |
| 25 | + |
| 26 | +import org.springframework.http.ResponseEntity; |
| 27 | + |
| 28 | +import org.springframework.web.bind.annotation.GetMapping; |
| 29 | +import org.springframework.web.bind.annotation.PathVariable; |
| 30 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 31 | +import org.springframework.web.bind.annotation.RestController; |
| 32 | + |
| 33 | +@RestController |
| 34 | +@RequestMapping("/account") |
| 35 | +public class AccountController extends FRestController { |
| 36 | + |
| 37 | + @GetMapping("/{id}") |
| 38 | + public ResponseEntity<AwsAccount> getAccountById(@PathVariable("id") String accountId) { |
| 39 | + AwsAccount account = ...; |
| 40 | + return this.success(account); |
| 41 | + } |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +### Unexpected Conditions |
| 46 | +All unhandled situations will be caught and handled by the `FRestControllreAdvice`. Nothing needed to be done |
| 47 | +for this mechanism to work. It will be active as you add the dependency. Just make sure `com.fcommons` is |
| 48 | +included in your package scan. |
| 49 | + |
| 50 | +With the `FRestControllerAdvice` in place: |
| 51 | +* you don't need to use try { ... } catch(...) blocks unless you'll execute a different and meaningful handling mechanism |
| 52 | +* you don't need to worry about invalid arguments or missing parameters |
| 53 | +* you can throw any type of exception from any layer for unexpected situations and don't worry about the rest. It will |
| 54 | +be caught and handled by the `FControllerAdvice` and the client will receive the message that you put into the |
| 55 | +exception. |
| 56 | + |
| 57 | + |
| 58 | +### Errors in the Response Body |
| 59 | +When the `FRestControllerAdvice` catches an exception, it returns an array of error items. Each error item has |
| 60 | +the following fields: |
| 61 | +* **title**: The error title |
| 62 | +* **message**: The error message |
| 63 | +* **type**: FErrorType: (Default, Object, or Field) Object and field error types are produced for invalid input |
| 64 | +parameters if you annotate your input parameters with `@Valid`. Any other type of error is considered as Default error. |
| 65 | + |
| 66 | +When you put @Valid annotation to your input parameters in your endpoints, Spring MVC automatically validates that |
| 67 | +object based on the bean validation specifications that you provide in your parameter's class definition. For each |
| 68 | +validation error, the `FRestControllerAdvice` will add separate error item into the response. |
| 69 | + |
| 70 | +```json |
| 71 | +{ |
| 72 | + "errors": [ |
| 73 | + { |
| 74 | + "title": "accountId", |
| 75 | + "message": "Invalid account id. The account id should contain exactly 12 characters", |
| 76 | + "type": "field-error" |
| 77 | + }, |
| 78 | + { |
| 79 | + "title": "email", |
| 80 | + "message": "Please provide a valid e-mail address", |
| 81 | + "type": "field-error" |
| 82 | + } |
| 83 | + ] |
| 84 | +} |
| 85 | +``` |
| 86 | +In this response, the title of the each error item correspond to a field name of the related input parameter. |
| 87 | + |
| 88 | +For the cases other than the invalid input parameter, the errors array will always contain one item which will include the |
| 89 | +thrown exception's message like below: |
| 90 | + |
| 91 | +```json |
| 92 | +{ |
| 93 | + "errors": [ |
| 94 | + { |
| 95 | + "title": "Server error", |
| 96 | + "message": "Account not found for the given provisioning type: COST_REPORTING", |
| 97 | + "type": "default-error" |
| 98 | + } |
| 99 | + ] |
| 100 | +} |
| 101 | +``` |
| 102 | + |
0 commit comments