体力活干多了, 就会思考怎么来提高效率了.
最近有个项目需要和第三方对接, 生成API实体很多且本质就是体力活.
遂思考: 大佬们应该也有这个困惑. 正所谓“前人栽树后人乘凉”, 只要能描述清楚自己诉求, 往上一搜一大堆.
结合自己的现状, 使用maven插件生成最方便. 下面开始jsonschema2pojo-maven-plugin
这个插件的快速入门.
引入唯一的依赖commons-lang
<dependency>
<groupId>commons-langgroupId>
<artifactId>commons-langartifactId>
<version>2.6version>
dependency>
添加plugin
<plugin>
<groupId>org.jsonschema2pojogroupId>
<artifactId>jsonschema2pojo-maven-pluginartifactId>
<version>0.4.34version>
<configuration>
<sourceDirectory>${basedir}/src/main/resourcessourceDirectory>
<targetPackage>com.demo.beantargetPackage>
<outputDirectory>${basedir}/src/main/javaoutputDirectory>
<addCompileSourceRoot>falseaddCompileSourceRoot>
<sourceType>jsonsourceType>
<annotationStyle>noneannotationStyle>
configuration>
<executions>
<execution>
<goals>
<goal>generategoal>
goals>
execution>
executions>
plugin>
configuration配置说明:
主要关注configuration节点下面的配置项:
sourceDirectory
:json文件所在的目录,会将该目录下所有json进行转换,不支持指定单个文件,如果不想全部转换,可以添加一个excludes
节点。如:<excludes>
<exclude>init.jsonexclude>
excludes>
targetPackage
:生成的Java pojo类目标包名。outputDirectory
:生成Java pojo类的(根)目录,换句话说,源文件最终目录为此目录+包目录,注意,此值默认为target\java-gen
,官方配置中并没有该默认值,如果成功生成后发现找不到源文件,看一看是不是这个原因。addCompileSourceRoot
:是否将输出目录作为项目的源码根目录。sourceType
:json格式,两个可选值,jsonschema
:json模式和json
:json文件,默认为jsonschema,不过我们一般是希望直接使用json文件生成,所以指定为
json`。annotationStyle
:生成的(字段)注解样式,有五个可选值:jackson1
、jackson2
、gson
、moshi1
和none
,默认是jackson2
,一般指定none
就可以了,还有比如用fastjson
,那也要指定none
。将json数据放到配置的sourceDirectory
目录下
orderReq.json
{
"appid": "e3b19c2dcfff49dfa019dced4e4ee9e5",
"personnelId": "2067831",
"saleId": "105198",
"contactMobile": "13900000000",
"number": "17317810524",
"installedAddress": "江苏路500号",
"province": "上海市",
"city": "上海市",
"idCard": "340321000000000000",
"name": "张三",
"county": "长宁区",
"sign": "NTA3ZjVhMTlkMGU0Y2E1MTFkYzRjMzk5OTg4MjI5NTM="
}
直接执行mvn package
打包命令就可以进行生成操作了,执行成功后你会在对应的包下面发现生成的java pojo源文件。
生成的Java类名就是json文件的文件名
public class OrderReq {
private String appid;
private String personnelId;
private String saleId;
private String contactMobile;
private String number;
private String installedAddress;
private String province;
private String city;
private String idCard;
private String name;
private String county;
private String sign;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
public String getAppid() {
return appid;
}
public void setAppid(String appid) {
this.appid = appid;
}
public String getPersonnelId() {
return personnelId;
}
public void setPersonnelId(String personnelId) {
this.personnelId = personnelId;
}
public String getSaleId() {
return saleId;
}
public void setSaleId(String saleId) {
this.saleId = saleId;
}
public String getContactMobile() {
return contactMobile;
}
public void setContactMobile(String contactMobile) {
this.contactMobile = contactMobile;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getInstalledAddress() {
return installedAddress;
}
public void setInstalledAddress(String installedAddress) {
this.installedAddress = installedAddress;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getIdCard() {
return idCard;
}
public void setIdCard(String idCard) {
this.idCard = idCard;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCounty() {
return county;
}
public void setCounty(String county) {
this.county = county;
}
public String getSign() {
return sign;
}
public void setSign(String sign) {
this.sign = sign;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}