springboot~ObjectMapper~dto到entity的自動賦值

來源:互聯網
上載者:User

標籤:ram   tor   .json   使用   出現   pat   target   ade   value   

實體與Dto自動賦值

在開發的過程中,實體之間相互賦值是很正常的事,但是我們一般的方法都通過set和get方法來進行的,如果要賦值的欄位少那還行,但是需要賦值的欄位超過10個,那就是個災難,你會看到整屏代碼中全是set和get方法。

  1. 兩個實體屬性欄位幾乎完全相同
  2. 兩個字型有部分欄位相同
  3. 源實體只有部分欄位賦值,目標實體有完整的值
第一種情況

對於第1點來說,我們用到最多的就是entity和dto之間的轉換了,這個我們可以使用Spring的工具類BeanUtils來解決,這裡要注意的一點是,==第一個參數是源,第二個參數是目標==。

import org.springframework.beans.BeanUtils;BeanUtils.copyProperties(origin, target);
第二種情況

但是對於第2點來說,就沒有那麼簡單了,再使用BeanUtils已經不能滿足我們的需要了。
我們可以使用jackson的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));

我們總結一下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的值將會過濾掉只有預設值的屬性
第三種情況

本情況主要對於從dto到entity轉換過程中出現 ,比如一個put操作,前端可能只修改某幾個屬性,而在後端處理時也只希望處理這幾個被賦值的屬性,這時我們使用下面的方法:

  @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);  }

通過objectMapper的使用,確實讓我們少寫很多重複的代碼。

springboot~ObjectMapper~dto到entity的自動賦值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.