Skip to content

JsonFormat does not affect serialisation of Date #4065

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
marccollin opened this issue Sep 30, 2015 · 7 comments
Closed

JsonFormat does not affect serialisation of Date #4065

marccollin opened this issue Sep 30, 2015 · 7 comments
Assignees
Labels
status: invalid An issue that we don't feel is valid

Comments

@marccollin
Copy link

i use spring boot 1.3.0 (tried to 1.3.0.M5), spring-data-jpa, spring rest and mysql.
i use java 8.

In my dto i have

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy", timezone="EST")
private Date birthdate;

client still continue to receive yyyy-mm-dd

@wilkinsona
Copy link
Member

@marccollin Can you please try with 1.3.0.BUILD-SNAPSHOT? #3891 made some changes to Jackson configuration, particularly when you're also using Spring Data REST or Spring HATEOAS.

@wilkinsona wilkinsona added the status: waiting-for-feedback We need additional information before we can continue label Sep 30, 2015
@marccollin
Copy link
Author

tried, get same result.

sent the setup of the application, i use only annotation.

@EntityScan(basePackageClasses = { ServerApplication.class, Jsr310JpaConverters.class })
@SpringBootApplication
@EnableCaching
public class ServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class, args);
    }
}

@wilkinsona wilkinsona removed the status: waiting-for-feedback We need additional information before we can continue label Oct 1, 2015
@wilkinsona
Copy link
Member

Thanks for trying with a snapshot

@wilkinsona wilkinsona self-assigned this Oct 1, 2015
@wilkinsona
Copy link
Member

@marccollin I can't reproduce the problem on 1.3.0.M5 or 1.3.0.BUILD-SNAPSHOT. I've tried with both a plain @RestController and using a controller provided by Spring Data REST that's derived from a JPA CrudRepository.

If you'd like me to investigate further, please provide a sample project that reproduces the issue.

@wilkinsona wilkinsona added the status: waiting-for-feedback We need additional information before we can continue label Oct 1, 2015
@marccollin
Copy link
Author

Link to a small project!

it use mysql.

so create a database named chezlise.

all the schema will be created.

if no data is created automatically only do
INSERT INTO lodger (birthdate) values(curdate());

@wilkinsona
Copy link
Member

This doesn't have anything to do with Spring Boot. You're returning a java.sql.Date and Jackson doesn't appear to apply the specified formatting to it. You can see the problem with the following code that doesn't involve Boot at all:

    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println(objectMapper.writeValueAsString(new Foo(new java.util.Date())));
        System.out.println(objectMapper.writeValueAsString(new Foo(new java.sql.Date(System.currentTimeMillis()))));
    }

    static class Foo {

        @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy", timezone="EST")
        private Date birthdate;

        public Foo() {

        }

        public Foo(Date birthdate) {
            this.birthdate = birthdate;
        }

        public Date getBirthdate() {
            return birthdate;
        }

    }

It produces the following output:

{"birthdate":"01/10/2015"}
{"birthdate":"2015-10-01"}

Note that the Jackson documentation has this to say about java.sql.Date:

Notes on java.sql.Date

(aka "Please do NOT use java.sql.Date, ever!")

Although Jackson supports java.sql.Date, there are known issues with respect to timezone handling, partly due to design of this class. It is recommended that this type is avoided, if possible, and regular java.util.Date (or java.util.Calendar) used instead. If this is not possible, it may be necessary for applications to convert these dates using java.util.Calendar and explicit timezone definition.

@wilkinsona wilkinsona added status: invalid An issue that we don't feel is valid and removed status: waiting-for-feedback We need additional information before we can continue labels Oct 1, 2015
@marccollin
Copy link
Author

if i use @TeMPOraL(javax.persistence.TemporalType.TIMESTAMP)

that work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

2 participants