Home> Java> javaTutorial> body text

Is JSON Schema supported using Jackson in Java?

PHPz
Release: 2023-08-20 18:01:14
forward
1499 people have browsed it

在Java中使用Jackson支持JSON Schema吗?

#JSON Schema is a specification based on JSON format, used to define the structure of JSON data. The JsonSchema class can provide a contract for the JSON data required by a given application and guide how to interact with it. JsonSchema can define validation, documentation, hyperlink navigation and interactive control of JSON data. We can use the generateSchema() method of JsonSchemaGenerator to generate JSON schema. This class encapsulates the JSON schema generation function.

Syntax

public JsonSchema generateSchema(Class type) throws com.fasterxml.jackson.databind.JsonMappingException
Copy after login

Example

import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.module.jsonSchema.JsonSchema; import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator; import java.util.List; public class JSONSchemaTest { public static void main(String[] args) throws JsonProcessingException { ObjectMapper jacksonObjectMapper = new ObjectMapper(); JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(jacksonObjectMapper); JsonSchema schema = schemaGen.generateSchema(Person.class); String schemaString = jacksonObjectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema); System.out.println(schemaString); } } // Person class class Person { private String name; private int age; private List courses; private Address address; public String getName() { return name; } public int getAge(){ return age; } public List getCourse() { return courses; } public Address getAddress() { return address; } } // Address class class Address { private String firstLine; private String secondLine; private String thirdLine; public String getFirstLine() { return firstLine; } public String getSecondLine() { return secondLine; } public String getThirdLine() { return thirdLine; } }
Copy after login

Output

{ "type" : "object", "id" : "urn:jsonschema:Person", "properties" : { "name" : { "type" : "string" }, "age" : { "type" : "integer" }, "address" : { "type" : "object", "id" : "urn:jsonschema:Address", "properties" : { "firstLine" : { "type" : "string" }, "secondLine" : { "type" : "string" }, "thirdLine" : { "type" : "string" } } }, "course" : { "type" : "array", "items" : { "type" : "string" } } } }
Copy after login

The above is the detailed content of Is JSON Schema supported using Jackson in Java?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!