java类的创建模式_从Java类创建JSON模式

小编典典

Gson库可能不包含类似的功能,但是您可以尝试使用Jackson库和jackson-

module-jsonSchema模块解决您的问题。例如,对于以下类别:

class Entity {

private Long id;

private List profiles;

// getters/setters

}

class Profile {

private String name;

private String value;

// getters / setters

}

这个程序:

import java.io.IOException;

import java.util.List;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.fasterxml.jackson.module.jsonSchema.JsonSchema;

import com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper;

public class JacksonProgram {

public static void main(String[] args) throws IOException {

ObjectMapper mapper = new ObjectMapper();

SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();

mapper.acceptJsonFormatVisitor(Entity.class, visitor);

JsonSchema schema = visitor.finalSchema();

System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema));

}

}

打印以下架构:

{

"type" : "object",

"properties" : {

"id" : {

"type" : "integer"

},

"profiles" : {

"type" : "array",

"items" : {

"type" : "object",

"properties" : {

"name" : {

"type" : "string"

},

"value" : {

"type" : "string"

}

}

}

}

}

}

2020-07-27

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值