Title: Spring RestController: custom date format for java.time.LocalDate does not work
Tags: java;spring;spring-boot;jackson
Question: I would like to change the default date format for the JSON serializer in my Spring-boot application. It seems that I really missed something because none of the following methods can work for me: 5 ways to customize Spring MVC JSON/XML output

Environment:


Java 11
Spring boot: 2.1.6.RELEASE


My relevant dependencies:

```<dependency&gt;
    <groupId&gt;org.springframework.boot</groupId&gt;
    <artifactId&gt;spring-boot-starter-data-jpa</artifactId&gt;
</dependency&gt;
<dependency&gt;
    <groupId&gt;org.springframework.boot</groupId&gt;
    <artifactId&gt;spring-boot-starter-web</artifactId&gt;
</dependency&gt;
<dependency&gt;
    <groupId&gt;io.springfox</groupId&gt;
    <artifactId&gt;springfox-swagger2</artifactId&gt;
</dependency&gt;
<dependency&gt;
    <groupId&gt;io.springfox</groupId&gt;
    <artifactId&gt;springfox-swagger-ui</artifactId&gt;
</dependency&gt;
<dependency&gt;
    <groupId&gt;javax.validation</groupId&gt;
    <artifactId&gt;validation-api</artifactId&gt;
</dependency&gt;
<dependency&gt;
    <groupId&gt;com.fasterxml.jackson.datatype</groupId&gt;
    <artifactId&gt;jackson-datatype-jsr310</artifactId&gt;
    <version&gt;2.10.0</version&gt;
</dependency&gt;
```

POJO:

```@Data
@NoArgsConstructor
public class User {

    ...

    @ApiModelProperty(example = "2019/01/01", notes = "date of birth as YYYY/MM/DD")
    @Past(message = "Are you really from the future?")
    private LocalDate dateOfBirth;
}
```

Spring configuration:

```@Configuration
public class OutputConfiguration {

    private static final String PATTERN = "yyyy/MM/dd";

    @Bean
    @Primary
    public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        objectMapper.setDateFormat(new SimpleDateFormat(pattern));
        return objectMapper;
    }
}
```

If I add ```@JsonFormat(pattern="yyyy/MM/dd")``` into the POJO, before the ```LocalDate``` variable then everything works fine. But because I have a lot of ```LocalDate``` variables I would like to add this configuration to the application level.

This is the error I get:

```org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.time.LocalDate` from String "2019/05/29": Failed to deserialize java.time.LocalDate: (java.time.format.DateTimeParseException) Text '2019/05/29' could not be parsed at index 4; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.LocalDate` from String "2019/05/29": Failed to deserialize java.time.LocalDate: (java.time.format.DateTimeParseException) Text '2019/05/29' could not be parsed at index 4
 at [Source: (PushbackInputStream); line: 2, column: 18] (through reference chain: com.remal.gombi.component.domain.User["dateOfBirth"])
```

What I am doing wrong?

UPDATE

If I add the following configuration, then the GET rest request returns with a properly formatted LocalDate: ```"dateOfBirth": "29/05/2019"```, BUT the POST method still expects the ISO date format: ```"dateOfBirth": "2019-01-01"```. It is so strange.

```@Bean
public Jackson2ObjectMapperBuilder customJacksonMapper() {
    return new Jackson2ObjectMapperBuilder()
            .indentOutput(true)
            .serializers(new LocalDateSerializer(DateTimeFormatter.ofPattern(DATE_PATTERN)));
}
```

Comment: According to [this tutorial](https://www.baeldung.com/spring-boot-formatting-json-dates), the @JsonFormat should be enough.

Comment: Have you tried with `spring.jackson.date-format=yyyy/MM/dd` in `application.properties`? This and other `Jackson Properties` can be found [here](https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html)

Comment: You can also write you own application level Seriliazer and add it using `@JsonSerialize`



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:10/09/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><spring><spring-boot><jackson>', 'Title': 'Spring RestController: custom date format for java.time.LocalDate does not work', 'ViewCount': 1017, 'id': 58311217, 'user_AboutMe': '<p>Hi, I am a senior software engineer.</p>\n', 'user_AccountId': 2492679.0, 'user_DisplayName': 'zappee', 'user_Reputation': 17318.0, 'user_Views': 881.0, 'user_id': 2169845.0}}