Java Spring-序列化ResponseEntity时HttpClient分配一个额外的字段

问题描述 投票:0回答:1

总之,问题是我的HttpResponse主体如下:

{"mammalList":[{"id":11,"details":{"id":3,"name":"Kangaroo","description":"Example",
"image":null},"age":40,"hasHooves":true,"hasPlacenta":false,"name":"Kangaroo"}]}

最后,还有另一个字段“ name”:“ Kangaroo”] >>

哺乳动物是: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());

/ *这是客户端-私有静态最终HttpClient客户端= HttpClient.newHttpClient(); * /

[使用bean和注释,我得到了与animals/all/mammals相关的方法在该方法的末尾返回以下内容:

    return ResponseEntity.ok(new MammalResponse(responseList));

((MammalResponse是仅包含哺乳动物列表,构造函数,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”

该哺乳动物对象没有名称属性,因此无法进行反序列化,一切都变得艰难。

任何想法为什么要在序列化的哺乳动物字符串的末尾添加该额外属性,以及如何阻止这种情况的发生?

编辑:如果我将更多的哺乳动物添加到数据库中,则每个都位于哺乳动物列表中,并且其Details对象中的name也放在末尾。

简而言之,我对HttpResponse具有以下正文:{“ mammalList”:[{“ id”:11,“ details”:{“ id”:3,“ name”:“ Kangaroo”,“ description “:” Example“,” image“:null},” age“:40,” hasHooves“:true,” ...

java spring serialization httprequest httpresponse
1个回答
0
投票

您对要获取HTTPResponse的“哪种服务”的描述还远远不够。这是您自己的服务吗?

© www.soinside.com 2019 - 2024. All rights reserved.