SpringBoot如何使⽤RequestBodyAdvice进⾏统⼀参数处理
SpringBoot RequestBodyAdvice参数处理
在实际项⽬中 , 往往需要对请求参数做⼀些统⼀的操作 , 例如参数的过滤 , 字符的编码 , 第三⽅的解密等等 , Spring提供了RequestBodyAdvice⼀个全局的解决⽅案 , 免去了我们在Controller处理的繁琐 . RequestBodyAdvice仅对使⽤了@RqestBody注解的⽣效 , 因为它原理上还是AOP , 所以GET⽅法是不会操作的.
package com.xbzmon.web;
import MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.verter.HttpMessageConverter;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.hod.annotation.RequestBodyAdvice;
import java.io.IOException;
import java.io.InputStream;
import flect.Type;
/**
* @title 全局请求参数处理类
* @author Xingbz
* @createDate 2019-8-2
*/
@ControllerAdvice(basePackages = "ller")//此处设置需要当前Advice执⾏的域 , 省略默认全局⽣效
public class GlobalRequestBodyAdvice implements RequestBodyAdvice {
/** 此处如果返回false , 则不执⾏当前Advice的业务 */
@Override
public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
// Method().isAnnotationPresent(XXApiReq.class);
return false;
}
/**
* @title 读取参数前执⾏
* @description 在此做些编码 / 解密 / 封装参数为对象的操作
*
* */
@Override
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
return new XHttpInputMessage(inputMessage, "UTF-8");
}
/**
* @title 读取参数后执⾏
* @author Xingbz
*/
@Override
public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter para
meter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
return inputMessage;
}
/**
* @title ⽆请求时的处理
*/
@Override
public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
return body;
}
}
//这⾥实现了HttpInputMessage 封装⼀个⾃⼰的HttpInputMessage
class XHttpInputMessage implements HttpInputMessage {
private HttpHeaders headers;
private InputStream body;
public XHttpInputMessage(HttpInputMessage httpInputMessage, String encode) throws IOException {
this.headers = Headers();
this.body = Body(), encode);
}
private InputStream encode(InputStream body, String encode) {
//省略对流进⾏编码的操作
return body;
}
@Override
public InputStream getBody() {
return body;
}
@Override
public HttpHeaders getHeaders() {
return null;
}
}
Spring默认提供了接⼝的抽象实现类RequestBodyAdviceAdapter , 我们可以继承这个类按需实现 , 让代码更简洁⼀点
package org.springframework.web.hod.annotation;
import java.io.IOException;
import flect.Type;
import MethodParameter;
import org.springframework.http.HttpInputMessage;
import org.verter.HttpMessageConverter;
import org.springframework.lang.Nullable;
public abstract class RequestBodyAdviceAdapter implements RequestBodyAdvice {
@Override
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter,
Type targetType, Class<? extends HttpMessageConverter<?>> converterType)
throws IOException {
return inputMessage;
}
@Override
public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter,
Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
return body;
}
@Override
@Nullable
public Object handleEmptyBody(@Nullable Object body, HttpInputMessage inputMessage,
MethodParameter parameter, Type targetType,
Class<? extends HttpMessageConverter<?>> converterType) {
return body;
}
Springboot 对RequestBody的值进⾏统⼀修改的⼏种⽅式
背景
最近在项⽬中遇到需要统⼀对Request请求中的某⼀个⾃定义对象的属性进⾏统⼀修改的需求。
考虑了⼏种实现⽅式,现在记录⼀下。由于原项⽬过于复杂,⾃⼰写⼏个demo进⾏记录。
解决⽅式
⽅式⼀:利⽤filter进⾏处理
⼤坑:
如果你想要改变加了RequestBody注解的数据,⽆论如何你都要通过getInputStream()⽅法来获取流来
拿到对应的参数,然后更改。在不经过拿取流的情况下,spring的RequestBody注解也是通过getInputStream()⽅法来获取流来映射为request对象。
但是如果你想要的统⼀的进⾏修改,也必须经过getInputStream()来⾸先拿到stream然后才能进⾏修改。但此时stream被消费之后,就会关闭。
然后你的controller中的参数就拿不到对象,报错如下。
I/O error while reading input message; nested exception is java.io.IOException: Stream closed
可以通过创建并使⽤⾃定义的的HttpServletRequestWrapper来避免这种情况。
步骤⼀:编写⾃定义HttpServletRequestWrapper
stlhf.filter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
ity.Student;
slf4j.Slf4j;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
/**
* @Description TODO
* @Author yyf
* @Date 2020/10/29 12:48
* @Version 1.0
**/
@Slf4j
public class ChangeStudentNameRequestWrapper extends HttpServletRequestWrapper {
/**
* 存储body数据的容器
*/
private byte[] body;
public ChangeStudentNameRequestWrapper(HttpServletRequest request) throws IOException {
super(request);
//接下来的request使⽤这个
String bodyStr = getBodyString(request);
body = Bytes(Charset.defaultCharset());
}
/**
* 获取请求Body
*
* @param request request
* @return String
public String getBodyString(final ServletRequest request) {
try {
return InputStream());
} catch (IOException e) {
<("", e);
throw new RuntimeException(e);
}
}
/**
* 获取请求Body
*
* @return String
*/
public String getBodyString() {
final InputStream inputStream = new ByteArrayInputStream(body);
return inputStream2String(inputStream);
}
/**
* 将inputStream⾥的数据读取出来并转换成字符串
*
* @param inputStream inputStream
* @return String
*/
private String inputStream2String(InputStream inputStream) {
StringBuilder sb = new StringBuilder();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(inputStream, Charset.defaultCharset()));
String line;
while ((line = adLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
("", e);
throw new RuntimeException(e);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
<("", e);
}
}
}
JSONObject jsonObject = JSONObject.String());
if (jsonObject != null && ("student") != null) {
Student student = JavaObject((JSON) ("student"), Student.class);
log.info("修改之前的学⽣名称为:" + Name());
student.setName("amd");
jsonObject.put("student", student);
JSONString();
}
String();
}
@Override
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(getInputStream()));
}
@Override
public ServletInputStream getInputStream() throws IOException {
final ByteArrayInputStream inputStream = new ByteArrayInputStream(body);
return new ServletInputStream() {
@Override
public int read() throws IOException {
ad();
}
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setReadListener(ReadListener readListener) {
}
};
}
}
步骤⼆:使⽤⾃定义的HttpServletRequestWrapper取代原有的
使⽤⾃定义的request取代原有的传递给过滤器链。
stlhf.filter;
slf4j.Slf4j;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
/**
* @Description TODO
* @Author yyf
* @Date 2020/10/29 13:20
* @Version 1.0
**/
@Slf4j
public class ReplaceStreamFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
log.info("StreamFilter初始化...");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { ServletRequest requestWrapper = null;
//获取请求中的流,将取出来的字符串,再次转换成流,然后把它放⼊到新request对象中,
if (request instanceof HttpServletRequest) {
requestWrapper = new ChangeStudentNameRequestWrapper((HttpServletRequest) request);
}
// 在chain.doFiler⽅法中传递新的request对象
if (requestWrapper == null) {
chain.doFilter(request, response);
} else {
chain.doFilter(requestWrapper, response);
}
}
@Override
public void destroy() {
log.info("StreamFilter销毁...");
}
}
步骤三:将过滤器注册进spring容器
stlhf.filter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import t.annotation.Bean;
import t.annotation.Configuration;
import javax.servlet.Filter;
/**
* @Description TODO
* @Author yyf
* @Date 2020/10/29 14:20
* @Version 1.0
**/
@Configuration
public class MyFilterConfig {
/**
* 注册过滤器
*
* @return FilterRegistrationBean
*/
@Bean
public FilterRegistrationBean someFilterRegistration() {
FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
registration.setFilter(replaceStreamFilter());
registration.addUrlPatterns("/*");
registration.setName("replaceStreamFilter");
return registration;
}
/**
* 实例化StreamFilter
*
* @return Filter
*/
@Bean(name = "replaceStreamFilter")
public Filter replaceStreamFilter() {
return new ReplaceStreamFilter();
}
}
看下效果:
到此使⽤过滤器对post请求中的参数的修改已经完毕。
⽅式⼆:使⽤进⾏处理
当我⾃以为可以使⽤前置通知进⾏处理时才发现,事情并不简单。
步骤⼀:⾃定义⼀个
如下图实现⼀个,preHandle中有HttpServletRequest request参数,虽然可以通过它的流获取到body中数据,但是如果将body中数据进⾏修改的话,其并不能传递给controller。因为request只有两个set⽅法。如果将要统⼀修改的值摄⼊Attribute,则还仍需从controller中拿到
步骤⼆:在controller中获取值
虽然⽤这种⽅式可以在request中添加统⼀的参数,也可以从每⼀个controller中获取值,但仍需要对每⼀个controller进⾏代码修改,显然这种⽅式并不是我们需要的。⽅式三:使⽤切⾯处理
步骤⼀:引⼊aspect所需要使⽤的maven依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
步骤⼆:编写⾃定义的前置通知以及表达
@Component
@Aspect
public class ChangeStudentNameAdvice {
@Before("execution(* stlhf.service.impl.*.*(..))&&args(addStudentRequset)")
public void aroundPoints(AddStudentRequset addStudentRequset) {
}
}
注意此处的形参需要和args括号内的字符串保持⼀致,否则报错。
注意此处的形参需要和args括号内的字符串保持⼀致,否则报错。
步骤三:开启注解@EnableAspectJAutoProxy
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论