Bartłomiej Paluch
Bartłomiej Paluch

Reputation: 41

Class parameter name for default type in spring-kafka listener

I'm attempting to change the default type value for kafka listener with property "spring.json.value.default.type=" using my own annotation in spring-kafka. Currently, it's possible to overwrite it with following values: properties="spring.json.value.default.type=com.package.class" which is canonical name of class. I've made an annotation that sets the following value:

@MyAnnotation(topic = Topics.BUILD_CONFIG_CREATED, defaultType = ConstantsClass.TYPE_HEADER + "prz.student.finger.kafkaBSC.MyObjectDTO")

Is there any way to avoid hard typing the class name? I would like to implement the option to use the following code(just giving the class that was imported):

@MyAnnotation(topic = Topics.BUILD_CONFIG_CREATED, defaultType = MyObjectDTO.class)

The closest to I've got is adding in my annotation:

@AliasFor(annotation = KafkaListener.class, attribute = "properties")
    String defaultType() default headerType()+dtoType().getCanonicalName().toString();
String headerType() default "spring.json.value.default.type=";

Unfortunately, the constraints regarding the compilation time values for class in annotation blocks me from implementing it. Is there any way to inject the cannonical name without hard typing it, or any other way to implement this?

Upvotes: 0

Views: 1055

Answers (1)

Gary Russell
Gary Russell

Reputation: 174689

The properties property can contain SpEL (see its Javadocs).

Something like #{@someBean.type.name}; where someBean is a bean with a method public Class<?> getType().

Upvotes: 1

Related Questions