Created
January 12, 2021 10:01
-
-
Save stefanozanella/bcfefddd6f45af3b2e156093a6a7a62b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.stefanozanella.sampleapp | |
import com.sksamuel.avro4k.Avro | |
import org.apache.avro.generic.GenericRecord | |
import org.springframework.beans.factory.annotation.Autowired | |
import org.springframework.kafka.core.KafkaTemplate | |
import org.springframework.stereotype.Controller | |
import org.springframework.ui.Model | |
import org.springframework.web.bind.annotation.GetMapping | |
import org.springframework.web.bind.annotation.ModelAttribute | |
import org.springframework.web.bind.annotation.PostMapping | |
@Controller | |
class StockPurchaseController { | |
@Autowired | |
lateinit var kafka: KafkaTemplate<String, GenericRecord> // <-- (1) | |
@GetMapping("/") | |
fun newStockPurchase(model: Model): String { | |
model.addAttribute("stockPurchase", StockPurchase()) | |
return "addStockPurchase" | |
} | |
@PostMapping("/stock_purchase") | |
fun addStockPurchase(@ModelAttribute stockPurchase: StockPurchase): String { | |
val avroRecord = Avro.default.toRecord(StockPurchase.serializer(), stockPurchase) // <-- (2) | |
kafka.send( | |
"stock-purchases", | |
stockPurchase.ticker, | |
avroRecord // <-- (3) | |
) | |
return "stockPurchaseCreated" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment