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.