Comments (10)
If you are using swagger with spring boot and your date is Date is always getting serialised as long,
and SerializationFeature.WRITE_DATES_AS_TIMESTAMPS & spring.jackson.serialization.write-dates-as-timestamps=false
are not helping, below solution worked for me. Add it to your class annotated with @SpringBootApplication:
@Autowired
private RequestMappingHandlerAdapter handlerAdapter;
@EventListener
public void handleContextRefresh(ContextRefreshedEvent event) {
handlerAdapter
.getMessageConverters()
.stream()
.forEach(c -> {
if (c instanceof MappingJackson2HttpMessageConverter) {
MappingJackson2HttpMessageConverter jsonMessageConverter = (MappingJackson2HttpMessageConverter) c;
ObjectMapper objectMapper = jsonMessageConverter.getObjectMapper();
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
}
});
}
Actual issue: SerializationFeature.WRITE_DATES_AS_TIMESTAMPS value is not read from spring configuration file which need to be set false in order to covert long value while serialisation.
from jackson-modules-java8.
Where do you put this in your code?
ObjectMapper mapper = new ObjectMapper()
.registerModule(new ParameterNamesModule())
.registerModule(new Jdk8Module())
.registerModule(new JavaTimeModule())
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
Thanks!
from jackson-modules-java8.
That's most likely a problem coming from spring configuration.
- Check there is no version conflict (pulling different version of jackson)
- Use application properties to configure jackson (
spring.jackson.serialization.write-dates-as-timestamps=false
) - If the 2 solutions above do not work, start removing your
@Enable___
annotations one by one, they sometime override your configuration.
from jackson-modules-java8.
Thanks.
#2 (adding spring.jackson.serialization.write-dates-as-timestamps=false to application properties) fixed it. I I guess spring jackson properties overloads the objectMapper of the Jackson Configuration.
from jackson-modules-java8.
Assuming this gets resolved as described, as Spring config is out of Jackson's hands.
But if Jackson-side problems exist, may be reopened with a standalone test.
from jackson-modules-java8.
Nowhere, you aren't supposed to instantiate your ObjectMapper
yourself if you are using spring-boot. See my earlier answer.
from jackson-modules-java8.
Hey guys, sorry for reopening this issue.
I've tried all of the solutions above to solve this problem but my Spring Controller keeps serializing LocalDateTime
with it's properties instead of YYYY-MM-DD...
I think it could be a version conflict since I'm using other dependencies, but I don't know how to check for this. Can anyone shine a light on how can I do it?
from jackson-modules-java8.
Make sure you have this in your application.yml:
spring:
jackson:
serialization:
write-dates-as-timestamps: false
Keep the 3 noted in project title page in dependencies:
"com.fasterxml.jackson.module:jackson-module-parameter-names:{version}"
"com.fasterxml.jackson.datatype:jackson-datatype-jdk8:{version}"
"com.fasterxml.jackson.datatype:jackson-datatype-jsr310:{version}"
from jackson-modules-java8.
Actual issue: SerializationFeature.WRITE_DATES_AS_TIMESTAMPS value is not read from spring configuration
Should there be, or is there, an issue filed with Spring for this?
from jackson-modules-java8.
@cogito-abhijeet
Hi, your solution worked but is there any way to achieve this without the EventListener annotation as we have certain restriction on startup time and I was looking for a way to configure this without using the EventListener?
Actual issue: SerializationFeature.WRITE_DATES_AS_TIMESTAMPS value is not read from spring configuration file which need to be set false in order to covert long value while serialisation.
Is this issue reported to Spring?
from jackson-modules-java8.
Related Issues (20)
- Xml cannot read in Optional.empty HOT 6
- Add `JavaTimeFeature.NORMALIZE_DESERIALIZED_ZONE_ID` to allow disabling ZoneId normalization on deserialization HOT 5
- Jacskon can’t serialize Duration with Long.MIN_VALUE HOT 2
- Milliseconds timestamps not parsing correctly for `LocalDate` even with `READ_DATE_TIMESTAMPS_AS_NANOSECONDS` as FALSE HOT 8
- Breaking change in `InstantDeserializer API between 2.15 and 2.16 HOT 1
- `LocalDateTime` serialization issue with custom-configured `LocalDateTimeSerializer` HOT 12
- InstantDeserializer fails to parse negative numeric timestamp strings for pre-1970 values. HOT 2
- Custom java time module ignored after upgrade to 2.16 HOT 3
- `Optional` deserialization, serialization ignore `contentConverter` HOT 1
- NPE when serializing a `LocalDate` or `LocalDateTime` using `AsDeductionTypeSerializer` HOT 9
- OffsetTimeSerializer not writing seconds for time object like 15:30:00Z (where seconds is 00) HOT 2
- Implicit name provided by `ParameterNamesModule` overridden by "incomplete" `@JsonProperty` HOT 1
- I don't deserialize java 8 date/time type `java.time.Instant` HOT 2
- ZonedDateTime not serialised as UTC by default HOT 1
- Instants get not deserialized as expected sometimes HOT 2
- Only `DateTimeFormatter.ISO_OFFSET_DATE_TIME` accepted by `ZonedDateTimeKeyDeserializer` HOT 1
- Can't deserialize OffsetDateTime.MIN: Invalid value for EpochDay
- Date and time serialization are very inconsistent HOT 3
- Absent nested optional shennanigans HOT 1
- The serialization of an object of type LocalDate did not record its type HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jackson-modules-java8.