Spring Data REST MongoDB: Joda DateTime representation in REST - spring

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

Related

#Transient - Why data is getting saved in Database?

I have an entity variable annotated with #Transient like below. Which means it should not be stored in the Database.
#Transient
private String passwordConfirm;
But when I go to H2-Console, I can see the data is saved there.
Why so? and How can I avoid it?
You're probably using #org.springframework.data.annotation.Transient.
Change it to the right import: #javax.persistence.Transient
This will do the job.
#javax.persistence.Transient is used by the persistence provider (eg.: Hibernate). The persistence provider looks for JPA spec annotations. #org.springframework.data.annotation.Transient is not a part of the JPA spec, so, the persistence provider ignores it.
The #org.springframework.data.annotation.Transient is intended to be used when Spring Data is the actual ORM. Some examples are Spring Data Elasticsearch and Spring Data MongoDB. These Spring Data implementations use the #org.springframework.data.annotation.Transient just like Hibernate uses the #javax.persistence.Transient - not mapping the marked field into the database.

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 Boot/JPA not persisting with service layer

I'm using Spring boot along with Spring Data JPA.
I'm stucking in an odd scenario when saving a new entity.
Unsing method from extended CrudRepository class, all works as expected.
If I inject via #Autowired the CrudRepository interface in my service layer, the method still works, but nothing is persisted on db.
The returned object from 'save' method seems ok, since I get an always increasing ID value.
Suggestions?
Cheers
FB
check whether we are populating the data properly in your bean and check it before passing to save method of spring data jpa

Joda time serialization (Spring Boot 1.3.1)

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?

JPA validation API

Does JPA contains any validation API?
E.g. email property validation like this in hibernate validation:
#Email
public String getUserEmail() {
...
}
Bean Validation (JSR303) can be used nicely alongside JPA, but is a separate API. This is a very brief introduction to Bean Validation itself, and see this for how an introduction of how it can be used with JPA.
Note: Similar to JPA, you will have to pick an implementation, of which there are many. Hibernate Validator is the reference implementation.

Resources