Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

JSON Schema Support ด้วย Jackson ใน Java

JSON Schema เป็น specification สำหรับ JSON-based format ในการกำหนดโครงสร้างของ JSON data คลาส JsonSchema ให้ contract สำหรับ JSON data ที่จำเป็นสำหรับแอปพลิเคชันและวิธีการ interact กับมัน JsonSchema สามารถกำหนด validation, documentation, hyperlink navigation, และ interaction control ของ JSON data เราสามารถ generate JSON schema โดยใช้เมธอด generateSchema() ของ JsonSchemaGenerator ซึ่ง wrap JSON schema generation functionality

Syntax

public JsonSchema generateSchema(Class<T> type) throws JsonMappingException

ตัวอย่างการใช้งาน

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<String> courses;
    private Address address;
    
    // getters...
    public String getName() { return name; }
    public int getAge() { return age; }
    public List<String> getCourse() { return courses; }
    public Address getAddress() { return address; }
}

// Address class
class Address {
    private String firstLine;
    private String secondLine;
    private String thirdLine;
    
    // getters...
    public String getFirstLine() { return firstLine; }
    public String getSecondLine() { return secondLine; }
    public String getThirdLine() { return thirdLine; }
}

ผลลัพธ์ (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" }
    }
  }
}

คำอธิบาย

JSON Schema ที่ generate ได้อธิบายโครงสร้างของ Person class:

  • เป็น object (type: "object")
  • มี properties: name (string), age (integer), address (object), course (array)
  • address เป็น nested object ที่มี properties ของตัวเอง
  • course เป็น array ของ strings
  • id ใช้ URN format สำหรับ reference

การใช้งาน JSON Schema

  • Validation - ตรวจสอบ JSON input ตาม schema ก่อน processing
  • Documentation - เป็น API documentation อัตโนมัติ
  • Code Generation - generate client/server code จาก schema
  • Testing - validate test data
  • Contract Testing - รับรอง API compatibility

Dependencies ที่ต้องการ

<dependency>
    <groupId>com.fasterxml.jackson.module</groupId>
    <artifactId>jackson-module-jsonSchema</artifactId>
    <version>2.x.x</version>
</dependency>

สรุป

Jackson JSON Schema module ให้การ generate schema จาก Java classes อัตโนมัติ ช่วยใน validation, documentation, และ contract-first development เป็นเครื่องมือสำคัญสำหรับ microservices และ API development