SpringBoot 更新业务场景下,如何区分null是清空属性值 还是null为vo属性默认值?

先看歧义现象

值为null
SpringBoot 更新业务场景下,如何区分null是清空属性值 还是null为vo属性默认值?_第1张图片

SpringBoot 更新业务场景下,如何区分null是清空属性值 还是null为vo属性默认值?_第2张图片
未传递此属性
SpringBoot 更新业务场景下,如何区分null是清空属性值 还是null为vo属性默认值?_第3张图片
SpringBoot 更新业务场景下,如何区分null是清空属性值 还是null为vo属性默认值?_第4张图片
所以此时如何区分null 时传递进来的的null,还是属性的默认值null?

引入方案

引入过滤器,中间截获requestBodyData并保存到HttpServletRequest,业务层从HttpServletRequest 获取到requestBodyData辅助判断此属性为未传递还是值为null

第1步:定义并注册过滤器

自定义过滤器截获requestBodyData并保存到HttpServletRequest

import com.practisesvr.utils.http.FilterHttpServletRequest;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.GenericFilterBean;

import java.io.IOException;

@Component
public class RequestCheckFilter extends GenericFilterBean {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.err.println("***RequestHeaderCheckFilter.doFilter.start***");

        if (!(request instanceof HttpServletRequest)) {
            throw new IllegalStateException("Expected HttpServletRequest");
        }

        HttpServletRequest httpRequest = (HttpServletRequest) request;
        FilterHttpServletRequest filterHttpRequest = new FilterHttpServletRequest(httpRequest);
        String body = filterHttpRequest.getBody();
        filterHttpRequest.setAttribute("requestBodyData", body);
        
        // 继续执行filter链
        chain.doFilter(filterHttpRequest, response);

        System.err.println("***RequestHeaderCheckFilter.doFilter.end***");
    }
}

[Ref: 过滤器使用到 HttpServletRequestWrapper 参考如下 ] What is HttpServletRequestWrapper does?

注册过滤器

@Configuration
public class AppConfig {
    @Bean
    public FilterRegistrationBean<RequestCheckFilter> requestCheckFilterRegistration() {
        FilterRegistrationBean<RequestCheckFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new RequestCheckFilter());
        // 可以设置过滤器名称
        registration.setName("requestCheckFilter");
        // 设置拦截规则
        registration.addUrlPatterns("/*"); // 拦截所有请求
        // 设置过滤器执行顺序,默认为0,数值越小优先级越高
        registration.setOrder(2);
        return registration;
    }
}

第2步:从HttpServletRequest 获取到requestBodyData

public class RequestIUtils {
    /**
     * 从当前请求上下文中获取并解析请求体数据,将其转换为Map类型对象.
     *
     * @return 如果成功获取到请求体数据并转换成功,则返回一个包含请求体内容的Map对象;
     * 若RequestAttributes不存在或请求体数据为空,则返回一个新的空HashMap对象。
     */
    public static Map<String, Object> getRequestBodyBody() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            return new HashMap<>();
        }
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();

        Object requestBody = request.getAttribute("requestBodyData");
        if (requestBody == null) {
            return new HashMap<>();
        }
        return JsonUtils.jsonToT(requestBody.toString(), Map.class);
    }
}    

第3步:业务层使用requestBodyData辅助判断

@Override
public Student update(Student student) {
    Map<String, Object> requestBody = RequestIUtils.getRequestBodyBody();
    
    Student stu = new Student();
    stu.setId(student.getId());
    stu.setUsername(student.getUsername());
    stu.setPassword(student.getPassword());
    stu.setAge(student.getAge());
    stu.setHeight(student.getHeight());
    stu.setGender(student.getGender());
    // 使用方式
    if (requestBody.containsKey("classId")){
        stu.setClassId(student.getClassId());
    }
    stu.setIsDelete(student.getIsDelete());
    
    this.studentsMapper.update(student);
    return this.queryById(student.getId());
}

SpringBoot 更新业务场景下,如何区分null是清空属性值 还是null为vo属性默认值?_第5张图片

SpringBoot 更新业务场景下,如何区分null是清空属性值 还是null为vo属性默认值?_第6张图片
SpringBoot 更新业务场景下,如何区分null是清空属性值 还是null为vo属性默认值?_第7张图片

SpringBoot 更新业务场景下,如何区分null是清空属性值 还是null为vo属性默认值?_第8张图片

你可能感兴趣的:(SpringMVC,SpringBoot,框架,spring,boot,后端,java)