Deserializing of object with DateTime variable fails

73 views
Skip to first unread message

Michal Stehlik

unread,
Sep 17, 2014, 3:02:04 PM9/17/14
to gen...@googlegroups.com
Hi,

I have an issue with deserializing of object using Genson together with JodaTime. It seems, that properly filled object in client side of application is processed by Genson and put into my method "add" with null values. I don't provide whole project, but classes which (I think) can lead to reproducing problem. I'll really appreciate if somebody can help me.

If you need more information, just ask :)

Btw my setup is: embedded Jetty with Jersey and Genson, JodaTime and Logback for logging.
jodaTimeIssue.zip

Eugen Cepoi

unread,
Sep 18, 2014, 6:24:07 AM9/18/14
to gen...@googlegroups.com
Hi,

I see you use JAXB annotations. Do you expect Genson to use them or not? If yes you must register the bundle JAXBBundle (see http://owlike.github.io/genson/Documentation/Extensions/#jaxb-annotations).

Your problem is that the field is private and the setDateTime returns something (it is not considered as a setter).

So two options:
1) You want genson to use your jaxb annotations then register the bundle and things should work.
2) You don't want genson to use the annotations, then make setDateTime return void.


Cheers,
Eugen


--
You received this message because you are subscribed to the Google Groups "Genson user group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to genson+un...@googlegroups.com.
To post to this group, send email to gen...@googlegroups.com.
Visit this group at http://groups.google.com/group/genson.
To view this discussion on the web visit https://groups.google.com/d/msgid/genson/59dccc25-dcb0-4c86-b15c-d4ca46cacc98%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Michal Stehlik

unread,
Sep 21, 2014, 5:15:54 AM9/21/14
to gen...@googlegroups.com
Hi Eugen,

I tried JAXBBundle and also change setter but non of this works. When I change DateTime to long, then everything woks fine, but I want to use DateTime. Can you please suggest some other solution, or review deserializing of DateTime object, because I think, this is real cause of this issue.

Eugen Cepoi

unread,
Sep 21, 2014, 5:37:05 AM9/21/14
to gen...@googlegroups.com
Hi Michal,

I tried your code (outside jersey just in a plain main).

1) Without the JAXBBundle, it works if setDateTime returns void.
2) With the JAXBBundle it worked with no changes.

Note I didn't use your DateUtils.DATE_TIME_FORMAT_LONG.

First try the same: a main where you just ser/de and check things work, and then check on server side what instance of Genson is injected in com.owlike.genson.ext.jaxrs.GensonJsonConverter, maybe yours is not being picked.



Michal Stehlik

unread,
Sep 22, 2014, 4:32:39 AM9/22/14
to gen...@googlegroups.com
Hi Eugen,

you are right. My Jersey don't use my provider for Genson. But how can I force Jersey to use it (with code only). Can you help me?

Eugen Cepoi

unread,
Sep 22, 2014, 8:28:12 AM9/22/14
to gen...@googlegroups.com
You can register your custom resolver like that:
new ClientConfig().register(Main.GensonCustomResolver.class);

Don't forget to register it also on the server side:
new ResourceConfig().register(GensonCustomResolver.class);

Note that you still need to: change the setter to return void or enable the jaxb bundle in genson.


Michal Stehlik

unread,
Sep 22, 2014, 3:04:28 PM9/22/14
to gen...@googlegroups.com
Setting up client is easy. I have bigger problems properly setting up server part. This is how my code looks like, but I can not see possibility somehow attach ResouceConfig into it:

Server server = new Server(PORT_NUMBER);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
EnvironmentLoaderListener listener = new EnvironmentLoaderListener();
context.callContextInitialized(listener, new ServletContextEvent(new ContextHandler.NoContext())); 
context.addEventListener(listener);
DatabaseContextListener databaseListener = new DatabaseContextListener();
context.addEventListener(databaseListener);
context.setAttribute(DatabaseContextListener.DB_CONNECTION_ATTRIBUTE, databaseListener);
FilterHolder filterHolder = new FilterHolder(); 
filterHolder.setFilter(new ShiroFilter()); 
EnumSet<DispatcherType> types = EnumSet.allOf(DispatcherType.class); 
context.addFilter(filterHolder, "/*", types); 
ServletHolder holder = new ServletHolder(ServletContainer.class);    
holder.setInitParameter(ServerProperties.PROVIDER_PACKAGES, "org.vrabec.datagateway.rest");
        context.addServlet(holder, "/*");
        server.setHandler(context);
        server.start();
        server.join();

Please help me if you can. Thanx.

Eugen Cepoi

unread,
Sep 22, 2014, 4:34:48 PM9/22/14
to gen...@googlegroups.com

Are you using jersey with jetty ? Which version for them ?

Michal Stehlik

unread,
Sep 23, 2014, 1:17:23 AM9/23/14
to gen...@googlegroups.com
jetty-distribution-9.2.3.v20140905 and Jersey 2.12 and Apache Shiro 1.2.3

Eugen Cepoi

unread,
Sep 23, 2014, 8:49:35 AM9/23/14
to gen...@googlegroups.com
To test the integration with jersey I also used jetty (but both are older versions), have a look at the test maybe it could help.

But as you are using the latest versions, the official doc is the best place to start https://jersey.java.net/documentation/latest/deployment.html. It looks like you can do something similar to:

RI baseUri = UriBuilder.fromUri("http://localhost/").port(9998).build();
ResourceConfig config = new ResourceConfig(MyResource.class); // here it becomes easy you can register all your components
Server server = JettyHttpContainerFactory.createServer(baseUri, config);


Michal Stehlik

unread,
Sep 23, 2014, 9:11:28 AM9/23/14
to gen...@googlegroups.com
Hi, I tied this approach 3 days before, but here I see big cons. You can not add filter to this server, or better say, you can not change server.setHandle(), because this is already set. Handler in this method don't provide such functionality like add filter (necessary for shiro). It seems, I have to rise question directly on Jersey forum.

Eugen Cepoi

unread,
Sep 23, 2014, 9:18:28 AM9/23/14
to gen...@googlegroups.com

Try to have a look at the source code of JettyHttpContainerFactory as they probably do there what you need.

Reply all
Reply to author
Forward
0 new messages