说明
本文代码基于 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
TRbtTestEntity
package com.test.invoice.model;
import com.baomidou.mybatisplus.annotation.TableName;
import com.test.invoice.model.base.SuperEntity;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.experimental.Accessors;
/**
* t_rbt_test 表对应实体类-TRbtTestEntity
*
* @author:
* @version:
* @date: 2019-08-09 14:25
*/
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
@ToString
@TableName("t_rbt_test")
public class TRbtTestEntity extends SuperEntity {
@ApiModelProperty("名称")
private String name;
@ApiModelProperty("版本号")
private String version;
}
TRbtTestData
package com.test.invoice.data;
import com.alibaba.fastjson.annotation.JSONField;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* TRbtTestData 参数类
*
* @author:
* @version:
* @date: 2019-08-09 08:57
*/
@Data
@Accessors(chain = true) //@Accessors 注解用来配置lombok如何产生和显示getters和setters的方法。
public class TRbtTestData {
@ApiModelProperty("id")
@JSONField(name = "id")
private String id;
@ApiModelProperty("名称")
@JSONField(name = "name")
private String name;
@ApiModelProperty("版本号")
@JSONField(name = "version")
private String version;
}
添加对象转换类-MapperUtils
test-invoice-common
项目,添加包 util
-mapper
-MapperUtils
package com.test.invoice.util.mapper;
import org.dozer.DozerBeanMapperBuilder;
import org.dozer.Mapper;
import org.springframework.util.Assert;
import java.util.ArrayList;
import java.util.List;
/**
* 对象转换类
*
* @author:
* @version:
* @date: 2019/3/13 18:13
*/
public class MapperUtils {
private static final Mapper MAPPER = DozerBeanMapperBuilder.buildDefault();
public static <T, S> T convert(S source, T target) {
if (source == null) {
return null;
}
Assert.notNull(target, "target instance required");
MAPPER.map(source, target);
return target;
}
public static <T, S> T convert(S source, Class<T> targetClass) {
if (source == null) {
return null;
}
Assert.notNull(targetClass, "targetClass required");
return MAPPER.map(source, targetClass);
}
public static <T, S> List<T> convert(List<S> source, Class<T> targetClass) {
if (source == null) {
return null;
}
List<T> targetList = new ArrayList<>();
for (S s : source) {
T target = MAPPER.map(s, targetClass);
targetList.add(target);
}
return targetList;
}
}
添加测试方法
test-invoice-web
的 TRbtTestController
添加测试方法 TypeConvert()
package com.test.invoice.controller;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.test.invoice.data.TRbtTestData;
import com.test.invoice.data.TRbtTestDataParam;
import com.test.invoice.enums.ResponseCode;
import com.test.invoice.model.TRbtTestEntity;
import com.test.invoice.service.consumer.TRbtTestConsumer;
import com.test.invoice.util.mapper.MapperUtils;
import com.test.invoice.vo.ResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
/**
* 消费者,通过 Feign-api 调用
*
* @author:
* @version:
* @date: 2019-08-12 14:49
*/
@RestController
@Api(description = "测试框架系统控制类")
@RequestMapping("/Inv/Api")
@Slf4j
public class TRbtTestController {
//其他代码...
@PostMapping("/Test/TypeConvert")
@ApiOperation(value = "系统框架测试-类型转换", httpMethod = "POST", response = ResponseVO.class, notes = "系统框架测试-类型转换")
public ResponseVO TypeConvert(){
TRbtTestEntity fromEntity = new TRbtTestEntity();
fromEntity.setId("1");
fromEntity.setName("luoma.1.1");
fromEntity.setVersion("version1.1");
fromEntity.setCreateBy("luoma");
TRbtTestData toEntity=new TRbtTestData();
toEntity = MapperUtils.convert(fromEntity,toEntity);
ResponseVO<TRbtTestData> result = new ResponseVO<>();
result.setCode(ResponseCode.OK.value());
result.setData(toEntity);
return result;
}
}
测试
使用 Postman
访问 TypeConvert
接口
地址:http://localhost:8080/Inv/Api/Test/TypeConvert
Body raw 参数:””
提交后返回值
{
"code": 2000,
"data": {
"name": "luoma.1.1",
"version": "version1.1",
"id": "1"
}
}
注意
两个转换的类字段名称需要大小写一致,我尝试过了大小写不一致的字段是无法转换的。
扩展-转换 List
test-invoice-web
的 TRbtTestController
修改测试方法 TypeConvert()
package com.test.invoice.controller;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.test.invoice.data.TRbtTestData;
import com.test.invoice.data.TRbtTestDataParam;
import com.test.invoice.enums.ResponseCode;
import com.test.invoice.model.TRbtTestEntity;
import com.test.invoice.service.consumer.TRbtTestConsumer;
import com.test.invoice.util.mapper.MapperUtils;
import com.test.invoice.vo.ResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
/**
* 消费者,通过 Feign-api 调用
*
* @author:
* @version:
* @date: 2019-08-12 14:49
*/
@RestController
@Api(description = "测试框架系统控制类")
@RequestMapping("/Inv/Api")
@Slf4j
public class TRbtTestController {
//其他代码...
@PostMapping("/Test/TypeConvert")
@ApiOperation(value = "系统框架测试-类型转换", httpMethod = "POST", response = ResponseVO.class, notes = "系统框架测试-类型转换")
public ResponseVO TypeConvert(){
List<TRbtTestEntity> fromList = new ArrayList<>();
for(int i =0;i<3;i++){
TRbtTestEntity fromEntity = new TRbtTestEntity();
fromEntity.setId("1");
fromEntity.setName("luoma.1.1");
fromEntity.setVersion("version1.1");
fromEntity.setCreateBy("luoma");
fromList.add(fromEntity);
// TRbtTestData toEntity=new TRbtTestData();
// toEntity = MapperUtils.convert(fromEntity,toEntity);
// ResponseVO<TRbtTestData> responseVO = new ResponseVO<>();
// responseVO.setData(toEntity);
}
List<TRbtTestData> toList = MapperUtils.convert(fromList,TRbtTestData.class);
ResponseVO<List<TRbtTestData>> responseVO = new ResponseVO<>();
responseVO.setData(toList);
responseVO.setCode(ResponseCode.OK.value());
return responseVO;
}
}
使用 Postman 访问 http://localhost:8080/Inv/Api/Test/TypeConvert
返回
{
"code": 2000,
"data": [
{
"id": "1",
"name": "luoma.1.1",
"version": "version1.1"
},
{
"id": "1",
"name": "luoma.1.1",
"version": "version1.1"
},
{
"id": "1",
"name": "luoma.1.1",
"version": "version1.1"
}
]
}