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
Related
In client-server applications with spring-boot and angular. Most resources I find explain how to expose REST endpoint from spring boot and consume it from angular with an http client.
Most of the time, communicating in JSON is preconized, maintaining DTOs (DataTransfertObject) in both angular and spring boot side.
I wonder if people with fullstack experience knows some alternative avoiding maintaining DTOs in both front and backend, maybe sharing models between the two ends of the application?
Swagger would be a good tool to use here.
You can take a code-first approach which will generate a swagger spec from your Java controllers & TOs or a spec-first approach which will generate your Java controllers & TOs from a swagger spec.
Either way, you can then use the swagger spec to generate a set of TypeScript interfaces for the client side.
As Pace said Swagger would be a great feature that you can use. In this case plus having great documentation for the API endpoints you can sync object models between frontend and backend. You have to just use the .json or .yaml file of Swagger to generate the services and object models on the frontend side by using ng-swagger-gen.
Then put the command for generating services and object model in package.json when you wanna build or run your application for instance:
...
"scripts": {
...
"start": "ng-swagger-gen && ng serve",
"build": "ng-swagger-gen && ng build -prod"
...
},
...
So after running one of these commands you will have updated object models and if an object property name or type changed, add/remove object property you will get the error and you have to fix it first then move forward.
Note: Just keep in mind the services and object models will be generated based on the Swagger file so it always should be updated.
PS: I was working on a project that we even every code on the backend side was generated based on the Swagger file ;) so they just change the Swagger file and that's it.
This is a difficult topic, since we are dealing with two different technology stacks. The only way I see is to generate those objects from a common data model.
Sounds good, doesn't work. It is not just about maintaining dto's.
Lets say api changes String to List. It is not enough to update your typescript dto from string to string[]. There is logic behind string manipulation that now needs to handle list of strings. Personally i do not find that troublesome to maintain dto's on both sides. Small tradeoff for flexibility and cleaner code (you will have different methods in different dto's)
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.
I have been reading the very interesting section of the Jersey documentation regarding entity filtering, particularly the section regarding role-based filtering.
Unfortunately it looks like the feature is only available when using MOXy and support for Jackson is in the backlog for now.
So I was thinking about giving it a shot and writing the required classes to implement Jackson-based security annotation role-based filtering. This doesn't seem like an easy task. I am guessing I would have to register my own ObjectMapperProvider for Jackson that returns a wrapper for the Jackson ObjectMapper and via "some magic" do the filtering in the wrapper.
Is this a good way to go? Or is there a simpler way?
Upgrade to Jersey 2.16 that has entity filtering support for JSON via Jackson.
reference: http://blog.dejavu.sk/2015/02/04/jerseys-entity-filtering-meets-jackson/
and here is an example of role-based entity filtering: https://github.com/jersey/jersey/tree/master/examples/entity-filtering-security
I'm trying to implement the HAL standard for JSON in a JAX-RS service. My project consists of Users containing many Projects containing many Nodes containing a variety of data and pointers to other Nodes.
So when an endpoint is hit, I'd like to embed objects one level deep, and link after that:
/user has user data and "_embedded" projects, but those projects only
contain "_links" to nodes (and self)
/project/1234 has "_embedded" nodes, but those nodes "_links" to further data.
And so on.
The Jackson JSONFilters look close, but I'm not quite grasping it. Especially relevant is that sometimes a property will be mapped in a collection of "_embedded" and sometimes in "_links" using different techniques.
Any one ever try something like this?
There is HalBuilder, but it seems like it requires hand serialization, which I'd like to avoid. Then again, Jackson seems to be almost as much code as hand serializing.
You'll need to implement your custom Jackson Serializer ( http://jackson.codehaus.org/1.7.9/javadoc/org/codehaus/jackson/map/JsonSerializer.html )
Take a look at Spring Hateoas project. They have implemented a jackson extension HalJacksonModule ( https://github.com/SpringSource/spring-hateoas/commit/61e73107c1213556c025dc8f68a8784daf089796
) to enable HAL serialization to Jackson. I think you can use it or adapt it to your needs.
Additionally, the project "Spring Data Rest" (http://www.springsource.org/spring-data/rest) provides a way to export your JPA model to REST (with hateoas) using Spring Hateoas. You may look at the code to get inspiration or simply use the framework in your code. (Remember to register the HalJacksonModule into the ObjectMapper).
I have found that the RestExpress library is pretty fantastic and it includes support for HAL. The author did all of the work of build the serialization mechanisms and links building based on one simple configuration.
https://github.com/RestExpress/HyperExpress
https://github.com/RestExpress/HyperExpress/tree/master/hal
I'm calling a 3rd party restful webservice that returns a complex JSON object. Is there a way for Spring or any open source tool to auto generate the client side code object.
In Soap, I'm use to doing wsdl2java in cxf but I don't know what the equivalent is in the restful space.
In the end I would like to use the rest template to make the following call:
restTemplate.getForObject("url", generateObject.class)
Answer is yes as long as you have json schema for your input file.
I dont remember at the moment how this framework is called but it is google product.
It auto generates Java class annotated with Json. It is really good. I will look for name.
For the same thing I used a framework called XStream (http://x-stream.github.io/). It's pretty light and will definately help you :)