一、概述
有时候由于业务逻辑复杂,需要在get请求参数列表中传递一个对象的引用对象数据。
如果直接将一个对象的JSON字符串传递到Spring Boot后台,Spring Boot是不会自动帮你将其序列化为Java对象的,会抛出一个ClassCastException
。
二、解决方法
假设有如下请求定义以及实体类定义
实体类定义
public class ClassTest1 {
private Long id;
private ClassTest2 classTest2;
}
public class ClassTest2 {
private String title;
}
请求定义
@GetMapping("/test")
public TableDataInfo test(ClassTest1 classTest1){
// ...
}
那么如何在请求中向classTest2
传递title
参数呢?
请求字符串示例
// 根据上述写法,便可以将参数赋值给classTest2.title
/test?id=1&classTest2.title=我是标题
// 此处为了防止URL转码错误,应对请求键值对的值进行URLencode操作
/test?id=1&classTest2.title=%E6%88%91%E6%98%AF%E6%A0%87%E9%A2%98