Rest API controller

Let's see a Rest Controller class that we'll use to create a REST API endpoint. This controller will trigger sending a message to Kafka using the NotificationService Spring Bean:

@RestController
public class CustomerController {
...
@Autowired
CustomerRepository customerRepository;
@Autowired
AccountService accountService;
@Autowired
NotificationService notificationService;
@PostMapping(value = "/customer")
public Customer save (@RequestBody Customer customer){
Notification notification = new Notification("Customer is created", "[email protected]", "9852XXX122");
notificationService.sendNotification(notification);
return customerRepository.save(customer);
}
...
...
}

As you can see in the preceding Controller class of Customer service, this class has a dependency with NotificationService. The save() method is responsible for creating a customer in the corresponding database, and also it creates a notification message using the Notification object and sends it to Kafka using the sendNotification() method of NotificationService. Let's see another side how Kafka listen to this message using topic name notification.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset