Answer the question
In order to leave comments, you need to log in
How to form a data structure and return it to the client?
Hello. I need such a data structure on the client, which I need to get from the PostgreSQL database:
{
id: nanoid(),
numberOperation: '010',
nameOperation: 'Токарная',
workshop: 6465,
area: 7,
OO: true,
OTK: false,
PZ: true,
KPS: true,
transition: [
{
id: nanoid(),
nameTransition: 'Закрепить деталь',
executor: [
{
id: nanoid(),
nameExecutor: '4784',
tsht: '4',
tpz: '9',
test: '7',
tshtCalculated: '',
tpzCalculated: '',
testCalculated: '',
kvr: '973'
},
]
},
],
}
Answer the question
In order to leave comments, you need to log in
Good afternoon.
Please tell me, do I understand correctly that you have a table with data, as well as the entities themselves (entity) and you want to return the result received from the database to the client?
Those. I'm interested in the following: db first or code first?
If you have code first, then you have entities and you need to create additional. classes (DTOs). Further, the received data from the database should be mapped into DTO.
For mapping (after the DTOs are created), you can use the add. either: ModelMapper, MapStruct, or interface Converter<S,T>
If you want to create a dto based on the json file you have, you can use an online service or any of them.
Service - https://www.jsonschema2pojo.org/
By the way, are you sure that you have json data in your question?
Here is the json:
{
"id": 1,
"numberOperation": "010",
"nameOperation": "Токарная",
"workshop": 6465,
"area": 7,
"OO": true,
"OTK": false,
"PZ": true,
"KPS": true,
"transition": [
{
"id": 1,
"nameTransition": "Закрепить деталь",
"executor": [
{
"id": 1,
"nameExecutor": "4784",
"tsht": "4",
"tpz": "9",
"test": "7",
"tshtCalculated": "",
"tpzCalculated": "",
"testCalculated": "",
"kvr": "973"
}, ]
},
]
}
-----------------------------------com.example.Example.java-----------------------------------
package com.example;
import java.util.List;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"id",
"numberOperation",
"nameOperation",
"workshop",
"area",
"OO",
"OTK",
"PZ",
"KPS",
"transition"
})
@Generated("jsonschema2pojo")
public class Example {
@JsonProperty("id")
public Long id;
@JsonProperty("numberOperation")
public String numberOperation;
@JsonProperty("nameOperation")
public String nameOperation;
@JsonProperty("workshop")
public Long workshop;
@JsonProperty("area")
public Long area;
@JsonProperty("OO")
public Boolean oo;
@JsonProperty("OTK")
public Boolean otk;
@JsonProperty("PZ")
public Boolean pz;
@JsonProperty("KPS")
public Boolean kps;
@JsonProperty("transition")
public List<Transition> transition = null;
}
-----------------------------------com.example.Executor.java-----------------------------------
package com.example;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"id",
"nameExecutor",
"tsht",
"tpz",
"test",
"tshtCalculated",
"tpzCalculated",
"testCalculated",
"kvr"
})
@Generated("jsonschema2pojo")
public class Executor {
@JsonProperty("id")
public Long id;
@JsonProperty("nameExecutor")
public String nameExecutor;
@JsonProperty("tsht")
public Long tsht;
@JsonProperty("tpz")
public Long tpz;
@JsonProperty("test")
public Long test;
@JsonProperty("tshtCalculated")
public String tshtCalculated;
@JsonProperty("tpzCalculated")
public String tpzCalculated;
@JsonProperty("testCalculated")
public String testCalculated;
@JsonProperty("kvr")
public Long kvr;
}
-----------------------------------com.example.Transition.java-----------------------------------
package com.example;
import java.util.List;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"id",
"nameTransition",
"executor"
})
@Generated("jsonschema2pojo")
public class Transition {
@JsonProperty("id")
public Long id;
@JsonProperty("nameTransition")
public String nameTransition;
@JsonProperty("executor")
public List<Executor> executor = null;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question