Java Spring - HttpClient 在序列化 ResponseEntity 时分配一个额外的字段
Java Spring - The HttpClient assigns an extra field when serializing a ResponseEntity
简而言之,问题是我有以下 HttpResponse 正文:
{"mammalList":[{"id":11,"details":{"id":3,"name":"Kangaroo","description":"Example",
"image":null},"age":40,"hasHooves":true,"hasPlacenta":false,"name":"Kangaroo"}]}
最后还有一个字段"name":"Kangaroo"
a 哺乳动物是:
Mammal(Long id, Details details, int age, boolean hasHooves, boolean hasPlacenta)
详细信息为 Details(Long id, String name, String description, String image)
假设我有一个 Get 映射 animals/all/mammals
我有一个 get 方法,它首先创建一个 HttpRequest
public static <E> E get(String path, Class<E> responseType) {
// Build HTTP request
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(host + "/" + path))
.setHeader("Content-Type", "application/json").GET().build();
然后尝试获得响应
HttpResponse<String> httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString());
/* 这是客户端 - private static final HttpClient client = HttpClient.newHttpClient(); */
使用 bean 和注释,我得到了连接到 animals/all/mammals
的方法
在方法的末尾 return 以下内容:
return ResponseEntity.ok(new MammalResponse(responseList));
(MammalResponse 是一个 class,仅包含哺乳动物列表、构造函数、getter 和 setter)
方法末尾的调试器显示如下:(这是预期的)
responseList = {ArrayList@10790} size = 1
0 = {Mammal@10792}
age = 40
hasHooves = true
hasPlacenta = false
id = {Long@10793} 11
details = {Details@10794}
id = {Long@10795} 3
name = "Kangaroo"
description = "Example"
image = null
问题是,当我传递到 get 方法的下一行时,我有以下 HttpResponse 主体:
{"mammalList":[{"id":11,"details":{"id":3,"name":"Kangaroo","description":"Example",
"image":null},"age":40,"hasHooves":true,"hasPlacenta":false,"name":"Kangaroo"}]}
最后还有一个字段"name":"Kangaroo"
Mammal 对象没有 name 属性,所以它不能被反序列化,一切都会走下坡路。
知道为什么它会在序列化的 Mammal 字符串的末尾添加额外的属性吗?我如何才能阻止这种情况发生?
编辑:如果我将更多哺乳动物添加到数据库中,那么每个哺乳动物都会在 mammalList 中,其详细信息对象中的 name
也会放在最后。
您对 "which service" 的描述远未完成。是你们自己的服务吗?
在任何情况下,快速解决方法是使用 @JsonIgnoreProperties 注释您的哺乳动物 class。
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
@JsonIgnoreProperties
class { ... }
使用该注释,未知字段将不再使您的程序运行 "downhill"。
感谢@Ţîgan Ion,它已修复!
我在 Mammal 中有一个 getName() 方法,这就是名称被放置两次的原因。当我将其注释掉时,从数据库中获取哺乳动物按预期工作。
再次感谢@Ţîgan Ion。如果您将其写为答案,我会将其标记为正确答案。
简而言之,问题是我有以下 HttpResponse 正文:
{"mammalList":[{"id":11,"details":{"id":3,"name":"Kangaroo","description":"Example",
"image":null},"age":40,"hasHooves":true,"hasPlacenta":false,"name":"Kangaroo"}]}
最后还有一个字段"name":"Kangaroo"
a 哺乳动物是:
Mammal(Long id, Details details, int age, boolean hasHooves, boolean hasPlacenta)
详细信息为 Details(Long id, String name, String description, String image)
假设我有一个 Get 映射 animals/all/mammals
我有一个 get 方法,它首先创建一个 HttpRequest
public static <E> E get(String path, Class<E> responseType) {
// Build HTTP request
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(host + "/" + path))
.setHeader("Content-Type", "application/json").GET().build();
然后尝试获得响应
HttpResponse<String> httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString());
/* 这是客户端 - private static final HttpClient client = HttpClient.newHttpClient(); */
使用 bean 和注释,我得到了连接到 animals/all/mammals
的方法
在方法的末尾 return 以下内容:
return ResponseEntity.ok(new MammalResponse(responseList));
(MammalResponse 是一个 class,仅包含哺乳动物列表、构造函数、getter 和 setter)
方法末尾的调试器显示如下:(这是预期的)
responseList = {ArrayList@10790} size = 1
0 = {Mammal@10792}
age = 40
hasHooves = true
hasPlacenta = false
id = {Long@10793} 11
details = {Details@10794}
id = {Long@10795} 3
name = "Kangaroo"
description = "Example"
image = null
问题是,当我传递到 get 方法的下一行时,我有以下 HttpResponse 主体:
{"mammalList":[{"id":11,"details":{"id":3,"name":"Kangaroo","description":"Example",
"image":null},"age":40,"hasHooves":true,"hasPlacenta":false,"name":"Kangaroo"}]}
最后还有一个字段"name":"Kangaroo" Mammal 对象没有 name 属性,所以它不能被反序列化,一切都会走下坡路。
知道为什么它会在序列化的 Mammal 字符串的末尾添加额外的属性吗?我如何才能阻止这种情况发生?
编辑:如果我将更多哺乳动物添加到数据库中,那么每个哺乳动物都会在 mammalList 中,其详细信息对象中的 name
也会放在最后。
您对 "which service" 的描述远未完成。是你们自己的服务吗?
在任何情况下,快速解决方法是使用 @JsonIgnoreProperties 注释您的哺乳动物 class。
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
@JsonIgnoreProperties
class { ... }
使用该注释,未知字段将不再使您的程序运行 "downhill"。
感谢@Ţîgan Ion,它已修复!
我在 Mammal 中有一个 getName() 方法,这就是名称被放置两次的原因。当我将其注释掉时,从数据库中获取哺乳动物按预期工作。
再次感谢@Ţîgan Ion。如果您将其写为答案,我会将其标记为正确答案。