1

I use spring boot and spring 4.

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

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

I try to use Java 8 LocalDate.

In the json for the date i have

birthdate: "1969-12-29"

On the server side for this field i have

@DateTimeFormat(iso=DateTimeFormat.ISO.DATE)
private LocalDate birthdate;

I get this error

org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not instantiate value of type [simple type, class java.time.LocalDate] from String value ('1969-12-29'); no single-String constructor/factory method

Edit

Dto send via ajax

public class LodgerInformationDto {
    private Long lodgerId;
    private String firstName;
    private String lastName;
    @DateTimeFormat(iso=DateTimeFormat.ISO.DATE)
    private LocalDate birthdate;
}

Controller Rest class

    @RequestMapping(value = "/lodgers", method = RequestMethod.POST)
    public LodgerInformationDto createLodger(@RequestBody @Valid final LodgerInformationDto lodgerDto) {
        return lodgerService.save(lodgerDto);
    }

with this dependencies: com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.6.3

this line in the application.properties spring.jackson.serialization.write-dates-as-timestamps=false

that work.

2
  • show the complete code please Commented Nov 21, 2015 at 2:07
  • I hope you recognize that your current solution is what I suggested. @DateTimeFormat does nothing in that case. Also, that property you added serves no purpose for the deserializing you're doing. Commented Nov 22, 2015 at 0:36

1 Answer 1

4

@DateTimeFormat is meant to be used with model attributes (command objects), not JSON bodies.

From your question,

In the json for the date i have

it seems you are submitting the date as part of the request body, as JSON.

There is an entirely different process that handles JSON deserialization. Typically, that is a MappingJackson2HttpMessageConverter assuming you have Jackson on the classpath.

For this to work with Jackson, you'll need a Module that supports Java 8 types. There is this one and this one. These know how to deserialize JSON content into Java 8 date-time types.

You can use @JsonFormat to provide a date format.

Sign up to request clarification or add additional context in comments.

2 Comments

DateTimeFormat can be used on PathVariable, RequestBody... For the Java 8 type, i added Jsr310JpaConverters.class that the class who add the support in spring. Date is in a DTO.
@roberttrudel For @PathVariable sure. For @RequestBody, it depends on the underlying HttpMessageConverter. Jackson knows nothing about @DateTimeFormat. Your Jpa converter is just that, a JPA converter. There's nothing here that involves JPA.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.