本文介绍: SpringBoot 更新业务场景下,如何区分null是清空属性值 还是null为vo属性默认值?
先看歧义现象
值为null
未传递此属性
所以此时如何区分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());
}
原文地址:https://blog.csdn.net/weixin_37646636/article/details/135647331
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_59396.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。