和请求体。

  1. AOP实现RESTful动态过滤嵌套对象字段

首先定义一个注解@DynamicFilter,用于标记需要进行动态过滤的方法。然后定义一个切面类DynamicFilterAspect,在该类中使用反射获取方法的返回值类型,然后根据请求参数中的字段过滤条件,使用反射设置返回值对象的相应字段值为null。

注解定义:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DynamicFilter {
}

切面类定义:

@Aspect
@Component
public class DynamicFilterAspect {

    @Around("@annotation(com.example.demo.DynamicFilter)")
    public Object dynamicFilter(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result = joinPoint.proceed();
        if (result != null) {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            String fields = request.getParameter("fields");
            if (fields != null && !fields.isEmpty()) {
                String[] fieldArray = fields.split(",");
                for (String field : fieldArray) {
                    filterField(result, field);
                }
            }
        }
        return result;
    }

    private void filterField(Object object, String fieldName) throws NoSuchFieldException, IllegalAccessException {
        Field field = object.getClass().getDeclaredField(fieldName);
        field.setAccessible(true);
        Object fieldValue = field.get(object);
        if (fieldValue != null) {
            filter(fieldValue);
        }
        field.set(object, null);
    }

    private void filter(Object object) throws NoSuchFieldException, IllegalAccessException {
        Field[] fields = object.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            Object fieldValue = field.get(object);
            if (fieldValue != null) {
                filter(fieldValue);
            }
            if (field.isAnnotationPresent(JsonIgnore.class) || field.getName().equals("serialVersionUID")) {
                continue;
            }
            field.set(object, null);
        }
    }
}

使用@DynamicFilter注解标记需要动态过滤的方法,例如:

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @DynamicFilter
    @GetMapping("/users/{id}")
    public User getUserById(@PathVariable Long id) {
        return userService.getUserById(id);
    }
}
  1. 反射实现RESTful动态过滤嵌套对象字段

实现过程与AOP类似,只是将动态过滤逻辑封装在一个工具类中,然后在Controller中调用该工具类实现动态过滤。

工具类定义:

public class DynamicFilterUtil {

    public static Object filter(Object object, String fields) throws NoSuchFieldException, IllegalAccessException {
        if (object == null) {
            return null;
        }
        if (fields != null && !fields.isEmpty()) {
            String[] fieldArray = fields.split(",");
            for (String field : fieldArray) {
                filterField(object, field);
            }
        }
        return object;
    }

    private static void filterField(Object object, String fieldName) throws NoSuchFieldException, IllegalAccessException {
        Field field = object.getClass().getDeclaredField(fieldName);
        field.setAccessible(true);
        Object fieldValue = field.get(object);
        if (fieldValue != null) {
            filter(fieldValue, null);
        }
        field.set(object, null);
    }

    private static void filter(Object object, String fields) throws NoSuchFieldException, IllegalAccessException {
        Field[] fieldsArray = object.getClass().getDeclaredFields();
        for (Field field : fieldsArray) {
            field.setAccessible(true);
            Object fieldValue = field.get(object);
            if (fieldValue != null) {
                filter(fieldValue, fields);
            }
            if (field.isAnnotationPresent(JsonIgnore.class) || field.getName().equals("serialVersionUID")) {
                continue;
            }
            if (fields != null && !fields.isEmpty()) {
                if (!fields.contains(field.getName())) {
                    field.set(object, null);
                }
            } else {
                field.set(object, null);
            }
        }
    }
}

Controller中调用工具类:

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/users/{id}")
    public Object getUserById(@PathVariable Long id, @RequestParam(required = false) String fields) throws NoSuchFieldException, IllegalAccessException {
        User user = userService.getUserById(id);
        return DynamicFilterUtil.filter(user, fields);
    }
}
  1. 请求案例

GET http://localhost:8080/users/1?fields=name,age,department.name

请求体为空。

返回结果:

{
    "id": 1,
    "name": "张三",
    "age": 20,
    "department": {
        "name": "技术部"
    }
}

说明:

请求参数中的fields参数指定需要返回的字段,使用逗号分隔多个字段。返回结果中只包含指定的字段,其他字段值为null。


原文地址: http://www.cveoy.top/t/topic/bgE8 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录