
@RestController @RequestMapping("/api/emails") public class EmailController { private final EmailService emailService; public EmailController(EmailService emailService) { this.emailService = emailService; } @PostMapping public ResponseEntity<String> sendEmail( @RequestBody SendEmailRequestDto sendEmailRequestDto ) { emailService.sendEmail(sendEmailRequestDto); return ResponseEntity.ok("이메일 발송 요청 완료"); } }
public class SendEmailRequestDto { private String from; // 발신자 이메일 private String to; // 수신자 이메일 private String subject; // 이메일 제목 private String body; // 이메일 본문 // getter 함수 public String getFrom() { return from; } public String getTo() { return to; } public String getSubject() { return subject; } public String getBody() { return body; } }
public class EmailSendMessage { private String from; // 발신자 이메일 private String to; // 수신자 이메일 private String subject; // 이메일 제목 private String body; // 이메일 본문 public EmailSendMessage(String from, String to, String subject, String body) { this.from = from; this.to = to; this.subject = subject; this.body = body; } public String getFrom() { return from; } public String getTo() { return to; } public String getSubject() { return subject; } public String getBody() { return body; } }
@Service public class EmailService { // <메시지의 Key 타입, 메시지의 Value 타입> // Kafka에 넣는 메시지는 Key-Value 형태로 넣을 수도 있고, // Key는 생략한 채로 Value만 넣을 수도 있다고 얘기했다. // 실습에서는 메시지를 만들 때 key는 생략한 채로 value만 넣을 예정이다. private final KafkaTemplate<String, String> kafkaTemplate; public EmailService(KafkaTemplate<String, String> kafkaTemplate) { this.kafkaTemplate = kafkaTemplate; } public void sendEmail(SendEmailRequestDto request) { EmailSendMessage emailSendMessage = new EmailSendMessage( request.getFrom(), request.getTo(), request.getSubject(), request.getBody() ); // 위에서 메시지의 valueEmailSendMessage 타입을 String으로 설정을 했다. // 그래서 객체를 String으로 변환해서 넣어주어야 한다. this.kafkaTemplate.send("email.send", toJsonString(emailSendMessage)); } // 객체를 Json 형태의 String으로 만들어주는 메서드 // (클래스로 분리하면 더 좋지만 편의를 위해 메서드로만 분리) private String toJsonString(Object object) { ObjectMapper objectMapper = new ObjectMapper(); try { String message = objectMapper.writeValueAsString(object); return message; } catch (JsonProcessingException e) { throw new RuntimeException("Json 직렬화 실패"); } } }