json 转 java bean 2023年4月21日11:59:55
parent
9fbb1a7850
commit
1162e3a5c1
|
|
@ -129,7 +129,7 @@
|
|||
|
||||

|
||||
|
||||
### 5. sql 工具
|
||||
### 5. 代码 工具
|
||||
|
||||
#### 5.1 mybatis 代码生成
|
||||
|
||||
|
|
@ -144,7 +144,14 @@
|
|||

|
||||

|
||||
|
||||
#### 5.3 json 转 java bean
|
||||
|
||||
> 参考文章: https://blog.csdn.net/weixin_39651356/article/details/127155659
|
||||
|
||||

|
||||

|
||||

|
||||

|
||||
|
||||
### 6. 网络工具
|
||||
|
||||
|
|
|
|||
21
pom.xml
21
pom.xml
|
|
@ -356,6 +356,27 @@
|
|||
<version>4.92-p13</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jsonschema2pojo</groupId>
|
||||
<artifactId>jsonschema2pojo-core</artifactId>
|
||||
<version>1.2.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger2</artifactId>
|
||||
<version>${swagger.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-bean-validators</artifactId>
|
||||
<version>${swagger.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger-ui</artifactId>
|
||||
<version>${swagger.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
|||
|
|
@ -500,4 +500,8 @@ public class HomeController implements Serializable {
|
|||
public void pdf_menu_item(ActionEvent event) {
|
||||
load_small_tools(13);
|
||||
}
|
||||
|
||||
public void json_javabean_gen_menu_item(ActionEvent event) {
|
||||
load_sql_tools(2);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,176 @@
|
|||
package com.zhangmeng.tools.controller;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import com.zhangmeng.tools.json2Pojo.AnnotateConfig;
|
||||
import com.zhangmeng.tools.json2Pojo.Json2PojoCodeGeneratorConfig;
|
||||
import com.zhangmeng.tools.json2Pojo.Json2PojoCodeGeneratorUtil;
|
||||
import com.zhangmeng.tools.utils.AlertUtils;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.ComboBox;
|
||||
import javafx.scene.control.TextArea;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.util.StringConverter;
|
||||
|
||||
/**
|
||||
* @author : 芊芊墨客
|
||||
* @version : 1.0
|
||||
* @date : 2023-04-21 10:01
|
||||
*/
|
||||
public class Json2PoJoController {
|
||||
|
||||
@FXML
|
||||
public ComboBox<EnableBean> lombook_enable;
|
||||
|
||||
@FXML
|
||||
public ComboBox<EnableBean> mybatis_plus_enable;
|
||||
|
||||
@FXML
|
||||
public ComboBox<EnableBean> swagger_enable;
|
||||
|
||||
@FXML
|
||||
public Button convert;
|
||||
|
||||
public static int index = 1;
|
||||
|
||||
@FXML
|
||||
public TextField class_name;
|
||||
|
||||
@FXML
|
||||
public TextField package_name;
|
||||
|
||||
@FXML
|
||||
public TextArea json_content;
|
||||
|
||||
@FXML
|
||||
public TextArea result_show;
|
||||
|
||||
@FXML
|
||||
public void initialize() {
|
||||
|
||||
ObservableList<EnableBean> lombook_enable_list = FXCollections.observableArrayList();
|
||||
lombook_enable_list.add(new EnableBean("开启",true));
|
||||
lombook_enable_list.add(new EnableBean("关闭",false));
|
||||
lombook_enable.setItems(lombook_enable_list);
|
||||
lombook_enable.getSelectionModel().select(index);
|
||||
lombook_enable.setConverter(new StringConverter<EnableBean>() {
|
||||
@Override
|
||||
public String toString(EnableBean object) {
|
||||
|
||||
if (object != null){
|
||||
return object.title;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EnableBean fromString(String string) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
ObservableList<EnableBean> mybatis_plus_enable_list = FXCollections.observableArrayList();
|
||||
mybatis_plus_enable_list.add(new EnableBean("开启",true));
|
||||
mybatis_plus_enable_list.add(new EnableBean("关闭",false));
|
||||
mybatis_plus_enable.setItems(mybatis_plus_enable_list);
|
||||
mybatis_plus_enable.getSelectionModel().select(index);
|
||||
mybatis_plus_enable.setConverter(new StringConverter<EnableBean>() {
|
||||
@Override
|
||||
public String toString(EnableBean object) {
|
||||
|
||||
if (object != null){
|
||||
return object.title;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EnableBean fromString(String string) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
ObservableList<EnableBean> swagger_enable_list = FXCollections.observableArrayList();
|
||||
swagger_enable_list.add(new EnableBean("开启",true));
|
||||
swagger_enable_list.add(new EnableBean("关闭",false));
|
||||
swagger_enable.setItems(swagger_enable_list);
|
||||
swagger_enable.getSelectionModel().select(index);
|
||||
|
||||
swagger_enable.setConverter(new StringConverter<>() {
|
||||
@Override
|
||||
public String toString(EnableBean object) {
|
||||
|
||||
if (object != null) {
|
||||
return object.title;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EnableBean fromString(String string) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
convert.setOnAction(event -> {
|
||||
|
||||
if (class_name.getText().length() == 0){
|
||||
AlertUtils.alert_warning("类名不能为空!");
|
||||
return;
|
||||
}
|
||||
if (package_name.getText().length() == 0){
|
||||
AlertUtils.alert_warning("包名不能为空!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (json_content.getText().length() == 0){
|
||||
AlertUtils.alert_warning("json内容不能为空!");
|
||||
return;
|
||||
}
|
||||
|
||||
String pojoCode = Json2PojoCodeGeneratorUtil.json2pojo(
|
||||
Json2PojoCodeGeneratorConfig.builder()
|
||||
.pojoClassName(class_name.getText())
|
||||
.pojoPackageName(package_name.getText())
|
||||
.jsonContent(json_content.getText()).build(),
|
||||
AnnotateConfig.builder()
|
||||
.mybatis_plus_enable(mybatis_plus_enable.getSelectionModel().getSelectedItem().isEnable())
|
||||
.swagger_enable(swagger_enable.getSelectionModel().getSelectedItem().isEnable())
|
||||
.lombook_enable(lombook_enable.getSelectionModel().getSelectedItem().isEnable()).build()
|
||||
);
|
||||
result_show.setText(pojoCode);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public static class EnableBean {
|
||||
private String title;//开启关闭
|
||||
private boolean isEnable;
|
||||
|
||||
public EnableBean(String title, boolean isEnable) {
|
||||
this.title = title;
|
||||
this.isEnable = isEnable;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public boolean isEnable() {
|
||||
return isEnable;
|
||||
}
|
||||
|
||||
public void setEnable(boolean enable) {
|
||||
isEnable = enable;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -152,7 +152,7 @@ public class SSHConnectionController {
|
|||
webSocketServer.stop();
|
||||
Thread.sleep(100);
|
||||
isConnection = false;
|
||||
} catch (IOException | InterruptedException e) {
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -60,7 +60,7 @@ public class SocketServerController {
|
|||
if (isStart){
|
||||
try {
|
||||
webSocketServer.stop();
|
||||
} catch (IOException | InterruptedException e) {
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
|
@ -73,7 +73,7 @@ public class SocketServerController {
|
|||
webSocketServer.stop();
|
||||
isStart = false;
|
||||
server_status.setText("服务已经停止!端口:" + port.getText());
|
||||
} catch (IOException | InterruptedException e) {
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -64,8 +64,8 @@ public class SqlToolsController {
|
|||
private SimpleDoubleProperty height = new SimpleDoubleProperty(0.0);
|
||||
private AnchorPane root;
|
||||
private AnchorPane mysql_code_gen;
|
||||
private AnchorPane spring_security;
|
||||
private AnchorPane mybatis_plus_gen;
|
||||
private AnchorPane json2JavaBean;
|
||||
|
||||
public static final String color_cell = "#f4f4f4";
|
||||
|
||||
|
|
@ -75,16 +75,6 @@ public class SqlToolsController {
|
|||
@FXML
|
||||
private SplitPane splitPane;
|
||||
|
||||
@FXML
|
||||
public void md5_menu_item() {
|
||||
|
||||
}
|
||||
|
||||
@FXML
|
||||
public void spring_security_menu_item() {
|
||||
|
||||
}
|
||||
|
||||
@FXML
|
||||
public void video_menu_item() {
|
||||
load_player(0);
|
||||
|
|
@ -222,6 +212,13 @@ public class SqlToolsController {
|
|||
}
|
||||
mybatis_plus_gen(flag);
|
||||
}
|
||||
|
||||
if (newValue.getIndex() == 2) {
|
||||
if (json2JavaBean != null){
|
||||
flag = true;
|
||||
}
|
||||
json2JavaBean(flag);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -230,6 +227,7 @@ public class SqlToolsController {
|
|||
return switch (player){
|
||||
case MySql_Code_Generate -> new Image(ImagePath.path(ImagePath.ImagePathType.MD5));
|
||||
case MyBatis_plus_Generate -> new Image(ImagePath.path(ImagePath.ImagePathType.MD5));
|
||||
case Json_To_JavaBean -> new Image(ImagePath.path(ImagePath.ImagePathType.MD5));
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -304,6 +302,24 @@ public class SqlToolsController {
|
|||
common_method();
|
||||
}
|
||||
|
||||
//json 转javabean
|
||||
private void json2JavaBean(boolean flag){
|
||||
//默认选择第一个
|
||||
listView.getSelectionModel().select(2);
|
||||
|
||||
if (!flag){
|
||||
try {
|
||||
root = FXMLLoader.load(ResourcesUtils.getResource("json2pojo"));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
json2JavaBean = root;
|
||||
}else {
|
||||
root = json2JavaBean;
|
||||
}
|
||||
common_method();
|
||||
}
|
||||
|
||||
private void mybatis_plus_gen(boolean flag){
|
||||
//默认选择第一个
|
||||
listView.getSelectionModel().select(1);
|
||||
|
|
@ -473,4 +489,21 @@ public class SqlToolsController {
|
|||
public void maven_jar_install_menu_item(ActionEvent event) {
|
||||
load_small_tools(10);
|
||||
}
|
||||
|
||||
public void json_javabean_gen_menu_item(ActionEvent event) {
|
||||
boolean flag= false;
|
||||
|
||||
if (json2JavaBean != null){
|
||||
flag = true;
|
||||
}
|
||||
json2JavaBean(flag);
|
||||
}
|
||||
|
||||
public void md5_menu_item(ActionEvent event) {
|
||||
load_codec_tools(0);
|
||||
}
|
||||
|
||||
public void spring_security_menu_item(ActionEvent event) {
|
||||
load_codec_tools(1);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,30 @@
|
|||
package com.zhangmeng.tools.json2Pojo;
|
||||
|
||||
import com.zhangmeng.tools.controller.Json2PoJoController;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.ComboBox;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author : 芊芊墨客
|
||||
* @version : 1.0
|
||||
* @date : 2023-04-21 11:38
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
public class AnnotateConfig {
|
||||
|
||||
private boolean lombook_enable;
|
||||
|
||||
private boolean mybatis_plus_enable;
|
||||
|
||||
private boolean swagger_enable;
|
||||
|
||||
private boolean jackson_enable;
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
package com.zhangmeng.tools.json2Pojo;
|
||||
|
||||
/**
|
||||
* @author : 芊芊墨客
|
||||
* @version : 1.0
|
||||
* @date : 2023-04-21 09:26
|
||||
*/
|
||||
public enum GenerateCodeTypeEnum {
|
||||
JSON2POJO
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
package com.zhangmeng.tools.json2Pojo;
|
||||
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author : 芊芊墨客
|
||||
* @version : 1.0
|
||||
* @date : 2023-04-21 09:18
|
||||
*
|
||||
* 源代码链接地址: https://blog.csdn.net/weixin_39651356/article/details/127155659
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Json2PojoCodeGeneratorConfig {
|
||||
|
||||
/**
|
||||
* 用于解析成Bean的json内容
|
||||
*/
|
||||
String jsonContent;
|
||||
|
||||
/**
|
||||
* 结果Bean的类名
|
||||
* 不设置则为【TestBean】
|
||||
*/
|
||||
String pojoClassName;
|
||||
|
||||
/**
|
||||
* 结果Bean的包名
|
||||
* 不设置则使用工具类默认包名
|
||||
*/
|
||||
String pojoPackageName;
|
||||
}
|
||||
|
|
@ -0,0 +1,82 @@
|
|||
package com.zhangmeng.tools.json2Pojo;
|
||||
|
||||
import cn.hutool.core.util.ClassUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.sun.codemodel.JCodeModel;
|
||||
import lombok.SneakyThrows;
|
||||
import org.jsonschema2pojo.SchemaMapper;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
/**
|
||||
* @author : 芊芊墨客
|
||||
* @version : 1.0
|
||||
* @date : 2023-04-21 09:22
|
||||
* * 源代码链接地址: https://blog.csdn.net/weixin_39651356/article/details/127155659
|
||||
*/
|
||||
public class Json2PojoCodeGeneratorUtil{
|
||||
|
||||
@SneakyThrows
|
||||
public static String json2pojo(String jsonContent) {
|
||||
return json2pojo(jsonContent, "TestBean", ClassUtil.getPackage(Json2PojoCodeGeneratorUtil.class),
|
||||
AnnotateConfig.builder()
|
||||
.mybatis_plus_enable(true)
|
||||
.swagger_enable(true)
|
||||
.lombook_enable(true)
|
||||
.jackson_enable(true)
|
||||
.build()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* json内容转成Bean代码
|
||||
* @param jsonContent json内容
|
||||
* @param pojoClassName 生成的Bean的Class名字
|
||||
* @param pojoPackageName 生成的Bean的包名字
|
||||
* @return
|
||||
*/
|
||||
@SneakyThrows
|
||||
public static String json2pojo(String jsonContent, String pojoClassName, String pojoPackageName,AnnotateConfig annotateConfig) {
|
||||
return json2pojo(Json2PojoCodeGeneratorConfig.builder()
|
||||
.jsonContent(jsonContent)
|
||||
.pojoClassName(pojoClassName)
|
||||
.pojoPackageName(pojoPackageName)
|
||||
.build(),annotateConfig);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* json内容转成Bean代码
|
||||
* @param json2PojoGenerateConfig 配置信息
|
||||
* @return
|
||||
*/
|
||||
@SneakyThrows
|
||||
public static String json2pojo(Json2PojoCodeGeneratorConfig json2PojoGenerateConfig,AnnotateConfig annotateConfig) {
|
||||
String jsonContent = json2PojoGenerateConfig.getJsonContent();
|
||||
Assert.isTrue(JSONUtil.isTypeJSON(jsonContent), "非JSON内容,请检查");
|
||||
String pojoClassName = StrUtil.blankToDefault(json2PojoGenerateConfig.getPojoClassName(), "TestBean");
|
||||
String pojoPackageName = StrUtil.blankToDefault(json2PojoGenerateConfig.getPojoPackageName(), ClassUtil.getPackage(Json2PojoCodeGeneratorUtil.class));
|
||||
|
||||
//1. 生成配置设置
|
||||
SchemaMapper mapper = MyJsonschema2pojoConfig.getDefaultSchemaMapper(annotateConfig);
|
||||
JCodeModel codeModel = new JCodeModel();
|
||||
mapper.generate(codeModel, pojoClassName, pojoPackageName, jsonContent);
|
||||
|
||||
//2. 开始生成代码
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
MyCodeWriter myCodeWriter = new MyCodeWriter(byteArrayOutputStream);
|
||||
codeModel.build(myCodeWriter);
|
||||
|
||||
//3. 获取结果
|
||||
String result = new String(byteArrayOutputStream.toByteArray());
|
||||
|
||||
return StrUtil.trim(result);
|
||||
}
|
||||
|
||||
public String generate(Json2PojoCodeGeneratorConfig generatorConfig,AnnotateConfig annotateConfig) {
|
||||
return json2pojo(generatorConfig,annotateConfig);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,99 @@
|
|||
package com.zhangmeng.tools.json2Pojo;
|
||||
|
||||
import cn.hutool.core.lang.func.LambdaUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.sun.codemodel.JDefinedClass;
|
||||
import com.sun.codemodel.JFieldVar;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.*;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.jsonschema2pojo.AbstractTypeInfoAwareAnnotator;
|
||||
import org.jsonschema2pojo.GenerationConfig;
|
||||
|
||||
/**
|
||||
* @author : 芊芊墨客
|
||||
* @version : 1.0
|
||||
* @date : 2023-04-21 09:29
|
||||
*
|
||||
* * 源代码链接地址: https://blog.csdn.net/weixin_39651356/article/details/127155659
|
||||
*/
|
||||
@Setter
|
||||
@Getter
|
||||
public class MyAbstractTypeInfoAwareAnnotator extends AbstractTypeInfoAwareAnnotator {
|
||||
|
||||
/**
|
||||
* 是否开启使用lombok注解
|
||||
*/
|
||||
boolean lombokAnnotFlag = true;
|
||||
|
||||
/**
|
||||
* 是否开启使用swagger注解
|
||||
*/
|
||||
boolean swaggerAnnotFlag = true;
|
||||
|
||||
/**
|
||||
* 是否开启使用mybatisplus注解
|
||||
*/
|
||||
boolean mybatisPlusAnnotFlag = true;
|
||||
|
||||
|
||||
/**
|
||||
* 是否开启使用jackson注解
|
||||
*/
|
||||
boolean jacksonAnnotFlag = false;
|
||||
|
||||
|
||||
public MyAbstractTypeInfoAwareAnnotator(GenerationConfig generationConfig) {
|
||||
super(generationConfig);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addJsonTypeInfoAnnotation(JDefinedClass clazz, String propertyName) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 类上的注解
|
||||
*/
|
||||
@Override
|
||||
public void propertyInclusion(JDefinedClass clazz, JsonNode schema) {
|
||||
if (lombokAnnotFlag) {
|
||||
clazz.annotate(Data.class);
|
||||
clazz.annotate(ToString.class).param(LambdaUtil.getMethodName(ToString::callSuper), true);
|
||||
clazz.annotate(Builder.class);
|
||||
clazz.annotate(NoArgsConstructor.class);
|
||||
clazz.annotate(AllArgsConstructor.class);
|
||||
clazz.annotate(Accessors.class).param(LambdaUtil.getMethodName(Accessors::chain), true);
|
||||
}
|
||||
if (mybatisPlusAnnotFlag) {
|
||||
clazz.annotate(TableName.class).param(LambdaUtil.getMethodName(TableName::value), StrUtil.toUnderlineCase(clazz.name()));
|
||||
}
|
||||
if (swaggerAnnotFlag) {
|
||||
clazz.annotate(ApiModel.class).param(LambdaUtil.getMethodName(ApiModel::description), clazz.name());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 属性字段上的注解
|
||||
*/
|
||||
@Override
|
||||
public void propertyField(JFieldVar field, JDefinedClass clazz, String propertyName, JsonNode propertyNode) {
|
||||
if (mybatisPlusAnnotFlag) {
|
||||
field.annotate(TableField.class).param("value", StrUtil.toUnderlineCase(propertyName));
|
||||
}
|
||||
if (swaggerAnnotFlag) {
|
||||
field.annotate(ApiModelProperty.class).param("value", propertyName);
|
||||
}
|
||||
if (jacksonAnnotFlag) {
|
||||
field.annotate(JsonProperty.class).param("value", propertyName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
package com.zhangmeng.tools.json2Pojo;
|
||||
|
||||
import com.sun.codemodel.CodeWriter;
|
||||
import com.sun.codemodel.JPackage;
|
||||
|
||||
import java.io.FilterOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
/**
|
||||
* @author : 芊芊墨客
|
||||
* @version : 1.0
|
||||
* @date : 2023-04-21 09:21
|
||||
*
|
||||
* * 源代码链接地址: https://blog.csdn.net/weixin_39651356/article/details/127155659
|
||||
*/
|
||||
public class MyCodeWriter extends CodeWriter {
|
||||
|
||||
|
||||
private final PrintStream out;
|
||||
|
||||
/**
|
||||
* @param os
|
||||
* This stream will be closed at the end of the code generation.
|
||||
*/
|
||||
public MyCodeWriter( OutputStream os ) {
|
||||
this.out = new PrintStream(os);;
|
||||
}
|
||||
|
||||
@Override
|
||||
public OutputStream openBinary(JPackage pkg, String fileName) throws IOException {
|
||||
String pkgName = pkg.name();
|
||||
if(pkgName.length()!=0) pkgName += '.';
|
||||
return new FilterOutputStream(out) {
|
||||
public void close() {
|
||||
// don't let this stream close
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,130 @@
|
|||
package com.zhangmeng.tools.json2Pojo;
|
||||
|
||||
import org.jsonschema2pojo.*;
|
||||
import org.jsonschema2pojo.rules.RuleFactory;
|
||||
|
||||
/**
|
||||
* @author : 芊芊墨客
|
||||
* @version : 1.0
|
||||
* @date : 2023-04-21 09:20
|
||||
*
|
||||
* * 源代码链接地址: https://blog.csdn.net/weixin_39651356/article/details/127155659
|
||||
*/
|
||||
public class MyJsonschema2pojoConfig {
|
||||
|
||||
/**
|
||||
* 总体配置
|
||||
* @param includeGetAndSetFlag 是否需要get\set代码
|
||||
* @return
|
||||
*/
|
||||
public static GenerationConfig getGenerationConfig(boolean includeGetAndSetFlag) {
|
||||
return new DefaultGenerationConfig() {
|
||||
|
||||
@Override
|
||||
public boolean isIncludeAllPropertiesConstructor() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用json内容进行构建javaBean
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public SourceType getSourceType() {
|
||||
return SourceType.JSON;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isGenerateBuilders() { // set config option by overriding method
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AnnotationStyle getAnnotationStyle() {
|
||||
return AnnotationStyle.NONE;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isIncludeAdditionalProperties() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isIncludeGetters() {
|
||||
return includeGetAndSetFlag;
|
||||
}
|
||||
@Override
|
||||
public boolean isIncludeSetters() {
|
||||
return includeGetAndSetFlag;
|
||||
}
|
||||
@Override
|
||||
public boolean isIncludeToString() {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isSerializable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isIncludeGeneratedAnnotation() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isIncludeHashcodeAndEquals() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTargetVersion() {
|
||||
return "1.8";
|
||||
}
|
||||
|
||||
@Override
|
||||
public InclusionLevel getInclusionLevel() {
|
||||
return InclusionLevel.ALWAYS;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static GenerationConfig getGenerationConfig() {
|
||||
return getGenerationConfig(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成的注解配置
|
||||
* @param generationConfig
|
||||
* @return
|
||||
*/
|
||||
public static MyAbstractTypeInfoAwareAnnotator getAnnotator(GenerationConfig generationConfig) {
|
||||
return new MyAbstractTypeInfoAwareAnnotator(generationConfig);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 自定义总体配置+注解配置
|
||||
* @return
|
||||
*/
|
||||
public static SchemaMapper getSchemaMapper(GenerationConfig config, MyAbstractTypeInfoAwareAnnotator myAbstractTypeInfoAwareAn) {
|
||||
return new SchemaMapper(new RuleFactory(config, myAbstractTypeInfoAwareAn, new SchemaStore()), new SchemaGenerator());
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认总体配置+注解配置
|
||||
* @return
|
||||
*/
|
||||
public static SchemaMapper getDefaultSchemaMapper(AnnotateConfig annotateConfig) {
|
||||
GenerationConfig generationConfig = getGenerationConfig();
|
||||
MyAbstractTypeInfoAwareAnnotator annotator = getAnnotator(generationConfig);
|
||||
annotator.setLombokAnnotFlag(annotateConfig.isLombook_enable());
|
||||
annotator.setSwaggerAnnotFlag(annotateConfig.isSwagger_enable());
|
||||
annotator.setMybatisPlusAnnotFlag(annotateConfig.isMybatis_plus_enable());
|
||||
annotator.setJacksonAnnotFlag(annotateConfig.isJackson_enable());
|
||||
return getSchemaMapper(generationConfig,annotator);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,198 @@
|
|||
package com.zhangmeng.tools.json2Pojo;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
|
||||
/**
|
||||
* @author : 芊芊墨客
|
||||
* @version : 1.0
|
||||
* @date : 2023-04-21 09:44
|
||||
*/
|
||||
public class TestUtils {
|
||||
|
||||
public static void main(String[] args) {
|
||||
t();
|
||||
}
|
||||
|
||||
public static void t(){
|
||||
|
||||
String json = "[{\n" +
|
||||
" \"title\": \"当日销售状况\",\n" +
|
||||
" \"childs\": [{\n" +
|
||||
" \"title\": \"堂食业务\",\n" +
|
||||
" \"childs\": [{\n" +
|
||||
" \"title\": \"折前销售额\",\n" +
|
||||
" \"value\": \"9999.00\"\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"title\": \"优惠金额\",\n" +
|
||||
" \"value\": \"9999.00\"\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"title\": \"折后销售额\",\n" +
|
||||
" \"value\": \"9999.00\"\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"title\": \"堂食动销费\",\n" +
|
||||
" \"value\": \"9999.00\"\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"title\": \"收款手续费\",\n" +
|
||||
" \"value\": \"9999.00\"\n" +
|
||||
" }\n" +
|
||||
" ]\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"title\": \"会员储值\",\n" +
|
||||
" \"childs\": [{\n" +
|
||||
" \"title\": \"储值金额\",\n" +
|
||||
" \"value\": \"9999.00\"\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"title\": \"赠送金额\",\n" +
|
||||
" \"value\": \"9999.00\"\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"title\": \"赠券金额\",\n" +
|
||||
" \"value\": \"9999.00\"\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"title\": \"收款手续费\",\n" +
|
||||
" \"value\": \"9999.00\"\n" +
|
||||
" }\n" +
|
||||
" ]\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"title\": \"预售业务\",\n" +
|
||||
" \"childs\": [{\n" +
|
||||
" \"title\": \"预售订单数\",\n" +
|
||||
" \"value\": \"9999.00\"\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"title\": \"销售额\",\n" +
|
||||
" \"value\": \"9999.00\"\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"title\": \"预售动销费\",\n" +
|
||||
" \"value\": \"9999.00\"\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"title\": \"技术服务费\",\n" +
|
||||
" \"value\": \"9999.00\"\n" +
|
||||
" }\n" +
|
||||
" ]\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"title\": \"团购业务\",\n" +
|
||||
" \"childs\": [{\n" +
|
||||
" \"title\": \"团购订单数\",\n" +
|
||||
" \"value\": \"9999.00\"\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"title\": \"销售额\",\n" +
|
||||
" \"value\": \"9999.00\"\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"title\": \"团购动销费\",\n" +
|
||||
" \"value\": \"9999.00\"\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"title\": \"技术服务费\",\n" +
|
||||
" \"value\": \"9999.00\"\n" +
|
||||
" }\n" +
|
||||
" ]\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"title\": \"外卖业务\",\n" +
|
||||
" \"childs\": [{\n" +
|
||||
" \"title\": \"外卖订单数\",\n" +
|
||||
" \"value\": \"9999.00\"\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"title\": \"折前销售额\",\n" +
|
||||
" \"value\": \"9999.00\"\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"title\": \"优惠金额\",\n" +
|
||||
" \"value\": \"9999.00\"\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"title\": \"折后销售额\",\n" +
|
||||
" \"value\": \"9999.00\"\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"title\": \"外卖动销费\",\n" +
|
||||
" \"value\": \"9999.00\"\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"title\": \"技术服务费\",\n" +
|
||||
" \"value\": \"9999.00\"\n" +
|
||||
" }\n" +
|
||||
" ]\n" +
|
||||
" }\n" +
|
||||
" ]\n" +
|
||||
"}, {\n" +
|
||||
" \"title\": \"当日用餐状况\",\n" +
|
||||
" \"childs\": [{\n" +
|
||||
" \"title\": \"\",\n" +
|
||||
" \"childs\": [{\n" +
|
||||
" \"title\": \"就餐人数\",\n" +
|
||||
" \"value\": \"999\"\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"title\": \"人均消费\",\n" +
|
||||
" \"value\": \"9999.00\"\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"title\": \"就餐桌数\",\n" +
|
||||
" \"value\": \"99\"\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"title\": \"桌均消费\",\n" +
|
||||
" \"value\": \"9999.00\"\n" +
|
||||
" }\n" +
|
||||
" ]\n" +
|
||||
" }]\n" +
|
||||
"}, {\n" +
|
||||
" \"title\": \"当日推广专用账户支出\",\n" +
|
||||
" \"childs\": [{\n" +
|
||||
" \"title\": \"\",\n" +
|
||||
" \"childs\": [{\n" +
|
||||
" \"title\": \"堂食动销费\",\n" +
|
||||
" \"value\": \"999.00\"\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"title\": \"促推活动奖金\",\n" +
|
||||
" \"value\": \"9999.00\"\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"title\": \"代言奖金\",\n" +
|
||||
" \"value\": \"9999.00\"\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"title\": \"推销奖金\",\n" +
|
||||
" \"value\": \"9999.00\"\n" +
|
||||
" }\n" +
|
||||
" ]\n" +
|
||||
" }]\n" +
|
||||
"}, {\n" +
|
||||
" \"title\": \"当日账户余额\",\n" +
|
||||
" \"childs\": [{\n" +
|
||||
" \"title\": \"\",\n" +
|
||||
" \"childs\": [{\n" +
|
||||
" \"title\": \"系统结算账户余额\",\n" +
|
||||
" \"value\": \"999.00\"\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"title\": \"推广专用账户余额\",\n" +
|
||||
" \"value\": \"9999.00\"\n" +
|
||||
" }\n" +
|
||||
" ]\n" +
|
||||
" }]\n" +
|
||||
"}]";
|
||||
|
||||
// String pojoCode = Json2PojoCodeGeneratorUtil.json2pojo(json,"Test", "work.linruchang.lrcutilsweb.consts");
|
||||
// Console.log(pojoCode);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
package com.zhangmeng.tools.utils;
|
||||
|
||||
import com.sun.codemodel.JCodeModel;
|
||||
import org.jsonschema2pojo.*;
|
||||
import org.jsonschema2pojo.rules.RuleFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
|
||||
/**
|
||||
* @author : 芊芊墨客
|
||||
* @version : 1.0
|
||||
* @date : 2023-04-21 09:06
|
||||
*/
|
||||
public class Code2pojo {
|
||||
|
||||
public void t() throws IOException {
|
||||
JCodeModel codeModel = new JCodeModel();
|
||||
|
||||
URL source = Code2pojo.class.getResource("/schema/required.json");
|
||||
|
||||
GenerationConfig config = new DefaultGenerationConfig() {
|
||||
@Override
|
||||
public boolean isGenerateBuilders() { // set config option by overriding method
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
SchemaMapper mapper = new SchemaMapper(new RuleFactory(config, new Jackson2Annotator(config), new SchemaStore()), new SchemaGenerator());
|
||||
mapper.generate(codeModel, "ClassName", "com.example", source);
|
||||
|
||||
codeModel.build(Files.createTempDirectory("required").toFile());
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -188,6 +188,7 @@ public class ResourcesUtils {
|
|||
|
||||
MySql_Code_Generate("mysql 代码生成",0),
|
||||
MyBatis_plus_Generate("mybatis-plus 代码生成",1),
|
||||
Json_To_JavaBean("json转javabean 代码生成",2),
|
||||
;
|
||||
|
||||
SqlTools(String title, int index) {
|
||||
|
|
|
|||
|
|
@ -56,10 +56,11 @@
|
|||
</items>
|
||||
</Menu>
|
||||
|
||||
<Menu mnemonicParsing="false" text="sql工具">
|
||||
<Menu mnemonicParsing="false" text="代码工具">
|
||||
<items>
|
||||
<MenuItem mnemonicParsing="false" text="mysql代码生成" onAction="#sql_code_gen_menu_item"/>
|
||||
<MenuItem mnemonicParsing="false" text="mybatis-plus 代码生成" onAction="#mybatis_plus_gen_menu_item"/>
|
||||
<MenuItem mnemonicParsing="false" text="json转javaBean 代码生成" onAction="#json_javabean_gen_menu_item"/>
|
||||
</items>
|
||||
</Menu>
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,28 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import javafx.scene.control.Button?>
|
||||
<?import javafx.scene.control.ComboBox?>
|
||||
<?import javafx.scene.control.Label?>
|
||||
<?import javafx.scene.control.TextArea?>
|
||||
<?import javafx.scene.control.TextField?>
|
||||
<?import javafx.scene.layout.AnchorPane?>
|
||||
|
||||
<AnchorPane prefHeight="649.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.zhangmeng.tools.controller.Json2PoJoController">
|
||||
<children>
|
||||
<TextField fx:id="class_name" layoutX="168.0" layoutY="60.0" prefHeight="25.0" prefWidth="246.0" />
|
||||
<Label layoutX="71.0" layoutY="64.0" text="生成的类名:" />
|
||||
<Label layoutX="71.0" layoutY="130.0" text="生成的包名:" />
|
||||
<Label layoutX="77.0" layoutY="202.0" text="json内容:" />
|
||||
<TextArea fx:id="json_content" layoutX="167.0" layoutY="202.0" prefHeight="413.0" prefWidth="439.0" AnchorPane.bottomAnchor="34.0" AnchorPane.topAnchor="202.0" />
|
||||
<TextField fx:id="package_name" layoutX="168.0" layoutY="126.0" prefHeight="25.0" prefWidth="246.0" />
|
||||
<Label layoutX="451.0" layoutY="64.0" text="是否开启lombook注解:" />
|
||||
<ComboBox fx:id="lombook_enable" layoutX="587.0" layoutY="60.0" prefHeight="25.0" prefWidth="127.0" />
|
||||
<ComboBox fx:id="mybatis_plus_enable" layoutX="612.0" layoutY="126.0" prefHeight="25.0" prefWidth="127.0" />
|
||||
<ComboBox fx:id="swagger_enable" layoutX="881.0" layoutY="60.0" prefHeight="25.0" prefWidth="127.0" />
|
||||
<Label layoutX="739.0" layoutY="64.0" text="是否开启swagger注解:" />
|
||||
<Label layoutX="451.0" layoutY="130.0" text="是否开启mbatis-plus注解:" />
|
||||
<Label layoutX="635.0" layoutY="202.0" text="javabean:" />
|
||||
<TextArea fx:id="result_show" layoutX="714.0" layoutY="202.0" prefHeight="413.0" prefWidth="447.0" AnchorPane.bottomAnchor="34.0" AnchorPane.leftAnchor="714.0" AnchorPane.rightAnchor="39.0" AnchorPane.topAnchor="202.0" />
|
||||
<Button fx:id="convert" layoutX="635.0" layoutY="371.0" mnemonicParsing="false" text="转换" />
|
||||
</children>
|
||||
</AnchorPane>
|
||||
|
|
@ -17,8 +17,7 @@
|
|||
<Menu mnemonicParsing="false" text="加密工具">
|
||||
<items>
|
||||
<MenuItem mnemonicParsing="false" onAction="#md5_menu_item" text="md5 加密"/>
|
||||
<MenuItem mnemonicParsing="false" onAction="#spring_security_menu_item"
|
||||
text="spring security 加密"/>
|
||||
<MenuItem mnemonicParsing="false" onAction="#spring_security_menu_item" text="spring security 加密"/>
|
||||
</items>
|
||||
</Menu>
|
||||
<Menu mnemonicParsing="false" text="影音工具">
|
||||
|
|
@ -53,10 +52,11 @@
|
|||
</items>
|
||||
</Menu>
|
||||
|
||||
<Menu mnemonicParsing="false" text="sql工具">
|
||||
<Menu mnemonicParsing="false" text="代码工具">
|
||||
<items>
|
||||
<MenuItem mnemonicParsing="false" text="mysql代码生成" onAction="#sql_code_gen_menu_item"/>
|
||||
<MenuItem mnemonicParsing="false" text="mybatis-plus 代码生成" onAction="#mybatis_plus_gen_menu_item"/>
|
||||
<MenuItem mnemonicParsing="false" text="json转javaBean 代码生成" onAction="#json_javabean_gen_menu_item"/>
|
||||
</items>
|
||||
</Menu>
|
||||
|
||||
|
|
|
|||
Binary file not shown.
|
After Width: | Height: | Size: 153 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 108 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 85 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 94 KiB |
Loading…
Reference in New Issue