Created
December 29, 2023 15:27
-
-
Save pavelfomin/6cc1b5cb2268a289bb570c0aae42ea61 to your computer and use it in GitHub Desktop.
ObjectMapper MixIn configuration for Avro classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.annotation.JsonAutoDetect; | |
import org.apache.avro.generic.GenericContainer; | |
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; | |
@Configuration | |
public class ObjectMapperConfiguration { | |
/** | |
* The context’s Jackson2ObjectMapperBuilder can be customized by one or more Jackson2ObjectMapperBuilderCustomizer beans. | |
* Such customizer beans can be ordered (Boot’s own customizer has an order of 0), letting additional customization be applied both before and after Boot’s customization. | |
*/ | |
@Bean | |
Jackson2ObjectMapperBuilderCustomizer jacksonCustomizer() { | |
// apply AvroSchemaJacksonMixin for every Avro class that needs to be [de]serialized as Json | |
return (Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) -> | |
jacksonObjectMapperBuilder.mixIn(GenericContainer.class, AvroSchemaJacksonMixin.class); | |
} | |
/** | |
* Use only public properties to serialize Avro objects excluding Avro specific fields and methods. | |
*/ | |
@JsonAutoDetect( | |
fieldVisibility = JsonAutoDetect.Visibility.ANY, | |
getterVisibility = JsonAutoDetect.Visibility.NONE, | |
setterVisibility = JsonAutoDetect.Visibility.NONE, | |
creatorVisibility = JsonAutoDetect.Visibility.ANY | |
) | |
static abstract class AvroSchemaJacksonMixin { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
AvroSchemaJacksonMixin
definition above uses fields names instead of getters/setters.Alternatively, specific accessor methods can be ignored: