日志管理模块是一个常见的功能模块。本文主要就参数获取部分展开,获取之后的存库或其他操作,各位看客可自行选择,本文不进行讨论。
前端传参通过分为multipart/form-data 与 application/json 两个传参方式。
multipart/form-data:除了普通参数外,还可以上传文件,而文件对象在后台的使用过程是不可序列化的。所以如果不加注意,就会发生序列化问题。针对这种传参,使用getParameterMap方法。该方法是request提供的,可以直接获得前端参数。
application/json:这是另一种常见的传参形式,本文通过定义Request增强类来完成参数的获取。
在SpringBoot中 json传参需要通过request中的inpustream来获取,但有个需要注意的问题是,inpustream只能一次性使用,所以需要针对这点来进行增强。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
package com.xue.log.wrapper;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletResponse;
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.InputStreamReader;
import java.nio.charset.StandardCharsets;
/**
* 增加reqeust
* application/json传 body获取及inpustream增加
*/
public class JsonRequestWrapper extends HttpServletRequestWrapper {
private final byte[] body;
public JsonRequestWrapper(HttpServletRequest request, ServletResponse response) {
super(request);
body = RequestHelper.getBody(request).getBytes(StandardCharsets.UTF_8);
}
@Override
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(getInputStream()));
}
@Override
public ServletInputStream getInputStream() throws IOException {
final ByteArrayInputStream bais = new ByteArrayInputStream(body);
return new ServletInputStream() {
@Override
public int read() throws IOException {
return bais.read();
}
@Override
public int available() throws IOException {
return body.length;
}
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setReadListener(ReadListener readListener) {
}
};
}
}
定义final byte[] 的body来解决inpustream只能使用一次的问题。
package com.xue.log.wrapper;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
/**
* 过滤器,使JsonReqeustWraper生效
*/
@Component
public class JsonRequestFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
/**
* 将application/json request请求包装成自定义的增加类
*
* @param request
* @param response
* @param chain
* @throws IOException
* @throws ServletException
*/
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
ServletRequest requestWrapper = null;
if (request instanceof HttpServletRequest
&& StringUtils.startsWithIgnoreCase(request.getContentType(), MediaType.APPLICATION_JSON_VALUE)) {
requestWrapper = new JsonRequestWrapper((HttpServletRequest) request, response);
}
if (null == requestWrapper) {
chain.doFilter(request, response);
} else {
chain.doFilter(requestWrapper, response);
}
}
@Override
public void destroy() {
}
}
通过自定义Filter 监控application/json 类型传参,如果是该类型传参就包装成上面自定义的JsonRequestWrapper。
package com.xue.log.wrapper;
import javax.servlet.ServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
public class RequestHelper {
/**
* body 获取
*
* @param request
* @return {@link String}
*/
public static String getBody(ServletRequest request) {
StringBuilder sb = new StringBuilder();
try (
InputStream inputStream = request.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
) {
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
}
package com.xue.log.aop;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xue.log.wrapper.JsonRequestWrapper;
import com.xue.log.wrapper.RequestHelper;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
@Component
@Aspect
public class AopLog {
private static final ObjectMapper JSON = new ObjectMapper();
/**
* 定义切面
*/
@Pointcut("execution(public * com.xue.log.controller.*.*(..))")
public void pointCut() {
}
@After("pointCut()")
public void doAfterReturing(JoinPoint joinPoint) throws JsonProcessingException {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
// 定义参数
String bodyString;
if (request instanceof JsonRequestWrapper) {
// application/json传参 参数获取
bodyString = RequestHelper.getBody(request);
System.out.println("application/json传参。参数:" + bodyString);
} else {
// multipart/form-data传参 参数获取
bodyString = JSON.writeValueAsString(request.getParameterMap());
System.out.println("multipart/form-data传参。参数:" + bodyString);
}
}
}
package com.xue.log.controller;
import com.xue.log.entity.TestEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
@RestController
public class TestController {
@PostMapping("/test1")
public void test1(MultipartFile file, String fileName) {
System.out.println("test1访问成功,multipart/form-data 带文件传参");
}
@GetMapping("/test2")
public void test2(String fileName) {
System.out.println("test2访问成功,multipart/form-data 不带文件传参");
}
@PostMapping("/test3")
public void test3(@RequestBody TestEntity entity) {
System.out.println("test3访问成功,application/json 类型传参");
}
}
以上就是今天要讲的内容,本文仅仅只是演示了下参数的获取,至于参数获取后的操作,就需要各位看客自行处理