spring boot使用jackson处理前端返回的json?

前端返回的json如下:

{
    "username": {
        "__v_isShallow": false,
        "dep": {
            "w": 0,
            "n": 0
        },
        "__v_isRef": true,
        "_rawValue": "1111",
        "_value": "1111"
    },
    "password": {
        "__v_isShallow": false,
        "dep": {
            "w": 0,
            "n": 0
        },
        "__v_isRef": true,
        "_rawValue": "12",
        "_value": "12"
    }
}

请问pojo类(如下图)里面相应变量类型应当如何定义?
image.png

阅读 345
2 个回答

拆分嘛,嵌套太多层或者感觉乱的时候就拆成多个类不用内部类了,改起来也不会眼花缭乱

public class User2
{
    private Username username;

    private Password password;

    //getter、setter方法省略
}
public class Username
{
    private boolean __v_isShallow;

    private Dep dep;

    private boolean __v_isRef;

    private String _rawValue;

    private String _value;

    //getter、setter方法省略
}
public class Password
{
    private boolean __v_isShallow;

    private Dep dep;

    private boolean __v_isRef;

    private String _rawValue;

    private String _value;

    //getter、setter方法省略
}
public class Dep
{
    private int w;

    private int n;

    //getter、setter方法省略
}

如果你想实体类属性和json字符串的key值不必一一对应,可以用@JsonProperty注解标在对应的实体类属性上,标在getter和setter上也是可以的(如果要标就一起标上)

pojo/Account.java

用内部类配合lombok

package com.xxx.pojo;

import lombok.Data;

@Data
public class Account {

    private Field username;
    private Field password;
}

@Data
class Dep {

    private int w;
    private int n;
}

@Data
class Field {

    private boolean __v_isShallow;
    private Dep dep;
    private boolean __v_isRef;
    private String _rawValue;
    private String _value;
}
推荐问题
宣传栏