1.Java-类型转换公共类

2019年09月27日 19:38 · 阅读(1106) ·

说明

本文代码基于 4.Spring Boot 过滤器 Filter 使用 项目代码进行修改

开发环境

名称 版本
操作系统 Windows 10 X64
JDK JDK1.8(jdk-8u151-windows-x64)
IntelliJ IDEA IntelliJ IDEA 2018.3
Maven Maven 3.6.0

测试类

SuperEntity

基础实体类-SuperEntity

TRbtTestEntity

  1. package com.test.invoice.model;
  2. import com.baomidou.mybatisplus.annotation.TableName;
  3. import com.test.invoice.model.base.SuperEntity;
  4. import io.swagger.annotations.ApiModelProperty;
  5. import lombok.Data;
  6. import lombok.EqualsAndHashCode;
  7. import lombok.ToString;
  8. import lombok.experimental.Accessors;
  9. /**
  10. * t_rbt_test 表对应实体类-TRbtTestEntity
  11. *
  12. * @author:
  13. * @version:
  14. * @date: 2019-08-09 14:25
  15. */
  16. @Data
  17. @EqualsAndHashCode(callSuper = true)
  18. @Accessors(chain = true)
  19. @ToString
  20. @TableName("t_rbt_test")
  21. public class TRbtTestEntity extends SuperEntity {
  22. @ApiModelProperty("名称")
  23. private String name;
  24. @ApiModelProperty("版本号")
  25. private String version;
  26. }

TRbtTestData

  1. package com.test.invoice.data;
  2. import com.alibaba.fastjson.annotation.JSONField;
  3. import io.swagger.annotations.ApiModelProperty;
  4. import lombok.Data;
  5. import lombok.experimental.Accessors;
  6. /**
  7. * TRbtTestData 参数类
  8. *
  9. * @author:
  10. * @version:
  11. * @date: 2019-08-09 08:57
  12. */
  13. @Data
  14. @Accessors(chain = true) //@Accessors 注解用来配置lombok如何产生和显示getters和setters的方法。
  15. public class TRbtTestData {
  16. @ApiModelProperty("id")
  17. @JSONField(name = "id")
  18. private String id;
  19. @ApiModelProperty("名称")
  20. @JSONField(name = "name")
  21. private String name;
  22. @ApiModelProperty("版本号")
  23. @JSONField(name = "version")
  24. private String version;
  25. }

添加对象转换类-MapperUtils

test-invoice-common 项目,添加包 util-mapper-MapperUtils

  1. package com.test.invoice.util.mapper;
  2. import org.dozer.DozerBeanMapperBuilder;
  3. import org.dozer.Mapper;
  4. import org.springframework.util.Assert;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. /**
  8. * 对象转换类
  9. *
  10. * @author:
  11. * @version:
  12. * @date: 2019/3/13 18:13
  13. */
  14. public class MapperUtils {
  15. private static final Mapper MAPPER = DozerBeanMapperBuilder.buildDefault();
  16. public static <T, S> T convert(S source, T target) {
  17. if (source == null) {
  18. return null;
  19. }
  20. Assert.notNull(target, "target instance required");
  21. MAPPER.map(source, target);
  22. return target;
  23. }
  24. public static <T, S> T convert(S source, Class<T> targetClass) {
  25. if (source == null) {
  26. return null;
  27. }
  28. Assert.notNull(targetClass, "targetClass required");
  29. return MAPPER.map(source, targetClass);
  30. }
  31. public static <T, S> List<T> convert(List<S> source, Class<T> targetClass) {
  32. if (source == null) {
  33. return null;
  34. }
  35. List<T> targetList = new ArrayList<>();
  36. for (S s : source) {
  37. T target = MAPPER.map(s, targetClass);
  38. targetList.add(target);
  39. }
  40. return targetList;
  41. }
  42. }

添加测试方法

test-invoice-webTRbtTestController 添加测试方法 TypeConvert()

  1. package com.test.invoice.controller;
  2. import com.google.gson.Gson;
  3. import com.google.gson.GsonBuilder;
  4. import com.test.invoice.data.TRbtTestData;
  5. import com.test.invoice.data.TRbtTestDataParam;
  6. import com.test.invoice.enums.ResponseCode;
  7. import com.test.invoice.model.TRbtTestEntity;
  8. import com.test.invoice.service.consumer.TRbtTestConsumer;
  9. import com.test.invoice.util.mapper.MapperUtils;
  10. import com.test.invoice.vo.ResponseVO;
  11. import io.swagger.annotations.Api;
  12. import io.swagger.annotations.ApiOperation;
  13. import lombok.extern.slf4j.Slf4j;
  14. import org.springframework.web.bind.annotation.*;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. /**
  17. * 消费者,通过 Feign-api 调用
  18. *
  19. * @author:
  20. * @version:
  21. * @date: 2019-08-12 14:49
  22. */
  23. @RestController
  24. @Api(description = "测试框架系统控制类")
  25. @RequestMapping("/Inv/Api")
  26. @Slf4j
  27. public class TRbtTestController {
  28. //其他代码...
  29. @PostMapping("/Test/TypeConvert")
  30. @ApiOperation(value = "系统框架测试-类型转换", httpMethod = "POST", response = ResponseVO.class, notes = "系统框架测试-类型转换")
  31. public ResponseVO TypeConvert(){
  32. TRbtTestEntity fromEntity = new TRbtTestEntity();
  33. fromEntity.setId("1");
  34. fromEntity.setName("luoma.1.1");
  35. fromEntity.setVersion("version1.1");
  36. fromEntity.setCreateBy("luoma");
  37. TRbtTestData toEntity=new TRbtTestData();
  38. toEntity = MapperUtils.convert(fromEntity,toEntity);
  39. ResponseVO<TRbtTestData> result = new ResponseVO<>();
  40. result.setCode(ResponseCode.OK.value());
  41. result.setData(toEntity);
  42. return result;
  43. }
  44. }

测试

使用 Postman 访问 TypeConvert 接口

地址:http://localhost:8080/Inv/Api/Test/TypeConvert
Body raw 参数:””

提交后返回值

  1. {
  2. "code": 2000,
  3. "data": {
  4. "name": "luoma.1.1",
  5. "version": "version1.1",
  6. "id": "1"
  7. }
  8. }

注意

两个转换的类字段名称需要大小写一致,我尝试过了大小写不一致的字段是无法转换的。

扩展-转换 List

test-invoice-webTRbtTestController 修改测试方法 TypeConvert()

  1. package com.test.invoice.controller;
  2. import com.google.gson.Gson;
  3. import com.google.gson.GsonBuilder;
  4. import com.test.invoice.data.TRbtTestData;
  5. import com.test.invoice.data.TRbtTestDataParam;
  6. import com.test.invoice.enums.ResponseCode;
  7. import com.test.invoice.model.TRbtTestEntity;
  8. import com.test.invoice.service.consumer.TRbtTestConsumer;
  9. import com.test.invoice.util.mapper.MapperUtils;
  10. import com.test.invoice.vo.ResponseVO;
  11. import io.swagger.annotations.Api;
  12. import io.swagger.annotations.ApiOperation;
  13. import lombok.extern.slf4j.Slf4j;
  14. import org.springframework.web.bind.annotation.*;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. /**
  17. * 消费者,通过 Feign-api 调用
  18. *
  19. * @author:
  20. * @version:
  21. * @date: 2019-08-12 14:49
  22. */
  23. @RestController
  24. @Api(description = "测试框架系统控制类")
  25. @RequestMapping("/Inv/Api")
  26. @Slf4j
  27. public class TRbtTestController {
  28. //其他代码...
  29. @PostMapping("/Test/TypeConvert")
  30. @ApiOperation(value = "系统框架测试-类型转换", httpMethod = "POST", response = ResponseVO.class, notes = "系统框架测试-类型转换")
  31. public ResponseVO TypeConvert(){
  32. List<TRbtTestEntity> fromList = new ArrayList<>();
  33. for(int i =0;i<3;i++){
  34. TRbtTestEntity fromEntity = new TRbtTestEntity();
  35. fromEntity.setId("1");
  36. fromEntity.setName("luoma.1.1");
  37. fromEntity.setVersion("version1.1");
  38. fromEntity.setCreateBy("luoma");
  39. fromList.add(fromEntity);
  40. // TRbtTestData toEntity=new TRbtTestData();
  41. // toEntity = MapperUtils.convert(fromEntity,toEntity);
  42. // ResponseVO<TRbtTestData> responseVO = new ResponseVO<>();
  43. // responseVO.setData(toEntity);
  44. }
  45. List<TRbtTestData> toList = MapperUtils.convert(fromList,TRbtTestData.class);
  46. ResponseVO<List<TRbtTestData>> responseVO = new ResponseVO<>();
  47. responseVO.setData(toList);
  48. responseVO.setCode(ResponseCode.OK.value());
  49. return responseVO;
  50. }
  51. }

使用 Postman 访问 http://localhost:8080/Inv/Api/Test/TypeConvert

返回

  1. {
  2. "code": 2000,
  3. "data": [
  4. {
  5. "id": "1",
  6. "name": "luoma.1.1",
  7. "version": "version1.1"
  8. },
  9. {
  10. "id": "1",
  11. "name": "luoma.1.1",
  12. "version": "version1.1"
  13. },
  14. {
  15. "id": "1",
  16. "name": "luoma.1.1",
  17. "version": "version1.1"
  18. }
  19. ]
  20. }