I am writing a Java EE app to be run within either Glassfish or JBoss, and I've been asked to provide JSON support for our JAX-RS clients.
Of course, simply enabling the JSON media type is simple enough but results in vendor-specific representations of my JAXB annotated models. So I'm looking for a way forward, one that ensures my tests work with both AS servers and that I still leverage JAXB/JAX-RS instead of writing lots of custom code.
I've tried adding a jaxb.properties file to select a specific implementation, but presumably shipping Jackson/MOXy/other JAXB library may conflict with provided libraries?
Any other ideas would be appreciated.
Related
Is it possible to use Jettison with Spring / Spring-Boot instead of default JSON Provider Jackson? I have one such requirement to match Json output with an very old project which used Jettison.
If yes, can i get some pointers/hints please?
It's possible but you're going to be doing a lot of hacking and writing boiler-plate configuration code as Jackson is embedded pretty deep inside Spring.
It might be easier for you to leverage Jackson's functionality to serialize/deserialize your data in the format you need (instead of the format Jackson silently provides)?
Looking for some help to serialize a deep nested java objects to Json. The constraint is that I cannot add any annotations or change the current Java code. Looking for a powerful Json library which has configuration options to convert Java to Json without altering the Java Object themselves. Following would be some of the options that might be required
Specify include/exclude fields/methods. Should be able to specify this at nested levels. A is composed of B and B is composed of C. Should have ability to specify include/exclude at C.
Include/Exclude Objects at nested levels.
Rename fields from java properties while converting to Json (at nested level objects too)
Manager circular dependencies.
Was looking at Jackson and Gson for this requirements. While there are tons of options using annotations to specify serialization configs while writing new Java Pojo's, I am looking at options where I need to specify serialization properties without changing the current Java code. Jackson and Gson do seem to have options for these, but not documented in depth.
Which library is easier to configure for the above requirements? Any other powerful library other than Jackson/Gson? Any pointers to this will be of great help.
Thanks much for your time.
As to Jackson, you might consider using so-called mix-in annotations (see f.ex http://www.studytrails.com/java/json/java-jackson-mix-in-annotation.jsp) which allow you to specify mix-ins to use, without adding them directly in the legacy classes.
This would let you use annotation-based configuration, but leave actual classes untouched.
But given all of your requirements, it may perhaps be better to just use Tree Model of Jackson or GSON (get JsonNode or such), and manually handle conversions to your liking.
You may then be able to convert tree value into POJO; Jackson, for example, has method(s) for doing this (ObjectMapper.treeToValue(), .valueToTree(), .convertValue()) which allow conversions of structurally compatible representations.
The custom BeanSerializerFactory in http://kyrill007.livejournal.com/2577.html is the only custom solution I found to allow directly throwing persistent beans to JSON via Spring 3.0, and it works, it only serializes non initialized (lazy) attributes / collections (this allows me to use the entity Pojo as a DTO, as I initialize only what I want, and what is not initialized, doesn't get serialized to JSON)
But this worked well with Jackson 1.6, and I wanted to upgrade to Jackson 1.8 to solve the issue with Java Generics (hopefully) and now that custom solution is not compiling.
So my questions are
What is the recomended way to auto serialize Entities to JSON without the need of DTOs
Is there an official Hibernate Aware Jackson BeanSerializerFactory besides the above
I'm starting to fear that if it's that hard to find, maybe my practice is not the best one
What is the recommended way to do RESTful Ajax then with Spring 3.0 MVC and JSON?
The problems to solve are
Not serializing lazy attributes / collections automatially (as the custom code above does)
Supporting Java Generics and some kind of a client side object schema / validation
What works on get should work on save, and allow partial objects graphs to be returned safely
Is there Anything? do I have to manually write DTOs for every Entity?, this is so non productive
While SO has lots of experts, you might consider also asking on Jackson users list. Kirill (author of the blog entry) is responsive, and there are other experts there as well.
We're currently using GWT RPC for serialization on a GWT project but we're currently maintaining two sets of objects - the object that we need to convert for the database to retrieve/save and a version of the object that is safe for GWT RPC serialization (no enums/big decimal, etc.).
We're spending a lot of effort writing code that merely converts from one format to the other format. In addition it's pretty painful to make any changes to the data model because it has to be changed in two places.
I was thinking that we could use a combination of Spring 3.0 MVC and Jackson to replace the RPC calls with JSON calls. If we built JavaScript objects for GWT to hold this JSON data, then it would remove the need for any property conversion code. However we'd still have to maintain two sets of objects - one JavaScriptObject for the client side code and the server side representation.
To eliminate this layer, to take a Java object and have it produce a GWT JavaScriptObject with the JSNI getters/setters exposed. Is there a library out there that could do this automatically?
We eventually dropped GWT and went with a Spring MVC/jQuery solution, but I did find the protostuff library which looked like it could do most of what I was looking for.
I'm writing a RIM BlackBerry client app. BlackBerry uses a simplified version of Java (no generics, no annotations, limited collections support, etc.; roughly a Java 1.3 dialect). My client will be speaking JSON to a server. We have a bunch of JAXB-generated POJOs, but they're heavily annotated, and they use various classes that aren't available on this platform (ArrayList, BigDecimal, XMLGregorianCalendar). We also have the XSD used by the JAXB-XJC compiler to generate those source files.
Being the lazy programmer that I am, I'd really rather not manually translate the existing source files to Java 1.3-compatible JSON-marshalling classes. I already tried JAXB 1.0.6 xjc. Unfortunately, it doesn't understand the XSD file well enough to emit proper classes.
Do you know of a tool that will take JAXB 2.0 XSD files and emit Java 1.3 classes? And do you know of a JSON marshalling library that works with old Java?
I think I am doomed because JSON arrived around 2006, and Java 5 was released in late 2004, meaning that people probably wouldn't be writing JSON-parsing code for old versions of Java.
However, it seems that there must be good JSON libraries for J2ME, which is why I'm holding out hope.
For the first part good luck but I really don't think you're going to find a better solution than to modify the code yourself. However, there is a good J2ME JSON library you can find a link to the mirror here.
I ended up using apt (annotation processing tool) to run over the 1.5 sources and emit new 1.3-friendly source. Actually turned out to be a pretty nice solution!
I still haven't figured out an elegant way to do the actual JSON marshalling, but the apt tool can probably help write the rote code that interfaces with a JSON library like the one Jonathan pointed out.