Code Monkey home page Code Monkey logo

Comments (10)

cogito-abhijeet avatar cogito-abhijeet commented on July 30, 2024 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.

Dienry avatar Dienry commented on July 30, 2024 4

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.

bendem avatar bendem commented on July 30, 2024 1

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.

ddyangz avatar ddyangz commented on July 30, 2024

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.

cowtowncoder avatar cowtowncoder commented on July 30, 2024

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.

bendem avatar bendem commented on July 30, 2024

Nowhere, you aren't supposed to instantiate your ObjectMapper yourself if you are using spring-boot. See my earlier answer.

from jackson-modules-java8.

tibuurcio avatar tibuurcio commented on July 30, 2024

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.

ddyangz avatar ddyangz commented on July 30, 2024

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.

kupci avatar kupci commented on July 30, 2024

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.

MisterDurden avatar MisterDurden commented on July 30, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.