输出
{
"id": this.id,
"questionType": this.questionType,
"subjectId": this.subjectId,
"score": this.score,
"gradeLevel": this.gradeLevel,
"difficult": this.difficult,
"infoTextContentId": this.infoTextContentId,
"createUser": this.createUser,
"status": this.status,
"createTime": this.createTime,
"deleted": this.deleted,
"correct": this.correct,
}
D:\proj\springBoot\exam-sys-db2\src\main\java\top\starp\util\JsonUtil.java
public static String clazzToJsonDefault(Class clazz) throws JsonMappingException {
// org.codehaus.jackson.map.ObjectMapper mapper = new ObjectMapper();
ObjectMapper mapper = new ObjectMapper();
//There are other configuration options you can set. This is the one I needed.
// mapper.configure(SerializationConfig.Feature.WRITE_ENUMS_USING_TO_STRING, true);
mapper.configure(WRITE_ENUMS_USING_TO_STRING, true);
JsonSchema schema = mapper.generateJsonSchema(clazz);
// Spring接收枚举类型_言玉gz的博客-CSDN博客_spring 枚举
// https://blog.csdn.net/u010811939/article/details/106671017/
ObjectNode schemaNode = schema.getSchemaNode();
// JsonNode type = schemaNode.get(0);
是type
//
// JsonNode type1 = schemaNode.get("type");
JsonNode properties = schemaNode.get("properties");
// System.out.println("properties");
// System.out.println(properties);
Iterator<String> stringIterator = properties.fieldNames();
// System.out.println("stringIterator");
// System.out.println(stringIterator);
String jsonStr="{\n" +
" #jsonBody#"+
" }";
StringBuilder jsonBody= new StringBuilder();
while (stringIterator.hasNext()) {
String next = stringIterator.next();
System.out.println(next);
String line= "\"#fieldName#\": this.#fieldName#,";
// String line= next+ " : "+" '', ";
line= line.replace("#fieldName#",next);
jsonBody.append(line).append("\n");
}
jsonStr= jsonStr.replace("#jsonBody#", jsonBody.toString());
// for (JsonNode property : properties) {
// System.out.println(property);
property.get
// }
// System.out.println("jsonNodeVal");
// System.out.println(jsonNodeVal);
// for (JsonNode jsonNode : jsonNodeVal) {
jsonNode.get
// System.out.println(jsonNode);
// }
// JsonNode jsonNode = schemaNode.get(0);
// System.out.println(jsonNode);
return jsonStr;
}
String jsonDefault = JsonUtil.clazzToJsonDefault(Question.class);
System.out.println(jsonDefault);
package com.starp.exam;
import java.util.Date;
public class Question {
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_question.id
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
private Integer id;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_question.question_type
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
private Integer questionType;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_question.subject_id
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
private Integer subjectId;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_question.score
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
private Integer score;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_question.grade_level
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
private Integer gradeLevel;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_question.difficult
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
private Integer difficult;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_question.info_text_content_id
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
private Integer infoTextContentId;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_question.create_user
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
private Integer createUser;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_question.status
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
private Integer status;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_question.create_time
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
private Date createTime;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_question.deleted
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
private Boolean deleted;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column t_question.correct
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
private String correct;
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column t_question.id
*
* @return the value of t_question.id
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
public Integer getId() {
return id;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column t_question.id
*
* @param id the value for t_question.id
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
public void setId(Integer id) {
this.id = id;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column t_question.question_type
*
* @return the value of t_question.question_type
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
public Integer getQuestionType() {
return questionType;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column t_question.question_type
*
* @param questionType the value for t_question.question_type
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
public void setQuestionType(Integer questionType) {
this.questionType = questionType;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column t_question.subject_id
*
* @return the value of t_question.subject_id
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
public Integer getSubjectId() {
return subjectId;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column t_question.subject_id
*
* @param subjectId the value for t_question.subject_id
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
public void setSubjectId(Integer subjectId) {
this.subjectId = subjectId;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column t_question.score
*
* @return the value of t_question.score
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
public Integer getScore() {
return score;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column t_question.score
*
* @param score the value for t_question.score
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
public void setScore(Integer score) {
this.score = score;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column t_question.grade_level
*
* @return the value of t_question.grade_level
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
public Integer getGradeLevel() {
return gradeLevel;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column t_question.grade_level
*
* @param gradeLevel the value for t_question.grade_level
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
public void setGradeLevel(Integer gradeLevel) {
this.gradeLevel = gradeLevel;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column t_question.difficult
*
* @return the value of t_question.difficult
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
public Integer getDifficult() {
return difficult;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column t_question.difficult
*
* @param difficult the value for t_question.difficult
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
public void setDifficult(Integer difficult) {
this.difficult = difficult;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column t_question.info_text_content_id
*
* @return the value of t_question.info_text_content_id
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
public Integer getInfoTextContentId() {
return infoTextContentId;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column t_question.info_text_content_id
*
* @param infoTextContentId the value for t_question.info_text_content_id
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
public void setInfoTextContentId(Integer infoTextContentId) {
this.infoTextContentId = infoTextContentId;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column t_question.create_user
*
* @return the value of t_question.create_user
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
public Integer getCreateUser() {
return createUser;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column t_question.create_user
*
* @param createUser the value for t_question.create_user
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
public void setCreateUser(Integer createUser) {
this.createUser = createUser;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column t_question.status
*
* @return the value of t_question.status
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
public Integer getStatus() {
return status;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column t_question.status
*
* @param status the value for t_question.status
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
public void setStatus(Integer status) {
this.status = status;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column t_question.create_time
*
* @return the value of t_question.create_time
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
public Date getCreateTime() {
return createTime;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column t_question.create_time
*
* @param createTime the value for t_question.create_time
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column t_question.deleted
*
* @return the value of t_question.deleted
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
public Boolean getDeleted() {
return deleted;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column t_question.deleted
*
* @param deleted the value for t_question.deleted
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
public void setDeleted(Boolean deleted) {
this.deleted = deleted;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column t_question.correct
*
* @return the value of t_question.correct
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
public String getCorrect() {
return correct;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column t_question.correct
*
* @param correct the value for t_question.correct
*
* @mbg.generated Mon Nov 14 13:40:37 CST 2022
*/
public void setCorrect(String correct) {
this.correct = correct == null ? null : correct.trim();
}
}