Can not parse Date field with jackon's @JsonFormat (use ebean-jackson)

3,490 views
Skip to first unread message

Scott Fan

unread,
Feb 2, 2018, 3:13:58 AM2/2/18
to eb...@googlegroups.com

@Column(name = "BEGINTIME")

@Temporal(TemporalType.TIMESTAMP)

@JsonFormat(shape = JsonFormat.Shape.STRING, timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")

private Date beginTime;  // java.util.Date


In Customer.java

   @Test

   public void test() throws IOException {

       ObjectMapper mapper = new ObjectMapper();

       JsonContext jsonContext = Ebean.json();

       mapper.registerModule(new JacksonEbeanModule(jsonContext));

       String jsonWithUnknown = "{\"id\":42,\"unknownProp\":\"foo\",\"name\":\"rob\",\"version\":1, \"beginTime\":\"2018-02-01 11:11:22\"}";

       Customer customer3 = jsonContext.toBean(Customer.class, jsonWithUnknown);

       // DateTime

       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

       String dateStr = sdf.format(customer3.getBeginTime());

       assertEquals(dateStr, "2018-02-01 11:11:22");

   }


In DateTimeTest.java



But I got an error:

java.lang.RuntimeException: Error parsing Datetime[2018-02-01 11:11:22]

        at io.ebeaninternal.server.type.DateTimeJsonParser.parse(DateTimeJsonParser.java:50)

        at io.ebeaninternal.server.type.ScalarTypeBaseDateTime.jsonRead(ScalarTypeBaseDateTime.java:108)

        at io.ebeaninternal.server.deploy.BeanProperty.jsonRead(BeanProperty.java:1439)

        at io.ebeaninternal.server.deploy.BeanDescriptorJsonHelp.jsonReadProperties(BeanDescriptorJsonHelp.java:142)

        at io.ebeaninternal.server.deploy.BeanDescriptorJsonHelp.jsonReadObject(BeanDescriptorJsonHelp.java:122)

        at io.ebeaninternal.server.deploy.BeanDescriptorJsonHelp.jsonRead(BeanDescriptorJsonHelp.java:91)

        at io.ebeaninternal.server.deploy.BeanDescriptor.jsonRead(BeanDescriptor.java:3166)

        at io.ebeaninternal.server.text.json.DJsonContext.toBean(DJsonContext.java:124)

        at io.ebeaninternal.server.text.json.DJsonContext.toBean(DJsonContext.java:115)

        at io.ebeaninternal.server.text.json.DJsonContext.toBean(DJsonContext.java:105)

        at io.ebeaninternal.server.text.json.DJsonContext.toBean(DJsonContext.java:95)

        at io.ebean.jackson.DateTimeTest.test(DateTimeTest.java:29)

Caused by: java.text.ParseException: Unparseable date: "2018-02-01 11:11:22"

        at java.text.DateFormat.parse(DateFormat.java:366)

        at io.ebeaninternal.server.type.DateTimeJsonParser.parse(DateTimeJsonParser.java:47)

        ... 45 more



And I change the ebean version to 11.6.1 in pom.xml.



Thanks


Rob Bygrave

unread,
Feb 2, 2018, 6:00:16 AM2/2/18
to ebean@googlegroups

So the issue with this part:


@JsonFormat(shape = JsonFormat.Shape.STRING, timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")

private Date beginTime;


Is that Ebean is not reading ( or honoring ) Jackson's @JsonFormat annotation on that property.  More specifically:
- Ebean internally (largely) uses Jackson core (and not Jackson object mapper)
- Ebean supports setting the format for date time types ... but this is effectively for ALL properties for those types (not specific properties)

So Ebean's JSON support isn't designed to handle this case where there is a specific JSON format on a specific date time property (that is not consistent with other date time properties).




So these parts with ObjectMapper:

ObjectMapper mapper = new ObjectMapper();

mapper.registerModule(new JacksonEbeanModule(jsonContext));


... they are not doing anything at all. 
The jsonContext.toBean(...) ... under the hood is using Jackson Core, but not the ObjectMapper.


Customer customer3 = jsonContext.toBean(Customer.class, jsonWithUnknown);




Cheers, Rob.


On 2 February 2018 at 21:13, Scott Fan <fandayr...@gmail.com> wrote:

@Column(name = "BEGINTIME")


@Temporal(TemporalType.TIMESTAMP)


@JsonFormat(shape = JsonFormat.Shape.STRING, timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")


private Date beginTime;



--

---
You received this message because you are subscribed to the Google Groups "Ebean ORM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ebean+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Scott Fan

unread,
Feb 2, 2018, 10:13:15 PM2/2/18
to Ebean ORM

Because there was another error since I have used ebean-11.6.1, so I try to use ebean-jackson to resolve the error (but I don't know whether or not it was the solution) . 
Here is the error:

com.fasterxml.jackson.databind.JsonMappingException: io.ebean.bean.EntityBeanIntercept.preSetter(ZILjava/lang/Object;Ljava/lang/Object;)Ljava/beans/PropertyChangeEvent; (through reference chain: org.example.domain.Customer["id"])
at com.fasterxml.jackson.databind.deser.SettableBeanProperty._throwAsIOE(SettableBeanProperty.java:564)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty._throwAsIOE(SettableBeanProperty.java:548)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:99)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:258)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:125)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3736)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2726)
at io.ebean.jackson.DateTimeTest.test(DateTimeTest.java:28)
Caused by: java.lang.NoSuchMethodError: io.ebean.bean.EntityBeanIntercept.preSetter(ZILjava/lang/Object;Ljava/lang/Object;)Ljava/beans/PropertyChangeEvent;
at org.example.domain.Customer._ebean_set_id(Customer.java:1)
at org.example.domain.Customer.setId(Customer.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:97)
... 39 more



Rob Bygrave

unread,
Feb 3, 2018, 3:46:13 PM2/3/18
to ebean@googlegroups
That error indicates that you need to update the enhancement agent. 

So that is ebean-agent , maven/Gradle plugin version or maven tile.

Scott Fan

unread,
Feb 5, 2018, 12:26:32 AM2/5/18
to Ebean ORM
Thanks Rob, it works. For maven I upgrade the tile to 5.1 as: 

<tile>io.ebean.tile:enhancement:5.1</tile>


在 2018年2月4日星期日 UTC+8上午4:46:13,Rob Bygrave写道:
To unsubscribe from this group and stop receiving emails from it, send an email to ebean+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages