有一个模型类“ Student”,它扩展了ResourceSupport并包含属性名称(字符串),age(int),marks(int)和class_leader(Student)。我定义了两个视图,basicview(用于名称和年龄)和fullview(扩展了基本视图,并用于mark和class_leader)。当我调用一个api来获得所有学生的详细信息时,我希望每个学生都有一个完整的视图,并且具有该学生班长的基本视图。为了添加该功能,我为class_leader属性并入了JSONSerializer并按预期工作。但是我现在面临的唯一问题是class_leader,链接不是以HAL格式出现的。如果我不使用正确的HAL格式的序列化器全视图,则会显示。
尝试使用@EnableHypermediaSupport(type = [(EnableHypermediaSupport.HypermediaType.HAL)])注释控制器类
这是我正在使用的序列化程序代码。
class StudentSerializer: JsonSerializer<Any>() {
@Throws(IOException::class, JsonGenerationException::class)
override fun serialize(value: Any?, jgen: JsonGenerator?, provider:
SerializerProvider?) {
var objectMapper = jacksonObjectMapper()
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
//objectMapper.registerModule( Jackson2HalModule())
objectMapper.writerWithView(Views.BaiscView :: class.java).writeValue(jgen, value)
}
}
Expected Result:
{
"_embedded": {
"studentList": [
{
"roll_no":1,
"name": "student1",
"age": 10,
"marks":47,
"class_leader": {
"roll_no":2,
"name": "student2",
"_links": {
"self": {
"href": "http://localhost:9090/class/students/2"
}
}
},
"_links": {
"studentdetail": {
"href": "http://localhost:9090/class/students/1"
}
}
},
...
]
},
"_links": {
"self": {
"href": "http://localhost:9090/class/students/"
}
}
}
Actual Result:
{
"_embedded": {
"studentList": [
{
"roll_no":1,
"name": "student1",
"age": 10,
"marks":47,
"class_leader": {
"roll_no":2,
"name": "student2",
"links": [
{
"rel": "self",
"href":"http://localhost:9090/class/students/2"
}
]
},
"_links": {
"studentdetail": {
"href": "http://localhost:9090/class/students/1"
}
}
},
...
]
},
"_links": {
"self": {
"href": "http://localhost:9090/class/students/"
}
}
}