mirror of
https://github.com/Dannecron/spring-boot-demo.git
synced 2025-12-26 08:42:33 +03:00
use kotlinx.serialization instead of ObjectMapper in kafka consumer
This commit is contained in:
@@ -2,7 +2,6 @@ package com.example.demo.config
|
|||||||
|
|
||||||
import com.example.demo.services.database.city.CityService
|
import com.example.demo.services.database.city.CityService
|
||||||
import com.example.demo.services.kafka.Consumer
|
import com.example.demo.services.kafka.Consumer
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper
|
|
||||||
import org.apache.kafka.clients.consumer.ConsumerConfig
|
import org.apache.kafka.clients.consumer.ConsumerConfig
|
||||||
import org.apache.kafka.common.serialization.StringDeserializer
|
import org.apache.kafka.common.serialization.StringDeserializer
|
||||||
import org.springframework.beans.factory.annotation.Autowired
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
@@ -23,10 +22,8 @@ class KafkaConsumerConfig(
|
|||||||
@Bean
|
@Bean
|
||||||
fun consumer(
|
fun consumer(
|
||||||
@Autowired cityService: CityService,
|
@Autowired cityService: CityService,
|
||||||
@Autowired objectMapper: ObjectMapper,
|
|
||||||
): Consumer = Consumer(
|
): Consumer = Consumer(
|
||||||
cityService = cityService,
|
cityService = cityService,
|
||||||
objectMapper = objectMapper,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package com.example.demo.services.kafka
|
|||||||
|
|
||||||
import com.example.demo.services.database.city.CityService
|
import com.example.demo.services.database.city.CityService
|
||||||
import com.example.demo.services.kafka.dto.CityCreateDto
|
import com.example.demo.services.kafka.dto.CityCreateDto
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper
|
import kotlinx.serialization.json.Json
|
||||||
import org.springframework.kafka.annotation.KafkaListener
|
import org.springframework.kafka.annotation.KafkaListener
|
||||||
import org.springframework.messaging.handler.annotation.Payload
|
import org.springframework.messaging.handler.annotation.Payload
|
||||||
import org.springframework.stereotype.Component
|
import org.springframework.stereotype.Component
|
||||||
@@ -10,14 +10,13 @@ import org.springframework.stereotype.Component
|
|||||||
@Component
|
@Component
|
||||||
class Consumer(
|
class Consumer(
|
||||||
private val cityService: CityService,
|
private val cityService: CityService,
|
||||||
private val objectMapper: ObjectMapper
|
|
||||||
) {
|
) {
|
||||||
@KafkaListener(
|
@KafkaListener(
|
||||||
topics = ["#{'\${kafka.consumer.topics}'.split(',')}"],
|
topics = ["#{'\${kafka.consumer.topics}'.split(',')}"],
|
||||||
autoStartup = "\${kafka.consumer.auto-startup:false}",
|
autoStartup = "\${kafka.consumer.auto-startup:false}",
|
||||||
)
|
)
|
||||||
fun handleCityCreate(@Payload message: String) {
|
fun handleCityCreate(@Payload message: String) {
|
||||||
val cityCreateDto = objectMapper.readValue(message, CityCreateDto::class.java)
|
val cityCreateDto = Json.decodeFromString<CityCreateDto>(message)
|
||||||
cityService.create(cityCreateDto)
|
cityService.create(cityCreateDto)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,16 +1,12 @@
|
|||||||
package com.example.demo.services.kafka.dto
|
package com.example.demo.services.kafka.dto
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
|
@Serializable
|
||||||
data class CityCreateDto (
|
data class CityCreateDto (
|
||||||
@JsonProperty("guid")
|
|
||||||
val guid: String,
|
val guid: String,
|
||||||
@JsonProperty("name")
|
|
||||||
val name: String,
|
val name: String,
|
||||||
@JsonProperty("createdAt")
|
|
||||||
val createdAt: String,
|
val createdAt: String,
|
||||||
@JsonProperty("updatedAt")
|
|
||||||
val updatedAt: String?,
|
val updatedAt: String?,
|
||||||
@JsonProperty("deletedAt")
|
|
||||||
val deletedAt: String?,
|
val deletedAt: String?,
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user