-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added custom serializer example #924
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, find some feedback from me in code lines.
In general this is great, although looks a little complicated.
Maybe just because I don't know Kotlin well...
Thank you very much so far!
samples/sample-04/.gitignore
Outdated
@@ -0,0 +1,29 @@ | |||
# Created by .ignore support plugin (hsz.mobi) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, copy/paste content from one existing .ignore
file, e.g. : https://github.com/spring-projects/spring-kafka/blob/master/samples/sample-01/.gitignore
samples/sample-04/README.adoc
Outdated
== Sample 4 | ||
|
||
This sample uses a custom kafka serializer and deserializer in order to consume and send events using the jdk8 LocalDatetime | ||
and Kotlin data classes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, follow One sentence per line rule: https://asciidoctor.org/docs/asciidoc-recommended-practices/#one-sentence-per-line
samples/sample-04/build.gradle
Outdated
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}") | ||
classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}") | ||
classpath("org.jetbrains.kotlin:kotlin-noarg:${kotlinVersion}") | ||
classpath("io.spring.gradle:dependency-management-plugin:1.0.2.RELEASE") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like there is a newer version of this plugin: https://github.com/spring-gradle-plugins/dependency-management-plugin/releases
samples/sample-04/build.gradle
Outdated
ext { | ||
kotlinVersion = "1.3.10" | ||
springBootVersion = "2.1.1.RELEASE" | ||
arrow_version = "0.8.1" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this about?
Doesn't look like this property is used somewhere in the buildscript
definition
samples/sample-04/build.gradle
Outdated
apply plugin: "kotlin-jpa" | ||
|
||
|
||
group "com.remusrd" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's wrong group if you contribute into this project.
See all other samples here on the matter.
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder | ||
import org.springframework.kafka.core.* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No asterisk imports, please.
|
||
|
||
@Bean | ||
fun kafkaTemplate(): KafkaTemplate<Any, Any> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we rely on Spring Boot, I don't see reason to declare this bean.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was because IntelliJ says there are no beans of type KafkaTemplate, I guess it is a bug of some type
|
||
|
||
@Bean | ||
fun defaultKafkaConsumerFactory(): ConsumerFactory<Any, Any> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need to override auto-configured beans?
This one and the next - defaultKafkaProducerFactory()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What should I do instead?, creating my own beans without replacing the default ones?
|
||
@Target(AnnotationTarget.CLASS) | ||
@Retention(AnnotationRetention.SOURCE) | ||
annotation class NoArg |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need this annotation and the next Open
one?
Aren't there something already explicit in Kotlin world?
Like I see those in the Gradle config:
apply plugin: "kotlin-allopen"
apply plugin: "kotlin-noarg"
spring.kafka.bootstrap-servers: localhost:9092 | ||
spring.kafka.consumer.group-id: noteGroup | ||
spring.kafka.consumer.auto-offset-reset: earliest | ||
spring.kafka.consumer.properties.spring.json.trusted.packages: com.remusrd.notesample.domain.event |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you reformat this file to really true YAML style?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for the feedback, will fix it in the next days!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, consider the next review round.
Thanks
implementation "org.springframework.boot:spring-boot-starter-web" | ||
implementation "org.springframework.boot:spring-boot-starter-data-jpa" | ||
implementation "org.springframework.kafka:spring-kafka" | ||
implementation "com.fasterxml.jackson.module:jackson-module-kotlin:2.9.7" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need an explicit version for this dependency as well. It is managed by the jackson-bom
: https://github.com/FasterXML/jackson-bom/blob/master/pom.xml#L258
|
||
// BBDD | ||
implementation "mysql:mysql-connector-java:8.0.13" | ||
implementation "com.h2database:h2:1.4.197" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two are managed by Spring Boot: we don't need their versions.
import com.example.domain.Note | ||
import com.example.service.NoteService | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.web.bind.annotation.* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, revise all the classes for avoiding asterisk imports.
import com.example.domain.annotation.Open | ||
|
||
@NoArg | ||
@Open |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need these annotations?
Why the mentioned plugins not enough?
import org.springframework.boot.autoconfigure.SpringBootApplication | ||
|
||
@SpringBootApplication | ||
class NoteSampleApplication |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why file name doesn't match class name?
properties: | ||
spring: | ||
json: | ||
trusted:packages: com.remusrd.notesample.domain.event |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this package?
Doesn't look like legitimate package name in the public project.
annotation("com.remusrd.notesample.domain.annotation.NoArg") | ||
} | ||
allOpen{ | ||
annotation("com.remusrd.notesample.domain.annotation.Open") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like we can just remove these two declarations.
First of all - wrong package.
Secondly if we are here, then project even works without these two annotations.
I'm confused, so everybody else is going to be too. 🤷♂️
|
||
Docker containers needed are in `samples/sample-04/docker`. | ||
|
||
You can do POST's and GET's to the `localhost:8080/notes endpoint.` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can do POST's and GET's to the localhost:8080/notes
endpoint.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution, but I concur with @artembilan - the samples here are meant to be small and compact snippets to show how to perform simple tasks, rather than fully functional applications. Configuring a custom deserializer is a rather trivial task and a user shouldn't have to plough through all this code to find out how to do it.
It probably belongs in some external samples repo.
val jsonDeserializer = JsonDeserializer<Any>(objectMapper) | ||
jsonDeserializer.configure(kafkaProperties.buildConsumerProperties(), false) | ||
val kafkaConsumerFactory = DefaultKafkaConsumerFactory<Any, Any>( | ||
kafkaProperties.buildConsumerProperties(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, the Boot team does not consider KafkaProperties
a public API and we would be conflicting with their position if we put code like this in an official Spring repo, promoting its use. They asked us to remove all such references from the documentation.
I'm closing this one as Thanks for understanding. |
Hi!, first PR here, hope it all works, just tell me if anything has to be fixed, thanks!