0

I need to create a DTO that will be sent to API in JSON format. Three classes are shown below. The idea is to create a class with all variables that appear in entities and to make a class that creates entities using certain filters. All entities are with 3 elements (2 Strings and 1 Arraylist - attributes) I'm having trouble filtering unnecessary ones.

package proj.model;

import com.fasterxml.jackson.annotation.JsonProperty;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;

public class BaseDTO {
    @NotNull
    @JsonProperty("id")
    private String id;
    @NotNull
    @JsonProperty("type")
    private String type;

    @JsonProperty
    ArrayList<AttDTO> attributes;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public BaseDTO(String id, String type, ArrayList<AttDTO> attributes)
 {
        this.id = id;
        this.type = type;
        this.attributes = attributes;
    }
}

I've listed every attribute for entity shown below:

package proj.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

public class AttDTO<String> {
    @JsonProperty("name")
    private String aname;
    @JsonProperty("type")
    private String atype;
    @JsonProperty("value")
    private String avalue;

    private String aid;
    private String alabel;
    private int intvalue;

    public String getAname() {
        return aname;
    }

    public void setAname(String aname) {
        this.aname = aname;
    }

    public String getAtype() {
        return atype;
    }

    public void setAtype(String atype) {
        this.atype = atype;
    }

    public String getAvalue() {
        return avalue;
    }

    public void setAvalue(String avalue) {
        this.avalue = avalue;
    }

    public String getAid() {
        return aid;
    }

    public void setAid(String aid) {
        this.aid = aid;
    }

    public String getAlabel() {
        return alabel;
    }

    public void setAlabel(String alabel) {
        this.alabel = alabel;
    }

    public int getIntvalue() {
        return intvalue;
    }

    public void setIntvalue(int intvalue) {
        this.intvalue = intvalue;
    }

    public AttDTO(String aname, String atype, String avalue) {
        this.aname = aname;
        this.atype = atype;
        this.avalue = avalue;

    }
}

Note: used main to test here. I need to have a particular output as API is requiring which is shown in the end.

package proj.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;


import java.util.ArrayList;

public class EntityCreator {

    static ArrayList<AttDTO> attributes = new ArrayList<AttDTO>();
    BaseDTO obj = new BaseDTO("APMManufacturingTasksDefReq1","APMManufacturingTasksDefReq",attributes);







    @SneakyThrows
    public static void main(String[] args) {
        BaseDTO obj = new BaseDTO("APMManufacturingTasksDefReq1","APMManufacturingTasksDefReq", attributes);
        attributes.add(new AttDTO("publisherId","Text","APM"));

        ObjectMapper mapper = new ObjectMapper();

        String json = mapper.writeValueAsString(obj);

        System.out.print(json);
    }
}

Output that I get:

{
  "attributes": [
    {
      "aid": null,
      "alabel": null,
      "intvalue": 0,
      "name": "publisherId",
      "type": "Text",
      "value": "APM"
    }
  ],
  "id": "APMManufacturingTasksDefReq1",
  "type": "APMManufacturingTasksDefReq"
}

Output that I need:

{
  "id":"APMManufacturingTasksDefReq1",
  "type":"APMManufacturingTasksDefReq",
  "attributes":[
    {
      "name":"publisherId",
      "type":"Text",
      "value":"APM"
    }
  ]
}

I can't filter what I need and I tried to find a solution in ordering the output as it is shown in what output I need to get.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
V.T
  • 7
  • 5
  • 2
    Have you checked below https://stackoverflow.com/questions/58228555/what-is-use-of-the-annotation-jsonignore – Sagar Kharab Feb 21 '23 at 09:36
  • Yes I did. But what if I need to create another entity with ignored attributes? Solution that I'm seeing is creating more classes what I'm opting when I run out of ideas – V.T Feb 21 '23 at 10:13

1 Answers1

1

Have you tried this :

Ignoring new fields on JSON objects using Jackson

Example (try maybe the @JsonIgnoreProperties on the attributs)

package proj.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonIgnoreProperties(ignoreUnknown = true)
public class AttDTO<String> {
    @JsonProperty("name")
    private String aname;
    @JsonProperty("type")
    private String atype;
    @JsonProperty("value")
    private String avalue;

    private String aid;
    private String alabel;
    private int intvalue;

    public String getAname() {
        return aname;
    }

    public void setAname(String aname) {
        this.aname = aname;
    }

    public String getAtype() {
        return atype;
    }

    public void setAtype(String atype) {
        this.atype = atype;
    }

    public String getAvalue() {
        return avalue;
    }

    public void setAvalue(String avalue) {
        this.avalue = avalue;
    }

    public String getAid() {
        return aid;
    }

    public void setAid(String aid) {
        this.aid = aid;
    }

    public String getAlabel() {
        return alabel;
    }

    public void setAlabel(String alabel) {
        this.alabel = alabel;
    }

    public int getIntvalue() {
        return intvalue;
    }

    public void setIntvalue(int intvalue) {
        this.intvalue = intvalue;
    }

    public AttDTO(String aname, String atype, String avalue) {
        this.aname = aname;
        this.atype = atype;
        this.avalue = avalue;

    }
}
Mael
  • 56
  • 4
  • I've tried it and it works for that entity. However for other ones where I need those properties It's somewhat different story. I don't need them ignored. What about sorting the elements that appear in a particular way? Still get attributes first, but need them last. – V.T Feb 21 '23 at 10:32
  • Have you tried @JsonProperty (Access=readonly or write only)? – Sagar Kharab Feb 21 '23 at 10:42