如何使用 WebClient 在 REST 调用中仅发送正文中的超类 [已关闭]

how to send only the superclass in the body off a rest call with webclient [closed]

提问人:0c7 提问时间:11/17/2023 最后编辑:enzo0c7 更新时间:11/17/2023 访问量:28

问:


想改进这个问题吗?通过编辑这篇文章添加详细信息并澄清问题。

2天前关闭。

示例:我有一个 Animal 类和一个 Cat 类来扩展它

public class Animal {   protected String sound;

   public Animal() {   }
 
   public String getSound() {
     return sound;   }
 
   public void setSound(String sound) {
     this.sound = sound;   } }
public class Cat extends Animal {
  private String catHouseNumber;

  public Cat() {
    this.sound = "miau";
  }

  public String getCatHouseNumber() {
    return catHouseNumber;
  }

  public void setCatHouseNumber(String catHouseNumber) {
    this.catHouseNumber = catHouseNumber;
  }
}

我正在使用 animal 类调用 rest api,该对象实际上是一只猫

var response =  webclient.post().uri("/xxx")
                .bodyValue(animal)
                .retrieve()

一切正常,但我在日志中看到它序列化并发送了整个 Cat。

我不需要送猫,只需要动物超类。

有没有办法告诉 Java 只使用超类?

java spring-webclient

评论


答: 暂无答案