Skip to content
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

Closed
wants to merge 10 commits into from
Closed

Conversation

RemusRD
Copy link

@RemusRD RemusRD commented Jan 3, 2019

Hi!, first PR here, hope it all works, just tell me if anything has to be fixed, thanks!

Copy link
Member

@artembilan artembilan left a 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!

@@ -0,0 +1,29 @@
# Created by .ignore support plugin (hsz.mobi)
Copy link
Member

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

== 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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")
Copy link
Member

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

ext {
kotlinVersion = "1.3.10"
springBootVersion = "2.1.1.RELEASE"
arrow_version = "0.8.1"
Copy link
Member

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

apply plugin: "kotlin-jpa"


group "com.remusrd"
Copy link
Member

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.*
Copy link
Member

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> {
Copy link
Member

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.

Copy link
Author

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> {
Copy link
Member

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().

Copy link
Author

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
Copy link
Member

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
Copy link
Member

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?

Copy link
Author

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!

Copy link
Member

@artembilan artembilan left a 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"
Copy link
Member

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"
Copy link
Member

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.*
Copy link
Member

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
Copy link
Member

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
Copy link
Member

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
Copy link
Member

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")
Copy link
Member

@artembilan artembilan Jan 8, 2019

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.`
Copy link
Contributor

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.

Copy link
Contributor

@garyrussell garyrussell left a 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(),
Copy link
Contributor

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.

@artembilan
Copy link
Member

I'm closing this one as not fully Kafka related: too much Kotlin, too much JPA.
The sample should be as simple as possible to give a reader a chance to learn something.
Plus there is no reporter reaction for our review for a long time.

Thanks for understanding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants