json schema to pojo generator - java

what is the best json to pojo generator which have oneof/allof/anyof features . We are currently using a custom one which doesn't support the latest additions in the json. I have tried some of them that shows in the google search but didn't work.

I use https://github.com/java-json-tools/json-schema-validator for schema validation and jackson for pojo-generator.
However, I did not find any support for allOf/AnyOf/oneOf explicitly in jackson. But jackson has a rich set of annotation and it can be built using those.
you can refer a discussion https://github.com/joelittlejohn/jsonschema2pojo/issues/392 to see if something helpful is there for you.

Related

Auto-generating frontend and backend validation for Angular/Java

I hope my question is not too broad.
Our application has an Angular frontend and a Java backend and
we need both frontend and backened validation. At the moment, we manually code
validators in Typescript and Java that basically do the same thing.
Since that is not only tedious but also error-prone, we would like to define once (maybe in a JSON file)
which input fields have which constraints and ideally use a piece of software that auto-generates
Java and Typescript code based on that definition.
My question is: are there any open-source/commercial software solutions that take care of this?
It seems like a common enough problem, but I haven't come across
a proper solution yet. JSON schema seemed promising and since we're using swagger
anyway, I'd hoped that swagger-codegen would automatically take the supported JSON-schema keywords as constraints into account,
but I haven't figured out how it works. When I generate Angular code, constraints such as 'minimum' and 'maximum' are completely ignored,
and when I generate Java SpringBoot code, they only show up as annotations.
This is exactly what JSON Schema is designed for. Open API (swagger) modifies JSON Schema for it's own purposes, which doesn't include validation.
JSON Schema has validator implementations in most major languages allowing you to use the same schema to do the same validation on the front end and the back end. http://json-schema.org/implementations.html
Unfortunately, you likely won't be able to use the same schemas you use for your Open API service definition. I'm not aware of any validator implementations that support Open API's flavor of JSON Schema. I understand that Open API is trying to close the gap between their definition and JSON Schema, but a gap still exists.

ODATA Support for JSON data using java and mysql

We have requirement to create ODATA supported Rest API. We will have to use Java and Mysql for this purpose. Data will be in JSON format which is dynamically changing and not possible to define the exact fields.
I'm looking at libraries like Apache Olingo for this. Documentation doesn't have that much of information about JSON support. Any thoughts on this is appreciated.
Apache Olingo is supporting JSON. It has a serializer for mapping from JSON - org.apache.olingo.server.core.serializer.json.ODataJsonSerializer.
But I'm not sure what do you mean by dynamically changing and not possible to define the exact fields. Do you want to use Open Types from OData? Unfortunately, I have no experience with them in Apacha Olingo.

Is there a json schema for the SurveyMonkey API?

Using the SurveyMonkey API, I'm writing some Java code to do survey analysis. The survey monkey API returns JSON data. I would like to generate some Java classes so I get some type safety and conciseness while I'm manipulating the data. I've had no luck finding a json schema for the Survey Monkey API.
So, I'm looking into tools that generate a schema from json directly, but obviously that will be less desirable than getting it from the canonical source.
The question:
Can anyone recommend a tool-chain that will take me from a set of json examples to a set of java classes that can be used to read an manipulate that json. This might include the intermediate step of generating a schema, but the end-game I'm after is the classes.
If anyone knows of a schema for the API, though, that would be even better.
Why don't you go with polljoy, it's Opensource and I have already integreat it on one of my site and it's working perfectly, you can find it out one below menioned link.
Polljoy

Felxibility of data import/export feature using XML-binding

I am going to develop a database import/export feature in a Java EE application.
I plan to use XML-binding solution to convert between Java object and XML file which is import/export file.
Import feature: unmarshal the XML file to Java object in memory representation, then use JDBC to update the database.
Export feature: inverse the import process. retrieve the database to Java object and marshal the object to XML.
I think it can work fine, but it's not flexible enough. Because the XSD of XML is pre-defined, it's impossible to change XML schema and Java object definition at runtime. Say it's dynamic binding. Even I want the feature supports other file formats (You could forget it, if the format is too far at this stage).
What is your advice about the feature? Thanks!
I don't know whether this is correct answer, but if in any case it is helpful:
I have used Spring, Hibernate, JAXB where you can annotate your database entity class and its element with jaxb annotation and you are not required to write any xml schema files. In spring you can use jaxb Marshaller.
I think it should be possible in pure jaxb also, so u can look into jaxb annotation.
I think it can work fine, but it's not
flexible enough. Because the XSD of
XML is pre-defined, it's impossible to
change XML schema and Java object
definition at runtime
If you think it isn't flexible enough, go with a data interchange format which relieves you from all these fixed schema definitions (I know even JSON has a schema specification but you get the point). Is using JSON acceptable?
I would go as far to argue that if "importing to database" and "exporting from database" is the only requirement, you need not even create Java objects for this. Simply pass in a JSON string which contains the schema which would then be processed by a JSON processor which interfaces directly with your DAO layer. Similarly with the data read from the database. The downside is that "date" support in JSON is spotty at best.
Come to think of it, it need not be JSON. You can take a look at other data serialization formats like Apache Avro. But then again, if XML is your requirement which can't be changed, you can get around the "flexibility limitation" by not using a schema at all.
After all, XML is like violence. If it doesn't solve your problem, you're not using enough of it. :-)

What is best practice in converting XML to Java object?

I need to convert XML data to Java objects. What would be best practice to convert this XML data to object?
Idea is to fetch data via a web service (it doesn't use WSDL, just HTTP GET queries, so I cannot use any framework) and answers are in XML. What would be best practice to handle this situation?
JAXB is a standard API for doing this: http://java.sun.com/developer/technicalArticles/WebServices/jaxb/
Have a look at XStream. It might not be the quickest, but it is one of the most user friendly and straightforward converters in Java, especially if your model is not complex.
For a JMS project we were marshalling and unmarshalling (going from java to xml and xml to java) XML embedded in TextMessages (string property). We tried JAXB, Jibx, and XMLBeans. We found that XMLBeans worked best for us. Fast, easily configurable, good documentation, and easy Maven integration.
I have used and will continue to use JDOM -> www.jdom.org
Another option is a Sax Parser. It is procedural - i.e. a visitor pattern - but if the xml is fairly lightweight, (and even medium weight) I have found it to be very useful for this.
JAXB API which comes in Java(In built).
I have used JIBX in MQ module. It works very well. Ant config is simple. Used Xsd2Jibx converter to generate the binding files and Java beans from XML schema. Marshalling and un-marshalling allow to specify character-set parameter. It was useful in my project to handle custom character-set. But I found an issue in the binding compiler. If the Java bean has lengthier path name, it generates class file with lengthier file name which will cause issue in Windows XP(it has a maximum file length limit).
I haven't used other APIs. So I am not trying to compare with others. If you decided to use JIBX, I hope this will be helpful.
More details, please refer JIBX website
I've used XStream as well, it is easy to use and customizable. You can add your own custom converters and that was very handy for me...
So surprised more people have not mentioned Jibx. Amazing lib and i think a lot simpler to use than Jaxb. Performance is also fab!
For this you can also consider apache's bitwixt and simple framework for xml

Categories