需要被增强的类
@Data
public class Stu {
private int no;
private String name;
private String age;
}
为了解决现实中的业务需求,再添加一个PageReuslt作为分页
@Getter
public class PageResult<T> implements Serializable {
private final List<T> data = new ArrayList<>();
private final long total;
private final Pageable pageable;
public PageResult(final List<T> data, final Long total, final Pageable pageable) {
if (Objects.nonNull(data) && data.size() > 0) {
this.data.addAll(data);
}
this.total = (total == null ? 0 : total);
this.pageable = pageable;
}
public static <T> PageResult<T> empty() {
return of(Collections.emptyList(), 0L);
}
public static <T> PageResult<T> of(final List<T> data, final Long total) {
return new PageResult<>(data, total, null);
}
public boolean isEmpty() {
return this.total <= 0 || this.data.isEmpty();
}
public <U> PageResult<U> map(Function<? super T, ? extends U> converter) {
return new PageResult<>(data.stream().map(converter).collect(Collectors.toList()), total, pageable);
}
}
@Getter
public class Pageable implements Serializable {
private final int page;
private final int size;
public static Pageable of(final Integer page, final Integer size) {
return of(page, size); // 默认分页不排序
}
}
下面这2个类是工具类
主要用来动态的给对象添加属性
大致使用如下,
引入几个jar包依赖(有些包可以不用,自己看着就好)
<!-- springboot的环境-->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>3.2.4</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.70</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>16.0.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
Stu resultVO = new Stu(2, "e", "er");
Map<String, Object> map = new HashMap();
map.put("states", "1");
Stu stu1 = (Stu)BeanAddPropertiesUtil.getTarget(stu, map);
System.out.println(JSON.toJSONString(stu1));
//这个stu1就是已经增强的,看清楚了 JSON.toJSONSting,
//自己重新的toString全是null。
具体可以看这里 https://www.jianshu.com/p/cc1014e71e8a
DynamicBean
package com.lsm.boot.config;
import net.sf.cglib.beans.BeanGenerator;
import net.sf.cglib.beans.BeanMap;
import java.util.Map;
public class DynamicBean {
/**
* 目标对象
*/
private Object target;
/**
* 属性集合
*/
private BeanMap beanMap;
public DynamicBean(Class superclass, Map<String, Class> propertyMap) {
this.target = generateBean(superclass, propertyMap);
this.beanMap = BeanMap.create(this.target);
}
/**
* bean 添加属性和值
*
* @param property
* @param value
*/
public void setValue(String property, Object value) {
beanMap.put(property, value);
}
/**
* 获取属性值
*
* @param property
* @return
*/
public Object getValue(String property) {
return beanMap.get(property);
}
/**
* 获取对象
* @return
*/
public Object getTarget() {
return this.target;
}
/**
* 根据属性生成对象
* @param superclass
* @param propertyMap
* @return
*/
private Object generateBean(Class superclass, Map<String, Class> propertyMap) {
BeanGenerator generator = new BeanGenerator();
if (null != superclass) {
generator.setSuperclass(superclass);
}
BeanGenerator.addProperties(generator, propertyMap);
return generator.create();
}
}
BeanAddPropertiesUtil
public class BeanAddPropertiesUtil {
public static Object getTarget(Object dest, Map<String, Object> addProperties) {
// get property map
PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(dest);
Map<String, Class> propertyMap = Maps.newHashMap();
for (PropertyDescriptor d : descriptors) {
if (!"class".equalsIgnoreCase(d.getName())) {
propertyMap.put(d.getName(), d.getPropertyType());
}
}
// add extra properties
addProperties.forEach((k, v) -> propertyMap.put(k, v.getClass()));
// new dynamic bean
DynamicBean dynamicBean = new DynamicBean(dest.getClass(), propertyMap);
// add old value
propertyMap.forEach((k, v) -> {
try {
// filter extra properties
if (!addProperties.containsKey(k)) {
dynamicBean.setValue(k, propertyUtilsBean.getNestedProperty(dest, k));
}
} catch (Exception e) {
e.printStackTrace();
}
});
// add extra value
addProperties.forEach((k, v) -> {
try {
dynamicBean.setValue(k, v);
} catch (Exception e) {
e.printStackTrace();
}
});
Object target = dynamicBean.getTarget();
return target;
}
}
isAddStatus
/**
只有加了这个注解的方法才进行拦截操作
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface isAddStatus {
String value() default "";
}
ResponseBodyAdvice的业务实现类
package com.lsm.boot.config;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ControllerAdvice
public class CustomReturnJsonAdvice implements ResponseBodyAdvice {
// 判断是否要执行beforeBodyWrite方法,true为执行,false不执行.默认是false,记得改
@Override
public boolean supports(MethodParameter methodParameter, Class aClass) {
return true;
}
//实现自己业务逻辑
@Override
public Object beforeBodyWrite(Object body, MethodParameter methodParameter, MediaType mediaType, Class clazz, ServerHttpRequest request, ServerHttpResponse response) {
//这里打印出来其实,没什么用,实际中需要用到一些东西的话还是去debug找
// methodParameter重要的信息多
System.out.println("methodParameter:" + methodParameter);
System.out.println("clazz:" + clazz);
System.out.println("request:" + request);
System.out.println("response:" + response);
// 判断该方法是是不是有这个注解, 只有加了这个注解的方法才进行拦截操作
boolean flag = methodParameter.getMethod().isAnnotationPresent(isAddStatus.class);
//Type genericReturnType = ((ParameterizedType) body.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
/**
* boolean result = obj instanceof Class
* obj 为一个对象,Class 表示一个类或者一个接口,当 obj 为 Class 的对象,或者是其直接或间接子类,
* 或者是其接口的实现类,结果result 都返回 true,否则返回false
* https://www.cnblogs.com/ysocean/p/8486500.html
*/
if (body != null && body instanceof PageResult && flag) {
/**
* public class PageResult<T> implements Serializable {
* private final List<T> data = new ArrayList<>();
* private final long total;
* }
*/
PageResult vo = (PageResult) body;
//存放新的list
ArrayList list = new ArrayList();
// 利用methodParameter获取方法,(如果需要其他的功能直接debug去里面找,看下图)
//genericReturnType 获取返回值的类型
ParameterizedType parameterizedType = (ParameterizedType) methodParameter.getMethod().getGenericReturnType();
// 然后取到泛型,这里就是Stu [getTypeName()方法获取到就是全路径 如com.lsm.boot.config.Stu]
String typeName = parameterizedType.getActualTypeArguments()[0].getTypeName();
//根据不同业务动态的操作
//我这里就是Stu的name是zhangsan,就动态添加一个字段
if (typeName.equals("com.lsm.boot.config.Stu")) {
List<Stu> data = vo.getData();
for (Stu stu : data) {
if (stu.getName().equals("zhangsan")) {
Map<String, Object> addProperties = new HashMap() {{
put("status", "true");
}};
Object o = BeanAddPropertiesUtil.getTarget(stu, addProperties);
list.add(o);
} else {
list.add(stu);
}
}
}
//简单逻辑
/* List data = vo.getData();
//ArrayList list = new ArrayList();
for (Object datum : data) {
Map<String, Object> addProperties = new HashMap() {{
put("动态属性名", "动态属性值");
}};
Object o = PicBeanAddPropertiesUtil.getTarget(datum, addProperties);
list.add(o);
}*/
// 因为我这里的属性为final修饰的,而且还是封装好的,在jar包里面,只能用反射赋值。简单点可以直接setData()
final Class<?> clz = vo.getClass();
try {
final Field nameField = clz.getDeclaredField("data");
nameField.setAccessible(true);
nameField.set(vo, list);
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
return body;
}
}
大佬链接:
https://blog.csdn.net/u013107634/article/details/89457762
@RestController
public class TestResponseController {
@RequestMapping("/test")
@isAddStatus()
public PageResult<Stu> test() {
List<Stu> list = new ArrayList<>();
Stu stu = new Stu(1,"zhangsan","n");
Stu stu1 = new Stu(2,"ab","nb");
list.add(stu);
list.add(stu1);
return new PageResult<Stu>(list,10L, null);
}
@RequestMapping("/test123")
public PageResult<Stu> test123() {
List<Stu> list = new ArrayList<>();
Stu stu = new Stu(1,"a","n");
Stu stu1 = new Stu(2,"zhangsan","nb");
list.add(stu);
list.add(stu1);
return new PageResult<Stu>(list,10L, null);
}
}