简体   繁体   English

Java只返回超类的属性

[英]Java return only attributes of superclass

I am trying to create an API controller with java and spring boot. 我正在尝试使用java和spring boot创建一个API控制器。 I want to return specific objects as JSON from specific API endpoints, nothing special so far. 我想从特定的API端点返回特定对象作为JSON,到目前为止没什么特别的。 Now I have a child class A and a parent class B. B is abstract and A is not abstract, but A contains more attributes than B. I have to pass the attributes of the superclass from A to the API endpoint, but when I cast the an object to an object of the superclass the JSON code contains always the attributes from A. 现在我有一个子类A和一个父类B.B是抽象的,A不是抽象的,但A包含的属性多于B.我必须将超类的属性从A传递给API端点,但是当我强制转换时作为超类对象的对象,JSON代码始终包含来自A的属性。

Is there another way to prevent this than setting the not relevant parts to null or empty elements? 是否有另一种方法可以防止这种情况,而不是将不相关的部分设置为null或空元素?


EDIT: 编辑:

I have the following Code: 我有以下代码:

@Entity
public abstract class B {
private String name;
private String description;

/* GETTERS AND SETTERS */
}

@Entity
public class A extends B {
private String moreInformation;

/* GETTERS AND SETTERS */
}


@RestController
@RequestMapping("/api/v1/")
public class ApiController {

    @RequestMapping(value = "/Bs", method = RequestMethod.GET)
    public ResponseEntity<List<B>> getBs() {
      List<A> As = DAO.getAllRelevantAsAsList();
      List<B> Bs = new ArrayList<>();
      Bs.appendAll(As);
      return new ResponseEntity<>(Bs, HttpStatus.OK);
    }

    /* DIFFERENT API ENDPOINTS */
}

The JSON Code generated by the API Endpoint looks like this: API端点生成的JSON代码如下所示:

[
  {
    "name": "CONTENT",
    "description": "CONTENT",
    "moreInformation": "CONTENT"
   },
   ...
]

I found a possible solution for this problem by annotating the attributes of class A with @JsonIgnore. 我通过使用@JsonIgnore注释类A的属性找到了解决此问题的可能方法。 But the main question of this thread is not limited to this special implementation: Is it possible to get only the attributes of the parent class A without annotating the subclass? 但是这个线程的主要问题不仅限于这个特殊的实现:是否有可能只获取父类A的属性而不注释子类?

I believe your options are: 我相信你的选择是:

  • @JsonIgnore on the fields you don't want serialized in A. @JsonIgnore关于你不想在A中序列化的字段
  • Configure the Jackson ObjectMapper to only serialize annotated fields and then annotate the fields you do want serialized in B with @JsonProperty . 将Jackson ObjectMapper配置为仅序列化带注释的字段 ,然后使用@JsonProperty注释要在B中序列化的字段。
  • Copy the A instances returned from getAllRelevantAsAsList() into a new object type which doesn't have the fields (either an anonymous implementation of B which delegates to a, or create a new child class that extends B but doesn't have new fields and has a copy ctor for an A instance. 将从getAllRelevantAsAsList()返回的A实例复制到一个没有字段的新对象类型中(B的匿名实现委托给a,或者创建一个扩展B但没有新字段的新子类,有一个A实例的副本。

     List<B> Bs = DAO.getAllRelevantAsAsList() .stream() .map(a -> new B() { public String getName() { a.getName(); } public void setName(String name) { a.setName(name); } /** other delegated getters and setters **/ }).collect(Collectors.toList()); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM