Elasticsearch Java Client Jackson Mapper pollutes Spring Jackson Mapper - elasticsearch

I'm updating my Spring Boot project to version 3.0.0 of Spring Boot.
One important dependecy is the Spring Data Elastic Repository. I included the starter dependency:
implementation("org.springframework.boot:spring-boot-starter-data-elasticsearch")
After updating the dependecies, my unit test are failing, because no more null values are provided by the web controller. I tried to change the behavior by using the property inclusion configuration:
spring:
jackson:
default-property-inclusion: always
This did not help. I debugged the code party and it seems like the new Elastic Java Client has it's own Jackson Mapper config, which sets the global config to 'NON_NULL'.
package co.elastic.clients.json.jackson;
...
public class JacksonJsonpMapper extends JsonpMapperBase {
private final JacksonJsonProvider provider;
private final ObjectMapper objectMapper;
...
public JacksonJsonpMapper(ObjectMapper objectMapper) {
this(
objectMapper
.configure(SerializationFeature.INDENT_OUTPUT, false)
.setSerializationInclusion(JsonInclude.Include.NON_NULL),
// Creating the json factory from the mapper ensures it will be returned by JsonParser.getCodec()
new JacksonJsonProvider(objectMapper.getFactory())
);
}
...
Does anybody know how to solve this issue?

I found a soultion for the problem. Spring Boot has an autoconfiguration for the Elastic Jackson mapper. This autocofiguration uses the same ObjectMapper as the web controller:
´´´
package org.springframework.boot.autoconfigure.elasticsearch;
...
/**
* Configurations for import into {#link ElasticsearchClientAutoConfiguration}.
*
* #author Andy Wilkinson
*/
class ElasticsearchClientConfigurations {
#ConditionalOnMissingBean(JsonpMapper.class)
#ConditionalOnBean(ObjectMapper.class)
#Configuration(proxyBeanMethods = false)
static class JacksonJsonpMapperConfiguration {
#Bean
JacksonJsonpMapper jacksonJsonpMapper(ObjectMapper objectMapper) {
return new JacksonJsonpMapper(objectMapper);
}
}
...
´´´
It is possible to override this Bean to copy the ObjectMapper instance and create an own ObjectMapper for the Elastic client:
´´´
#Configuration
internal class CustomJacksonJsonpMapperConfiguration {
#Bean
fun jacksonJsonpMapper(objectMapper: ObjectMapper): JacksonJsonpMapper {
return JacksonJsonpMapper(objectMapper.copy())
}
}
´´´

Related

Programmatic RedissonClient in Spring boot project

I am trying to implement Hibernate second level caching in a Spring boot project using Redisson.
I have followed this blog as a reference
https://pavankjadda.medium.com/implement-hibernate-2nd-level-cache-with-redis-spring-boot-and-spring-data-jpa-7cdbf5632883
Also i am trying to initialize the RedissionClient programmatically and not through declaratively /through a config file
Created a spring bean to be initialized which should create the RedissonClient instance.
#Configuration
#Lazy(value = false)
public class RedissonConfig {
#Bean
public RedissonClient redissionClient() {
Config config = new Config();
config.useSingleServer().setAddress("redis://127.0.0.1:6379");
return Redisson.create(config);
}
}
However this bean is never intialized and i get the following error while application startup.
Caused by: org.hibernate.cache.CacheException: Unable to locate Redisson configuration
at org.redisson.hibernate.RedissonRegionFactory.createRedissonClient(RedissonRegionFactory.java:107) ~[redisson-hibernate-53-3.12.1.jar:3.12.1]
at org.redisson.hibernate.RedissonRegionFactory.prepareForUse(RedissonRegionFactory.java:83) ~[redisson-hibernate-53-3.12.1.jar:3.12.1]
It seems Spring boot Hibernate still trying to load the Redisson config through a config file.
is it possible to load the Redission config in spring boot programmatically ?
Best Regards,
Saurav
I just did exactly this, here is how:
you need a custom RegionFactory that is similar to the JndiRedissonRegionFactory but gets its RedissonClient injected somehow.
an instance of this Class, fully configured, is put into the hibernate-properties map. Hibernates internal code is flexible: if the value of hibernate.cache.region.factory_class is a string it is treated as a FQDN. If it is an instance of Class<?>, it will be instantiated. If it is an Object, it will be used.
Spring offers a rather simple way to customize hibernate properties with a bean:
#AutoConfiguration(after = RedissonAutoConfiguration.class, before = JpaAutoConfiguration.class)
#ConditionalOnProperty("spring.jpa.properties.hibernate.cache.use_second_level_cache")
public class HibernateCacheAutoConfiguration {
#Bean
public HibernatePropertiesCustomizer setRegionFactory(RedissonClient redisson) {
return hibernateProperties -> hibernateProperties.put(AvailableSettings.CACHE_REGION_FACTORY, new SpringBootRedissonRegionFactory(redisson));
}
}
My RegionFactory is really simple:
#AllArgsConstructor
public class SpringBootRedissonRegionFactory extends RedissonRegionFactory {
private RedissonClient redissonClient;
#Override
protected RedissonClient createRedissonClient(Map properties) {
return redissonClient;
}
#Override
protected void releaseFromUse() {
}
}
I used the redisson-starter to get a RedissonClient, hence the reference to RedissonAutoConfiguration, but you could just create an instance by hand.
It is possible, but then you need to provide a custom implementation of RegionFactory to Hibernate, which can extends RedissonRegionFactory but uses your own client instance.

Springboot Webflux jackson deserialization not working

I have a springboot server in which I'musing webflux. I overrode the default Jackson ObjectMapper by setting a default type resolver, but when Flux encoder is not working as expected:
// Configuration.java
#Configuration
public class Configuration {
#Bean
Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
TypeResolverBuilder<?> typeResolver =
new ObjectMapper.DefaultTypeResolverBuilder(DefaultTyping.NON_FINAL).init(Id.CLASS, null)
.inclusion(As.PROPERTY);
return jacksonObjectMapperBuilder -> jacksonObjectMapperBuilder
.defaultTyping(typeResolver);
}
}
// Controller.java
#RestController
public class Controller {
#Autowired
ObjectMapper mapper;
#GetMapping(value = "/flux")
public Flux<Boolean> getFlux() throws Exception {
System.err.println(mapper.writeValueAsString(true)); // prints "true" : OK
return Flux.just(true); // Not returning "true" on the browser
}
}
When I test the endpoint with a browser, I get the following:
["org.springframework.web.servlet.mvc.method.annotation.ReactiveTypeHandler$CollectedValuesList",[true]]
Obviously the deserialization is wrong, not only it shouldn't include the type (because boolean is a final class), but also the representation is wrong (included as an array).
When I remove the jackson configuration, I get on the browser true as an output which is correct
Spring boot version: 2.1.2.RELEASE
Java version: 8

Kotlin spring-boot #ConfigurationProperties

I'm trying to create the following bean AmazonDynamoDBAsyncClientProvider. I've application.properties that defines endpoint and tablePrefix which I'm trying to inject using #ConfigurationProperties
Following is the code snippet for the same. When I run my spring-boot app it doesn't work.
I've tried doing the same ConfigurationProperties class using a regular java class which does set those properties but when it comes to AmazonDynamoDBAsyncClientProvider, the properties are empty. What am I missing here?
#Component
open class AmazonDynamoDBAsyncClientProvider #Autowired constructor(val dynamoDBConfiguration: DynamoDBConfig){
#Bean open fun getAmazonDBAsync() = AmazonDynamoDBAsyncClientBuilder.standard()
.withEndpointConfiguration(
AwsClientBuilder.EndpointConfiguration(dynamoDBConfiguration.endpoint, dynamoDBConfiguration.prefix))
.build()
}
here is the kotlin bean that I'm trying to autowire with configuration
#Component
#ConfigurationProperties(value = "dynamo")
open class DynamoDBConfig(var endpoint: String="", var prefix: String="")
finally heres the regular java bean that does get populated with ConfigurationProperties but when it gets Autowired I see those properties being empty/null
#Component
#ConfigurationProperties("dynamo")
public class DynamoDBConfiguration {
private String endpoint;
private String tablePrefix;
public String getEndpoint() {
return endpoint;
}
public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}
public String getTablePrefix() {
return tablePrefix;
}
public void setTablePrefix(String tablePrefix) {
this.tablePrefix = tablePrefix;
}
}
Have you tried getting rid of the #Component annotation on your ConfigurationProperties class?
Here is what I have done with Kotlin and Spring, hope it helps.
I am trying to leverage the kotlin-spring and kotlin-allopen gradle plugin
dependencies {
classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlinVersion"
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlinVersion"
}
apply plugin: 'kotlin-spring'
apply plugin: 'kotlin-noarg'
noArg {
annotation("your.noarg.annotation.package.NoArg")
}
They do make spring development with kotlin a lot easier.
#ConfigurationProperties("dynamo")
#NoArg
data class DynamoDBConfiguration(var endpoint: String, var prefix: String)
I tried your configuration class and it gets populated. I think your mistake is in the way you are trying to create the bean, the function needs to be in a class annotated with #Configuration, this should work:
#Configuration
class Beans {
#Bean
fun getAmazonDBAsync(config: DynamoDBConfiguration) =
AmazonDynamoDBAsyncClientBuilder.standard()
.withEndpointConfiguration(
AwsClientBuilder.EndpointConfiguration(config.endpoint, config.prefix)
)
.build()
}
Spring will inject the config for you, as long as you annotate the config with #Component, like you did above.
I had a similar problem and fixed it this way:
I defined the configuration properties class with lateinit vars:
#ConfigurationProperties(prefix = "app")
open class ApplicationConfigProperties {
lateinit var publicUrl: String
}
Then configured a bean in my spring boot application:
#SpringBootApplication
open class Application {
#Bean open fun appConfigProperties() = ApplicationConfigProperties()
}

Spring return image from controller while using Jackson Hibernate5Module

I am using Spring 4.3.1 and Hibernate 5.1.0 for my webapp.
For Jackson to be able serializing lazy objects I have to add the Hibernate5Module to my default ObjectMapper. This I have done via
#EnableWebMvc
#Configuration
#ComponentScan({ "xxx.controller" })
public class SpringWebConfig extends WebMvcConfigurerAdapter {
#Autowired
SessionFactory sf;
...
#Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
Hibernate5Module module = new Hibernate5Module(sf);
module.disable(Feature.USE_TRANSIENT_ANNOTATION);
module.enable(Feature.FORCE_LAZY_LOADING);
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.modulesToInstall(module);
converters.add(new MappingJackson2HttpMessageConverter(builder.build()));
super.configureMessageConverters(converters);
}
}
This is working but if it is enabled serializing a byte[] does not work anymore and fails with HTTP Status 500 - Could not write content: No serializer found for class java.io.BufferedInputStream
So my question is how to extend the default ObjectMapper while preserving the default ones?
I have seen somthing preserving the defaults using Spring Boot but I do not use Spring Boot. Any ideas?
As specified in the WebMvcConfigurer.configureMessageConverters javadoc, "If no converters are added, a default list of converters is registered", i.e. you will have to manually add all the default converters if you are using WebMvcConfigurer. Calling 'super.configureMessageConverters(converters)' does nothing if you extend WebMvcConfigurer. Take a look in 'WebMvcConfigurationSupport.addDefaultHttpMessageConverters(...)' to see all the default message converters, you can also extend this class instead of WebMvcConfigurer, with which you get slightly more clarity what happens.

Convert Spring bean configuration into XML configuration

i am working on BIRT reporting tool. which is need to called by spring MVC.
i got one example from spring which is here. in this example, configuration is done via bean. can anyone help me convert this configuration in to xml based configuration ?
#EnableWebMvc
#ComponentScan({ "org.eclipse.birt.spring.core","org.eclipse.birt.spring.example" })
#Configuration
public class BirtWebConfiguration extends WebMvcConfigurerAdapter {
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/reports").setViewName("birtView");
}
#Bean
public BirtView birtView() {
BirtView bv = new BirtView();
// bv.setReportFormatRequestParameter("ReportFormat");
// bv.setReportNameRequestParameter("ReportName");
bv.setBirtEngine(this.engine().getObject());
return bv;
}
#Bean
public BeanNameViewResolver beanNameResolver() {
BeanNameViewResolver br = new BeanNameViewResolver();
return br;
}
#Bean
protected BirtEngineFactory engine() {
BirtEngineFactory factory = new BirtEngineFactory();
return factory;
}
}
I wants a similar configuration in xml file.
There's really no tool for extracting Spring annotations to Spring bean context xml file. You'll have to do it by hand, shouldn't be too hard as all the Spring annotations functionality can be duplicated into Spring context xml tags.
if you want to use spingmvc, so no need the configuration files.
my solution is that in Birt Script i call the impl java file like this :
sampleService = new Packages.com.example.warlock.service.SampleServiceImpl();
pojo = new Packages.com.example.warlock.entity.Sample();
iterator = sampleService.getSamples().iterator();
because my SampleService is a interface and SampleServiceImpl is impl java, the two java file are not config as #Bean.
At first i want to get the data from ModelMap but failed, so i skip the controller and straight to call Service, then final call the DAO to get the Data from DB

Resources