Spring Redis JSON serializer embeds referenced field - java

I have a class which has many simple attributes (type is int,String,...). It also has an attribute which is an instance of another class of mine. Now I want to send the object via a Redis pub/sub channel. To do this I serialize it with the GenericJackson2JsonRedisSerializer. As both classes have their own repository I don't want to embed the object every time but instead only send the ID. I thought that this should be possible by adding the org.springframework.data.annotation.Reference annotation to the field.
Unfortunately this didn't work, instead it just embeds the object. Is there anything I did wrong? What do I have to do to just get the object's ID in the serialized version?
Thank you for your help!

Alright after way too much research for such a basic thing I finally figured out how to do it.
Basically I needed to add the com.fasterxml.jackson.annotation.JsonIdentityInfo annotation to the class or field. As already stated in the question this produced the same result, first time completely included, afterwards only referenced.
No documentation mentioned how to always have the id, I had to look into the code: there's another annotation com.fasterxml.jackson.annotation.JsonIdentityReference which has a boolean property called alwaysAsId. If you set this one to true it always adds the id instead of the object.
To get the deserializing working one needs to specify a custom resolver for the ids. As I'm using Spring it has been quite easy to get access to my repository.

Did you use Spring Data Redis ? http://projects.spring.io/spring-data-redis/
Check http://docs.spring.io/spring-data/redis/docs/current/reference/html/#redis.repositories.references
If you are not using Spring Data Redis I think you should have to implement your own reference when serialize/deserialize your data.
You can implements your own Serializer with
YourObjectSerializer extends Jackson2JsonRedisSerializer

Related

Cassandra custom type mapping?

I'd like to be able to do a custom serialization/deserialization of raw types with cassandra without needing to make UDT. Basically I want to store a string column, but have it automatically deserialized into a String wrapper value I have. Is there any way to hook into the cassandra mapping code? I looked through it and I didn't see a particular opening where to put in a custom mapper.
Currently, it is not possible to plug in a custom mapping mechanism into the Java driver, but JAVA-721 is scheduled for 2.1.7 and will probably bring you the ability you are looking for.

Spring MVC: Bind Input field to multiple properties

I am trying to get Spring MVC to bind the value from one input field to multiple properties.
My use-case is a forum, that contains a form to create a new topic. That dialog actually create a topic and a post, which both have the same title, so only one input field exists for the title. If I copy the value myself in the controller method, using the "#Validated" annotation in the signature won't work, since the validator is called before I can copy the value, and so the title would still be missing in one field. Of course I can call the validator myself after copying the value, but if at all possible I would prefer if Spring would bind that value itself, just to get a clean solution (and to learn if and how it is possible).
Best regards,
Christian

Spring Integration Custom Poller for Different Events

I need to poll a folder for changes i.e. files added, modified and deleted.
If I want to distinguish between the different types of events listed above would I need to implement a custom poller i.e. implement AbstractPoller. I have already implemented a poller that does this for a different project but would like to us spring integration and batch as I need to use other functionality.
What is the best way of doing this?
Thanks
Wouldn't you mind to share your code? BTW you always can utilize the custom code with <int:inbound-channel-adapter> as a ref and method, where an underlying POJO will return some object which will become as payload of message.
As you know the <int:inbound-channel-adapter> should be configured with <poller> how often you want to call that undelying POJO.

Best workaround for Spring MVC Json parsing limitations

I have a project which uses Spring, Hibernate, and has controllers which return JSONs. Naturally, my models contain lists and such to use JPA annotations to define the hibernate relationships, so, for example, I have Users, which contain a set of Challenges they own, and likewise Challenge contains a User who owns it.
Unfortunately I seem to be having a lot of issues with collections embedded in my JSONs.
For example, with that set up (a User owns challenges and a challenge has a owner) I can return a Challenge just fine. I can return a User just fine. But when I try to return a list of challenges, everything blows up! I receive the following error from my Jmeter test:
Error 500 Server Error
I believe this means that the Jackson json parser had an issue setting the json. I believe this, because if I use #JsonIgnoreProperties({"challengesOwned"}) then I can return the list of challenges just fine, since each individual challenge object no longer has a list embedded inside it.
This seems very strange to me. Can Jackson really not map simple embedded lists within JSONs? I've also got a huge problem because I have a Map which uses a User as its key ... and it seems it's not even possible to define a JSON map's key as an embedded object at all!
Does anyone have a suggestion for my issue? Do I have to manually define some Json mappings? Is there a simple solution I just don't know about?
EDIT:
While what j0ntech says does seem to have been true, it turns out that was not the whole story. It seems that when Spring used Jackson to serialize one of my hibernate entities into it's JSON version, hibernate was trying to lazy load one of that entity's properties, but since the entity was outside of its transaction at that point (being "in" the controller), it caused an exception, which just got swallowed up.
So there were actually TWO issues. I figured this out by trying to manually use Jackson to serialize the object I was returning before actually returning it. That way I actually got the stack trace for the other issue.
You probably have a recursive loop (as per DwB's comment): User contains a list of Challenges, which each contain a User, which contains a list of Challenges and so on and so forth. The parser (or your server at large) doesn't like that. You should use the annotations JsonManagedReference and JsonBackReference.
You can read about how to use these annotations here and here. I've used them in some of my own projects and they work very well if correctly implemented.
you could try flexjson (used by Spring Roo) or gson (developed by google)
Parsing GSON with Lists seems to be pretty straight forward
http://rishabhsays.wordpress.com/2011/02/24/parsing-list-of-json-objects-with-gson/

implementing dynamic class with using hibernate annotation

I want to implement a class which its fields could change (add new fields) through time. The problem is that I want to give my client this ability to do this himself by just completing a form of what field and which type he wants and then the change will be done automatically!
Does anyone have any idea how can I implement this class and make this change in my database using hibernate annotation?
You should use a map of name-value pairs. Maps are supported by Hibernate per se, and their contents are dynamic.
You definitely can't make that change using Hibernate Annotations, since annotations can only be attached to a class at compile time. However if you want to change the structure of your class at runtime (add fields, methods, etc.) you will have to recompile the class and recreate the database mapping Hibernate uses to access your database.

Categories