Class parameter name for default type in spring-kafka listener

36
June 23, 2022, at 09:20 AM

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?

Answer 1

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

READ ALSO
Springboot Qaurtz Scheduler skips the next fire time

Springboot Qaurtz Scheduler skips the next fire time

I am using Quartz Scheduler to schedule some Jobs, when I was using a simple trigger for hourly and daily intervals it was working fine

53
Why I am unable to wrap the execution of a request with an @Around advice?

Why I am unable to wrap the execution of a request with an @Around advice?

I work on a REST API, and I'd like to do some very simplistic tests to see how long it takes to process a request

40
removeRow using XSSF libraries is not working

removeRow using XSSF libraries is not working

I tried the below code, but it's not working

71