2
votes

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. - specializt

1 Answers

1
votes

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);
        }

    }

}