2

How do you configure how Jackson is parsing into a Calendar? Is there anyway to set the format?

I am using

@RequestMapping(value = "/assign", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Form5398Obj arriveTrip(@PathVariable String siteId,
                @RequestBody ErrorMsg anError) throws Exception {

        System.out.println(anError.toString());

    }

I noticed I am getting ERROR 400 because the Calendar field in ErrorMsg is not being converted properly. If I remove it from the POST, it works properly.

I noticed from a search that there are some standard forms that worked for me ""yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"

Is there anyway to explicitly tell it to parse a certain way?

1
  • ... even after several attempts i fail to understand what you were trying to say. Your sentences dont make much sense. Sorry. Commented Nov 21, 2014 at 23:32

1 Answer 1

1

Look here in the FAQs for ways to do it for Jackson 2.0.

Example from the FAQ for serialization:

public class DateStuff {
  @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd,HH:00", timezone="CET")
  public Date creationTime;
}

Independent from that, there is the possibility of writing your own Serializer class and annotate the property with @JsonDeSerialize(using MyDeSerializer.class)

public class MyJsonDateDeserializer extends JsonDeserializer<Date>
{
    @Override
    public Date deserialize(JsonParser jsonparser,
            DeserializationContext deserializationcontext) throws IOException, JsonProcessingException {

        SimpleDateFormat f = new SimpleDateFormat("dd.MM.yyyy"); // german date
        String d = jsonparser.getText();
        try {
            return f.parse(d);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

    }

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

1 Comment

using @JsonFormat worked great for me. Thanks! I thought that annotation was only when serializing to JSON.

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.