The code for this microservice is a simple textbook REST service. This is a simple blocking service so that we can explore a simpler paradigm. Later we'll build a Webflux service when we build the Event Activity service. Here's the basic outline of the solution:
The client request
The client request is the input to the microservice REST API. It's a simplified json record with the basic requirements for sending emails. Any additional information required to build a SendGrid request are stored as configurations
POST/email/v2/send HTTP/1.1Host: localhost:5000Content-Type: application/jsonContent-Length:269{"senderName": "me","fromAddress": "sender@gmail.com","toAddress": "receiver@gmail.com","subject": "This is my email","content": "Click here to create a click event: http://www.google.com","customArgs": {"mycounter":60000 }}
The Controller
The controller is a simple rest service that our client calls. It handles the client request by passing it on to the main program and returning the final result back to the client.
SendGridController.java
/* You will often see examples where the Java object is passed as an object: * ResponseEntity<String> handleRequest(@RequestBody SendgridRequest mailRequest) letting Spring handle the json conversion implicitly - but some teams don't do it that way, preferring to pass in the json string and manage the conversion*/@PostMapping(path ="/send")publicResponseEntity<String>handleRequest(@RequestBodyString mailRequest) {HttpHeaders responseHeaders =newHttpHeaders();responseHeaders.set("Content-Type","application/json");try {String result =controllerFacade.requestHandler(mailRequest);returnResponseEntity.ok().headers(responseHeaders).body(result); } catch (Exception e) {returnResponseEntity.badRequest().headers(responseHeaders).body(e.getLocalizedMessage()); } }
The Handler
The handler function is just a pass-through - it's only function is to convert from a json string to a SendgridRequest Object and to provide a final catch for any errant runtime exceptions. The design paradigm is that everything returning to the caller is in the form of a valid Response object.