ObjectMapper4Tx.java 930 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package com.persagy.bdtp.adm.datatx;
  2. import com.fasterxml.jackson.annotation.JsonInclude;
  3. import com.fasterxml.jackson.core.JsonProcessingException;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import com.persagy.bdtp.adm.entity.db.AdmObject;
  6. import org.springframework.stereotype.Component;
  7. /**
  8. * 针对数据传输的ObjectMapper,过滤字段
  9. */
  10. @Component
  11. public class ObjectMapper4Tx {
  12. private ObjectMapper mapper;
  13. public ObjectMapper4Tx() {
  14. this.mapper = new ObjectMapper();
  15. configMapper();
  16. }
  17. private void configMapper(){
  18. mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
  19. //设置实体的传输字段
  20. mapper.addMixIn(AdmObject.class, ObjectForTx.class);
  21. }
  22. public ObjectMapper mapper() {
  23. return mapper;
  24. }
  25. public String toJson(Object obj){
  26. try{
  27. return mapper.writeValueAsString(obj);
  28. }catch (JsonProcessingException e){
  29. throw new RuntimeException(e);
  30. }
  31. }
  32. }