1

I'm working with Spring-boot to expose REST Api and I need to check two differents patterns of date for my input DTO. For now, my code checks the format

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")

but I need to give to client possibility to send date also with the format "yyyy-MM.
One of solutions is tu use annotation @Pattern rather than @JsonFormat annotation but the neede regex to validate the two dates is too long, I want to know if there is a better solution.

2
  • What is the type of the field? java.util.Date? Commented Jul 20, 2018 at 11:05
  • Yes. The proposed solution in the first answer works with some modification. I'm using StdDeserializer and it works but I dont know what's better Commented Jul 23, 2018 at 14:27

2 Answers 2

1

try something like below,

public class CustomDateSerializer extends StdSerializer<Date> {

    private SimpleDateFormat formatter
            = new SimpleDateFormat("dd-MM-yyyy");
    private SimpleDateFormat formatter1
            = new SimpleDateFormat("dd-MM");

    public CustomDateSerializer() {
        this(null);
    }

    public CustomDateSerializer(Class t) {
        super(t);
    }

    @Override
    public void serialize (Date value, JsonGenerator gen, SerializerProvider arg2)
            throws IOException, JsonProcessingException {
        Date date;
        try {
            //if not valid, it will throw ParseException
            date = formatter.parse(value.toString());
        } catch (ParseException e) {
            try {
                date = formatter1.parse(value.toString());
            }catch(ParseException ex) {
                ex.printStackTrace();                    
            }
        }
        gen.writeString(formatter.format(date));
    }
}

and @JsonSerialize(using = CustomDateSerializer.class) public Date eventDate;

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

Comments

1

You have many ways of achieving this. Two of which are

1 JsonDeserialize

Register custom deserializer

public class DateDeserializer extends JsonDeserializer<LocalDate> {
    @Override
    public LocalDatedeserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    // .................
    // try multiple formats in a loop here if, success return date if not throw exception
    return something;
   }
}
  @JsonDeserialize(using = DateDeserializer .class)
  private LocalDate date;

2 Use string setter and parse date manually

private String someDate;
public LocalDate setSomeDate(String someDate){
  // loop through patterns here and try to parse date, then assign parsed date to 
  // another value you are going to use internally 
  // I usually keep string value as well 
}

1 Comment

I got Failed to evaluate Jackson deserialization for type, It works with StdDeserializer<Date>. Do you know what can be the cause of this error and why to use JsonDeserializer rather than StdDeserializer ?

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.