Deserialize header to Pojo in Webflux? - spring

I have a header that's some encoded Json, so I have a POJO to deserialize it to.
How do I make Spring Boot auto deserialize it?
I've tried creating a #Component Test implements Converter<String, MyPojo> but it never gets called. The one example I found was for WebMvc.

Related

Configuring custom Kafka Consumer Deserializer in application.properties file. [spring boot]

I want to consume avro messages and deserialize them without using the Confluent schema registry. I have the schema locally. So, for that, I followed this https://medium.com/#mailshine/apache-avro-quick-example-in-kafka-7b2909396c02 for Consumer part only. Now, I want to configure this deserializer in the application.properties file (the Spring boot way).
I tried adding
spring.kafka.consumer.value-deserializer=com.example.AvroDeserializer
But this results in error saying "Could not find a public no-argument constructor for com.example.AvroDeserializer".
Is there any way to call the constructor with argument from application.properties configuration.
Or
Do I need to configure this in Code instead of properties?
Thanks in advance!!
You can do it using properties, but you have to do it via the configure method (with a no-arg constructor) - this is because Kafka instantiates the deserializer, not Spring.
See the org.springframework.kafka.support.serializer.JsonDeserializer source code for an example.
Otherwise, you can just inject your Deserializer into the consumer factory...
#Bean
MyDeserializer(DefaultKafkaConsumerFactory<String, Foo> factory) {
MyDeserializer<Foo> deser = new MyDeserializer<>(...);
factory.setValueDeserializer(deser);
return deser;
}

How does a spring converter get a transactional scope?

I was wondering, how a spring converter implementation gets an transactional scope.
Having a Converter which is used to convert a path placeholder (entity id) in an (rest) controller to the Entity itself. The Entity thus is loaded via Hibernate from the database.
For the details:
spring boot 2.3.2
A rest controller endpoint which triggers the converter to expand a path-placeholder (prior executing the rest-endpoint method body)
If in question, the controller method is not annotated #Transactional either
Question:
How (since we disabled OSIV) does this Converter get it's Transactional scope if the convert neither the method/nor the converter class is annotated using Transactional nor the convert method does custom transaction handling?
If you mean how Spring Data repositories get a transaction, the answer is all repository methods are transactional by default.

How can I force #AutoConfigureJsonTesters to use HAL format from spring-hateoas?

I have a small module that should only contain the resource model of my REST service. I want to create a test in this module to ensure that the resource model serializes and deserializes appropriate HAL format.
I have a single test and this is the configuration:
#RunWith(SpringRunner::class
#SpringBootTest
#AutoConfigureJsonTesters
class MyTest {
#Autowired
private lateinit var collectionTester: JacksonTester<Resources<Entity>>
....
}
and a very simple configuration
#SpringBootConfiguration
class TestConfig
When calling collectionTester.write on a list of Entity (which extends ResourceSupport) I don't get an _embedded field, instead I get
{"links":[],"content":[...]}
which is not HAL format.
How can I force #AutoConfigureJsonTesters to give me a JacksonTester with an ObjectMapper configured for HAL?
Spring Boot 2.0.0.RELEASE
Thanks!
The auto-configured JacksonTester will use the context’s ObjectMapper which won’t have any of the Spring HATEOAS stuff configured on it. You might be better creating a JacksonTester yourself and passing it an appropriately configured ObjectMapper to use.
I believe Spring HATEOAS has a module that it applies to the ObjectMapper to configure it. If you get stuck with that, asking in gitter/spring-projects/spring-data is probably your best bet as Spring HATEOAS is maintained by the Data team due to it being used by Spring Data REST.

Spring Boot and Jackson, JSR310 in response body

The JacksonAutoConfiguration initializes an ObjectMapper with the JSR310Module registered. Then HttpMessageConvertersAutoConfiguration initializes a MappingJackson2HttpMessageConverter. But this MessageConverter never gets plugged in the RequestResponseBodyMethodProcessor.
As a result:
If the controller returns a object which should be jsonified by Jackson, dates are poorly converted because the wrong MappingJackson2HttpMessageConverter is used (initialized in WebMvcConfigurationSupport#addDefaultHttpMessageConverters)
But, if the ObjectMapper is injected in the controller and used to jsonify the same object, then the date format is alright.
How can I can wake the WebMvcAutoConfigurationAdapter up? What should I do to bind the configured Jackson ObjectMapper with Spring MVC and make it handle response bodies?
If you use #EnableWebMvc that would explain your symptoms So the rule thumb is: do not use #EnableWebMvc with Spring Boot, unless you wan't to customize Web MVC configuration?

Spring-Boot 0.5.0.BUILD-SNAPSHOT and customer Json MappingJackson2HttpMessageConverter

Has anyone working with Spring-Boot figured out how to override the default Jackson ObjectMapper that is automatically configured? I have tried to instantiate both the ObjectMapper as a bean and within a MappingJackson2HttpMessageConverter also as a been. But with no success...
Any thoughts?
Spring handles HTTP #ResponseBody using the HttpMessageConverter abstraction, so it's there that you would have to add customizations. Normally you would add a #Bean extending WebMvcConfigurerAdapter and override the configureMessageConverters() method.
Adding a #Bean of type ObjectMapper sounds like a neat way to do this for a Spring Boot application, so you could provide that feature in a pull request if you like.

Resources