How to exclude some fields from json schema - java

I am trying to generate POJO classes from JSON schema using jsonschema2pojo in java spring boot. I am using the maven plugin mode of it. I like to exclude some fields which are in the schema, but it seems this feature is not supported in the jsonschema2pojo library as per https://github.com/joelittlejohn/jsonschema2pojo/issues/966.
Any suggestion on how can I achieve this? or any other library which supports this feature?

Related

Json schema validation in Spring REST APIs

I’m building a REST API using Spring Boot and [jackson-module-jsonSchema] (https://github.com/FasterXML/jackson-module-jsonSchema) for JSON schema generation.
I’m looking the best way to validate the request JSON payload arriving to my APIs endpoints (Spring controllers) against the defined JSON schema defined for the exposed resource, validation includes check required fields , format , min and max values, etc.. everything we can validate against the schema.
Seems jackson json schema module is useful for schema generation but not for validation, am I right?
Any suggestion on how to achieve what I’m trying to do?
If you take a look at JSON schema site, there are only two libraries for validation in Java.
The one that Jorge Campos suggested is mature, but looking for new maintainer: https://github.com/fge/json-schema-validator
Second one is relatively new: http://github.com/everit-org/json-schema
I was recently in situation where I had to choose one or the other and I picked first option. It is being used also by Rest Assured library under the hood.
You can also look at Rest Assured Json Schema validator
https://www.baeldung.com/rest-assured-json-schema

Generate XSD schemas from an existing database or JPA entities

I have been using hyperjaxb tool to generate JPA entities and database scripts from XSD schemas. Now I want basically the inverse operation. How can I generate XSD schemas based on JPA anotated entities or database? I am using Maven, so any maven solution would be preferable.
If you're using IntelliJ IDEA, see the following link:
https://www.jetbrains.com/idea/help/generate-xml-schema-from-java-using-jaxb-dialog.html
This maven plugin ( jaxb2-maven-plugin ) might be useful for you.
This post has more information about its usage and the given example uses POJO's to do the transformation
Generate XSD from Java class

Pojo to json schema v4 and vice versa via maven

I am trying to come up with a set of documents for one of our projects where there is a requirement to convert Java POJOs to JSON Schema 4 and sometimes JSON schema 4 back to POJOs. I couldn't find a maven plug-in that does both of it.
I was able to find https://github.com/wodzuu/JSONschema4-mapper for JSON Schema generation and https://github.com/joelittlejohn/jsonschema2pojo for PoJo generation.
I am sure it is a very common use case, so just writing to check what you folks used to get past this? Any inputs
I have been using jsonschema2pojo for a long time and am very supportive of this nice library and its tooling.
To generate JsonSchema out of POJO:
First, it should be very doable, for a POJO class can be converted to JSON using JSON serializing tools (jackson etc...). Just convert a sample of your POJO to JSON and jsonschema2pojo can generate a JSONSchema out of if. Other than that, integrating it to maven must be as easy as just executing a command in maven.
Second, you can submit a feature request to the developer. The community is very active..
Hope it helps.

Is it possible to auto generate annotated-mapped POJO classes using techniques like Hibernate, JIBX etc?

Hibernate can auto-generate schemas from properly annotated POJO classes. And I also know that JIBX can create a data model (set of classes) out of properly structured XML schemas. Is there a way to automaticaly generate annotated-mapped classes from an XML schema? Or is it just possible to run a tool on a set of POJO classes, and expect it to create meaningful annotations on the specified classes? So later on we can create database schemas using these classes. To annotate every class that JIBX produces takes actually more work than manually designing the database schema according to the xml schema.
The Hyperjaxb project will generate JAXB classes from an XML schema that contain JPA annotations that could be used to create a database schema.
http://java.net/projects/hyperjaxb
Nice question! We had the same problem and we ended up developing the POJO generator with Freemarker.
By the way, the requirements to these POJOs may strongly vary, so, if such tool exists, it must have quite bloated configuration.

A tool for transforming POJO to XML and JSON, based on xml-mapping is needed

I need an open-source tool which can convert POJOs to XML and JSON strings.
Jersey (and probably other JAX-RS implementations) would fit these requirements if mappings could be configured through xml-files but not through annotations.
Is there anything suitable?
POJO to XML
JAXB is the Java standard (JSR-222) for converting Java objects to/from XML (I am a member of the JAXB expert group):
http://bdoughan.blogspot.com/2010/07/jaxb-xml-binding-standard.html
POJO to JSON
People have been using JAXB with Jettison to produce JSON. This is how Jersey (JAX-RS reference implementation) converts POJOs to JSON by default.
http://bdoughan.blogspot.com/2011/04/jaxb-and-json-via-jettison.html
XML Configuration
If you require the ability to configure the metadata via XML, then you can use EclipseLink JAXB (MOXy), I'm the MOXy tech lead:
http://bdoughan.blogspot.com/2010/12/extending-jaxb-representing-annotations.html
There are lots of suitable JSON libraries; aside from GSON that was mentioned, Jackson is an obvious choice. Typically you don't need any annotations; but even if you neded, Jackson can use so-called mix-in annotations which means associating configuration, not modifying actual POJOs. Finally, Jackson supports JAXB annotations if you must use them (for example, when using JAXB for producing XML).
And as an added bonus, there is Jackson extension module, jackson-xml-databind, which can data bind POJOs to and from XML, so you can just use one library for both.
Have you tried Castor?
http://www.castor.org/
Supports XML mapping -
http://www.castor.org/xml-mapping.html
From XML, you could use json-lib if you need JSON.
http://answers.oreilly.com/topic/278-how-to-convert-xml-to-json-in-java/
Check out GSON for converting POJOs to JSON:
http://code.google.com/p/google-gson/
We've been using for a couple of years now. Very excellent library.
For POJOs to XML, Jersey uses JAXB or you could roll your own serializer.

Categories

Resources