Joda time serialization (Spring Boot 1.3.1) - spring

I cannot serialize a date of type org.joda.time.DateTime with spring boot 1.3.1.
I have included the following dependencies:
joda-time:joda-time
com.fasterxml.jackson.datatype:jackson-datatype-joda
What could be the problem?
Thanks

I faced the same problem but it is not related to Spring Boot. It's got to do with the Jackson not recognizing the "DateTime" data type. I converted the datetime to a String before serialization.
However it is possible to directly serializing it by registering the Jackson module while initializing the ObjectMapper object. Check out How to serialize Joda DateTime with Jackson JSON processer?

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;
}

spring.jackson.default-property-inclusion=NON_NULL not working when added to application.yml file in spring boot 2.0

I am trying to add the jackson annotation at the application level via application.yml file. But when I run the application, the jackson annotation is ignored and the response has the null attributes as well. Cn someone help me here? I have added the below line in my application.yml
spring.jackson:
default-property-inclusion: NON_NULL
even after this I am seeing the response with null values.
Am I missing something or is there an issue with the version I m using? I am currently using jackson annotation version - 2.9.0
Property spring.jackson.default-property-inclusion: NON_NULL works with latest spring-boot version 2.5.0.
For older versions, where the property doesn't work, you can use #JsonInclude(Include.NON_NULL) annotation at class or field level.
Please note that field level annotation overrides the class level annotation, and class level annotation overrides the application level property.

Jackson Custom Deserializer/Serializer with Spring MongoTemplates

I am having trouble accessing and writing data from/to mongoDB using spring mongoTemplate.
For starters I have a data-model that represents the object that I am trying to retrieve from mongo. I have it annotated with #JsonSerialize and #JsonDeserialize for specifying custom converters.
However when I invoke mongoTemplate.findById(), and try to get this object, I find that my custom deserializer is not invoked at all and I get HttpMessageNotWriteableException.
Is there any other configuration that must be put in place to let mongo know that it needs to use my custom Jackson deserializer?
You can use this for reference: https://gist.github.com/letalvoj/978e6c975398693fc6843c5fe648416d

Spring Data REST MongoDB: Joda DateTime representation in REST

My Spring Data MongoDB entity has a property of Joda DateTime type:
#JsonProperty("myDate")
private DateTime myDate;
With Spring Boot 1.3.6, Spring Data 1.8.4, Spring Data REST 2.4.4 this property gets rendered as
"myDate": "2016-09-25T15:58:37.486Z"
in the REST representation of my Spring Data entity.
After I have updated the project dependencies to Spring Boot 1.4.1, Spring Data MongoDB 1.9.3, Spring Data REST 2.5.3 I suddenly get my date field represented as
"myDate": {
"content": "2016-09-25T15:58:37.486Z"
},
It looks like Joda's DateTime started to get treated as data entity again:
https://jira.spring.io/browse/DATAMONGO-624
Now I'm wondering how do I get back to my Spring Boot 1.3.6 DateTime representation in REST without downgrading to that Spring Boot version.
Edit:
Adding Jackson2 #JsonUnwrapped annotation to the property helps to get representation back:
#JsonProperty("myDate")
#JsonUnwrapped
private DateTime myDate;
This is a sub-optimal solution for me though, since my properties get auto-generated from JSON schema and I have limited control over generated annotations.
Edit 2:
The Javadoc for the CustomConversions class states that
These types will be considered simple ones (which means they neither
need deeper inspection nor nested conversion. Thus the
CustomConversions also act as factory for SimpleTypeHolder
Which is in fact not true for Spring Data REST [any more], from what I can tell debugging my code.
The types that are added via default CustomConversions are not treated as simple ones when rendering JSON and properties of those types get serialised as embedded entity objects.
Here is the place in the Spring Data REST PersistentEntityJackson2Module that calls to Spring Data MongoDB PersistentEntity implementation to check whether the property type is simple or not. And since the simpleTypeHolder in the Spring Data Commons AbstractMappingContext does not contain the types for which Spring Boot auto-configuration adds Joda DateTime converters, the DateTime field is treated as complex object.
Edit 3:
Tracking this issue in JIRA:
https://jira.spring.io/browse/DATAREST-907

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?

Resources