• clazzToJsonDefault java 实体 to json 字符串


    输出

    {
            "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,
        }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    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;
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    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();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
  • 相关阅读:
    NIFI关于Variables的使用
    使用FastChat部署Baichuan2
    学习强化学习该具备的技能和环境
    SSM学习47:SpringMvc五种参数传递
    写给自己:入职两个月的收获与变化
    我们来用Unity做个2D像素boss战
    可用于高质量回测的 MetaTrader 历史数据导入及转换教程
    springboot日志配置(logback+slf4j配置)
    day5:Node.js 第三方库
    ODS、数据集市、数据仓库的异同点
  • 原文地址:https://blog.csdn.net/jonathan_joestar/article/details/128106144