今天做一个测试,一个类中嵌套一个集合类,对最外层的类使用logbok,对内部类没使用。最后导致Json串转java类之后内部类没有属性值。具体如下
示例json串
{ "displayValue": test", "empty": false, "values": [ { "empty": false, "id": "xxx", "name": "test" } ] }
示例代码
import lombok.Data; import java.io.Serializable; import java.util.List; @Data public class NGSearchFilterFiledCheeck implements Serializable{ private String displayValue; private String empty; private List<Values> values; public static class Values implements Serializable { private String empty; private String id; private String name; } }
import com.alibaba.fastjson.JSON; import com.souche.thriver.bean.NGSearchFilterFiledCheck; import java.util.*; /** * @author fanlongfei * @date 2021年09月14日 6:30 下午 */ public class DemoTest { public static void main(String[] args) { String jsonStr = "{\n" + " \"displayValue\": \"test\",\n" + " \"empty\": false,\n" + " \"values\": [\n" + " {\n" + " \"empty\": false,\n" + " \"id\": \"xxx\",\n" + " \"name\": \"test\"\n" + " }\n" + " ]\n" + " }"; NGSearchFilterFiledCheck filter = JSON.parseObject(jsonStr, NGSearchFilterFiledCheck.class); System.out.println(JSON.toJSONString(filter)); } }
运行结果
类中的values属性为空
{"displayValue":"联盟测试店铺主店对内","empty":"false","values":[{}]}
经过排查是内部类没有加@Data注解造成的,加上之后就运行ok
import lombok.Data; import java.io.Serializable; import java.util.List; @Data public class NGSearchFilterFiledCheeck implements Serializable{ private String displayValue; private String empty; private List<Values> values; @Data public static class Values implements Serializable { private String empty; private String id; private String name; } }