1

My pojo class is as given below

public class Employee {

private int empId;
    
    @NotEmpty
    private String empName;

    
    @DateTimeFormat(pattern = "dd/MM/yyyy")
    private Date empDoj;

    public int getEmpId() {
        return empId;
    }

    public void setEmpId(int empId) {
        this.empId = empId;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public Date getEmpDoj() {
        return empDoj;
    }

    public void setEmpDoj(Date empDoj) {
        this.empDoj = empDoj;
    }

I am accepting the request in controller as shown below

@RequestMapping(value = "/employee",method = RequestMethod.POST)
    public Employee employee(@RequestBody @Valid Employee employee
            ) { 
        return employee ;
    }

when I am sending the below JSON request from postman it is giving me an error

{
         "empName":"Anubhav",
         "empDoj":"10/10/2019"
      }

JSON parse error: Cannot deserialize value of type java.util.Date from String "10/10/2019": not a valid representation (error: Failed to parse Date value '10/10/2019': Cannot parse date "10/10/2019": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSX", "yyyy-MM-dd'T'HH:mm:ss.SSS", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd")); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type java.util.Date from String "10/10/2019": not a valid representation (error: Failed to parse Date value '10/10/2019': Cannot parse date "10/10/2019": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSX", "yyyy-MM-dd'T'HH:mm:ss.SSS", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd")) at [Source: (PushbackInputStream); l

when I am doing the same for the method handler

@RequestMapping(value = "/dateformat",method = RequestMethod.POST)
    public Date dateex(@DateTimeFormat(pattern = "dd-MM-yyyy") @RequestParam(value = "date")Date date) {        

}

It is working fine. Converting string specified in given pattern to Date object then why not with Java class member. In the docs also it says it can be used either at method parameter or field formatted as given date type of field

unknown
  • 412
  • 7
  • 20
  • Does this answer your question? [Date format Mapping to JSON Jackson](https://stackoverflow.com/questions/12463049/date-format-mapping-to-json-jackson) – DwB May 14 '21 at 17:21
  • @DwB can you please comment on the edits I made in the original question? – unknown May 14 '21 at 17:42
  • the DateTimeFormat is a Spring Boot annotation, JsonFormat is a Jackson annotation. It appears that Spring Boot does not search "inside" the method parameter class for the dateTimeFormat annotation, it only applies it to method parameters. – DwB May 14 '21 at 19:32

2 Answers2

3

Since you are sending in JSON you need to add the @JsonFormat(pattern="dd/MM/yyyy") annotation to empDoj. If all your dates will be this format you can set spring.jackson.date-format=dd/MM/yyyy in your application.properties file.

Lee Greiner
  • 649
  • 3
  • 6
  • Lee Many thanks for the answer. Those it is working but why do we need to use @JSONFormat.According to the docs it is a General-purpose annotation used for configuring details of how values of properties are to be serialized. Properties concerning here are mostly date related serialization. In this particular case I just want my JSON string value to take the pattern format and convert it into given Date object. Why JsonFormat is required here? – unknown May 14 '21 at 17:11
  • Since Jackson is serializing/deserializing your JSON you need to use the Jackson annotations. If Spring was serializing/deserializing the request body the DateTimeFormat would work. DateTimeFormat would also work for request parameters since Spring serializes/deserializes those. – Lee Greiner May 14 '21 at 17:30
0

Some details about my vote to close.

You are attempting to use a SpringBoot parameter annotation to specify JSON formatting. I believe you need to use the JsonFormat annotation (as noted in the buræquete answer to this question)

DwB
  • 37,124
  • 11
  • 56
  • 82