In our daily development, the mutual conversion between dto, vo and entity is very frequent. This article will briefly record my conversion method in daily development.
Before this, let's briefly describe dto, vo, entity
DTO: It is most common that we define data transfer objects (DTOs) in development to receive parameters passed by the front end.
VO: In normal development, VO (view object) is generally defined to encapsulate the data returned to the front end.
Entity: In our development, Entity is suitable for one of the core components representing persistent objects. It usually corresponds directly to tables in the database and is used to map objects recorded by the database to the application
In our usual additions, deletions, modifications and searches, we simply put it
Just like the new operation, dto is generally used to accept parameters passed by the front-end, then convert dto to Entity, then set some fields such as creation time, and then insert the Entity entity object data into the database.
In query operation, first query the data, namely Entity, from the database, then convert Entity into VO view data, and then return it to the front end.
In my daily development, I will use the following encapsulated tool classes to convert between dto, vo, and entity. The code is as follows:
First introduce dependencies:
<!-- JSON Parsers and generators --> <dependency> <groupId></groupId> <artifactId>fastjson</artifactId> <version>1.2.83</version> </dependency>
Encapsulate the JSONUtil class
package ; import ; import ; import ; import ; import ; import ; import ; import ; public class JsonUtil { public JsonUtil() { } public static List listToJsonField(List lists) { String jsonStr = (lists, new SerializerFeature[]{}); List list = (List)(jsonStr, ); return list; } public static Map<String, Object> entityToMap(Object object) { String jsonStr = (object); Map<String, Object> map = (Map)(jsonStr, new TypeReference<Map<String, Object>>() { }, new Feature[0]); return map; } public static Map<String, String> entityToMaps(Object object) { String jsonStr = (object); Map<String, String> map = (Map)(jsonStr, new TypeReference<Map<String, String>>() { }, new Feature[0]); return map; } public static Map<String, Object> stringToMap(String object) { Map<String, Object> map = (Map)(object, new TypeReference<Map<String, Object>>() { }, new Feature[0]); return map; } public static <T> T getJsonToBean(String jsonData, Class<T> clazz) { return (jsonData, clazz); } public static JSONArray getJsonToJsonArray(String json) { return (json); } public static <T> JSONArray getListToJsonArray(List<T> list) { return (getObjectToString(list)); } public static String getObjectToString(Object object) { return (object, new SerializerFeature[]{}); } public static String getObjectToStringDateFormat(Object object, String dateFormat) { return (object, dateFormat, new SerializerFeature[]{}); } public static <T> List<T> getJsonToList(String jsonData, Class<T> clazz) { return (jsonData, clazz); } public static List<Map<String, Object>> getJsonToListMap(String jsonData) { return (List)(jsonData, new TypeReference<List<Map<String, Object>>>() { }, new Feature[0]); } public static List<Map<String, Object>> getJsonToList(JSONArray jsonArray) { return (List)((jsonArray), new TypeReference<List<Map<String, Object>>>() { }, new Feature[0]); } public static <T> T getJsonToBean(Object dto, Class<T> clazz) { return (getObjectToString(dto), clazz); } public static <T> List<T> getJsonToList(Object dto, Class<T> clazz) { return (getObjectToString(dto), clazz); } }
The method I usually use most often in daily use is to convert the dto, vo, and entity objects. A simple example is as follows:
Example:
The front-end passes DTO (name, age, birthTime) over, and then we turn it into VO (ame, age, birthTime, creatorTime) and return.
DTO class:
package ; import ; import ; @Data public class TestJsonForm { /** Name **/ @JsonProperty("name") private String name; /** age **/ @JsonProperty("age") private Integer age; /** Date of birth **/ @JsonProperty("birthTime") private String birthTime; }
VO:
package ; import ; import ; import ; import ; @Data public class TestJsonVo { @JSONField(name = "name") private String name; @JSONField(name = "age") private Integer age; @JSONField(name = "birthTime") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") private Date birthTime; @JSONField(name = "birthTime") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") private Date creatorTime; }
Controller:
package ; import ; import ; import ; import ; import .*; import ; @RestController @RequestMapping("/json") public class TestJsonController { @PostMapping("/save") public ActionResult uploadTest(@RequestBody TestJsonForm testJsonForm) { // Convert dto to vo TestJsonVo vo = (testJsonForm, ); (new Date()); return (vo); } }
@JsonProperty
You can see that in DTO, I used the @JsonProperty annotation. This is very simple. In fact, it identifies what the parameter field is passed by the front end, which allows the parameter field accepted by our backend to be inconsistent with the field passed by the front end. If it is inconsistent, our backend can use @JsonProperty for mapping.
For the name field we defined above, if the field passed by the front end is cusname, we can use such annotation @JsonProperty ("cusname") on the name field.
@JSONField
Similarly, I use @JSONField annotation in VO. The above example is to convert DTO to VO. @JSONField in VO corresponds to fields in DTO.
In fact, in our daily development, as long as the fields are consistent, we do not need to use these two annotations at all.
The @JsonFormat annotation is also used in the VO above. This is formatted time. Convert the Date type to the String type yyyy-MM-dd HH:mm:ss to return to the front end.
The above is the detailed explanation of the mutual conversion of DTO/VO/Entity in SpringBoot. For more information on the mutual conversion of SpringBoot DTO VO Entity, please follow my other related articles!