简体   繁体   中英

Sharing common variables in classes Java

The following question might be too naive, but please help me find a solution to this.

I'm working on Java RESTful APIs, where I'm sending the response format in JSON format. As there are multiple APIs to be designed, I thought of keeping some common variables in Abstract Classes and reuse wherever possible. Below are 2 abstract classes I designed.

public @Data abstract class AbstractEntityWebServResDTO<D> {

    // Attributes
    //////////////
    protected Date createdDt;
    protected String createdBy;
    protected Date updatedDt;
    protected String updatedBy;

}
public @Data abstract class AbstractModifyWebServReqDTO<D> {

    // Attributes
    //////////////
    protected boolean delete;
    protected boolean modify;

}

Some examples of response DTOs.

public @Data class ResponseFormat1 extends AbstractEntityWebServResDTO<ResponseFormat1>{

    protected String response1Data;

}

This would produce JSON:

{
  "response1Data": "SOME DATA",
  "createdDt": "2019-11-02 23:33:23",
  "createdBy": "USERID",
  "updatedDt": "2019-11-02 23:33:23",
  "updatedBy": "USERID"
}

Another Example of response DTO

public @Data class ResponseFormat2 extends AbstractModifyWebServReqDTO<ResponseFormat2>{

    protected String response2Data;

}

This would produce JSON:

{
  "response2Data": "SOME DATA",
  "delete": true,
  "modify": false
}

As shown above, the classes are extending different Abstract classes that are relevant to them. But in some cases, I would require to use all the variables present in both abstract classes. As we cannot extend 2 abstract classes how can I achieve this?

Solutions I thought of:

  • Creating a single abstract class with all variables and use it. But as I'm using FasterXML to convert the DTO to JSON format. It would bring all unwanted variables in the response format.
  • Extending Abstract with another Abstract class. The extending class would produce the same JSON format with unwanted variables in it.
  • Creating interfaces with variables - But the variables inside interfaces should be either static or final.

Please help to highlight if any solution available to avoid sending unwanted keys in the JSON?

use composition instead of inheritance

example:

public class CreateInfo {

          protected String updatedDt;
          protected String updatedBy;
}



public class UpdateInfo {

          protected Date updatedDt;
          protected String updatedBy;
}


public class FinalModel {

          protected CreateInfo createInfo;
          protected UpdateInfo updateInfo;
}

this way your models are much more flexible than inheritance, using inheritance is not a good practice in most of the cases.

In case where you need properties from both abstract classes use composition and com.fasterxml.jackson.annotation.JsonUnwrapped annotation.

class ResponseFormatAll {
    @JsonUnwrapped
    private AbstractEntityWebServResDTO res;

    @JsonUnwrapped
    private AbstractModifyWebServReqDTO req;
}

Since now, all fields from two abstract classes appear in your response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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