Spring Common Data 2.1.2 (and Data MongoDB 2.1.2) cannot deserialise Optional field from my class. It worked with previous version (2.0.8), now my object is saved in DB (with MongoRepository.save), but there is an exception when i try to read it (MongoRepository.findById). I've narrowed down an exact field and the problem is with storing Optional:
java.lang.UnsupportedOperationException: Cannot set immutable property java.util.Optional.value!
at org.springframework.data.mapping.model.BeanWrapper.setProperty(BeanWrapper.java:86) ~[spring-data-commons-2.1.2.RELEASE.jar:2.1.2.RELEASE]
at org.springframework.data.mapping.model.ConvertingPropertyAccessor.setProperty(ConvertingPropertyAccessor.java:61) ~[spring-data-commons-2.1.2.RELEASE.jar:2.1.2.RELE
and my class:
public class Coordinates { private double latitude; private double longitude; private Optional altitude; }
there is an exception when data in DB is in a form:
altitude: Object
value: 0
and no error when altitude doesn't have a 'value'
altitude: Object
(no value)
It worked with previous Spring Boot version (and Spring Data Commons/Mongodb 2.0.8).
I can't find any information about changed behaviour/different handling of Optionals etc.
How to solve it ?
Should i keep it as
private Double altitude;
with getter:
public Optional<Double> getAltitude() {
return Optional.ofNullable(altitude);
}
or any other way ?