Automatic assignment of Springboot~objectmapper~dto to entity

Source: Internet
Author: User

Entities and DTOs automatically assign values

In the process of development, it is normal for entities to assign values to each other, but our general approach is done through the set and get methods, if the field to be assigned is less, but the number of fields that need to be assigned is more than 10, it is a disaster and you will see that the entire screen code is full of set and get methods.

    1. Two entity attribute fields are almost identical
    2. Two fonts are part of the same field
    3. The source entity has only partial field assignments, and the target entity has a full value
First case

For the 1th, the most we use is the conversion between the entity and the DTO, which we can use Spring's tool class beanutils to solve, one thing to note is that = = The first parameter is the source, the second parameter is the target = =.

import org.springframework.beans.BeanUtils;BeanUtils.copyProperties(origin, target);
The second case

But for the 2nd, it's not that simple, and the use of beanutils is not enough to meet our needs.
We can use Jackson's objectmapper.

import com.fasterxml.jackson.databind.DeserializationFeature;import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.ObjectReader;import com.jd.fastjson.JSON;ObjectMapper objectMapper = new ObjectMapper();//配置该objectMapper在反序列化时,忽略目标对象没有的属性。凡是使用该objectMapper反序列化时,都会拥有该特性。objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);//读入需要更新的目标实体ObjectReader objectReader = objectMapper.readerForUpdating(target);//将源实体的值赋值到目标实体上objectReader.readValue(JSON.toJSONString(source));

Let's summarize the filter parameters of Objectmapper:

 /* 通过该方法对mapper对象进行设置,所有序列化的对象都将按改规则进行系列化 Include.Include.ALWAYS 默认 Include.NON_DEFAULT 属性为默认值不序列化 Include.NON_EMPTY 属性为 空(“”)  或者为 NULL 都不序列化 Include.NON_NULL 属性为NULL 不序列化 */    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);    String outJson = objectMapper.writeValueAsString(productDetail);//上面代码里,outJson的值将会过滤掉只有默认值的属性
The third case

This is mainly for the process from DTO to entity conversion, such as a put operation, the front end may only modify a few properties, and in the back-end processing only want to deal with these assigned properties, then we use the following method:

  @RequestMapping(value = "/{id}", method = RequestMethod.PUT)  public HttpEntity update(@PathVariable int id, @RequestBody ProductDetail productDetail)      throws IOException {    ProductDetail existing = repository.findById(id).get();    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);    String outJson = objectMapper.writeValueAsString(productDetail);    ObjectReader objectReader = objectMapper.readerForUpdating(existing);    objectReader.readValue(outJson);    repository.save(existing);    return new ResponseEntity<>(existing, HttpStatus.ACCEPTED);  }

Through the use of objectmapper, really let us write a lot less repetitive code.

Automatic assignment of Springboot~objectmapper~dto to entity

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.