我想从ServletResponse
获得Response Body
,以便在传递给客户端之前从中生成哈希签名。我无法找到这样做的方法。为了从ServletRequest
获得请求主体,我实现了如下:
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
public class RequestWrapper extends HttpServletRequestWrapper{
private final String body;
public RequestWrapper(HttpServletRequest request) throws IOException
{
//So that other request method behave just like before
super(request);
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = null;
try {
InputStream inputStream = request.getInputStream();
if (inputStream != null) {
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
char[] charBuffer = new char[128];
int bytesRead = -1;
while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
stringBuilder.append(charBuffer, 0, bytesRead);
}
} else {
stringBuilder.append("");
}
} catch (IOException ex) {
throw ex;
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException ex) {
throw ex;
}
}
}
//Store request pody content in 'body' variable
body = stringBuilder.toString();
}
@Override
public ServletInputStream getInputStream() throws IOException {
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes());
ServletInputStream servletInputStream = new ServletInputStream() {
public int read() throws IOException {
return byteArrayInputStream.read();
}
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setReadListener(ReadListener listener) {
}
};
return servletInputStream;
}
@Override
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(this.getInputStream()));
}
//Use this method to read the request body N times
public String getBody() {
return this.body;
}
}
我如何做类似于ServletResponse
的事情?
我在Filter中实现了ContentCachingResponseWrapper
,以从ServletResponse
获得响应体,如下所示:
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
ContentCachingResponseWrapper responseCacheWrapperObject = new ContentCachingResponseWrapper((HttpServletResponse) servletResponse);
filterChain.doFilter(servletRequest, responseCacheWrapperObject);
byte[] responseArray = responseCacheWrapperObject.getContentAsByteArray();
String responseStr = new String(responseArray, responseCacheWrapperObject.getCharacterEncoding());
//....use responsestr to make the signature
responseCacheWrapperObject.copyBodyToResponse();
}
尝试使用以下内容
public void handleException(Throwable e, HttpServletRequest request, HttpServletResponse response, int status) {
CustomErrorResponseException errorResponse = new CustomErrorResponseException();
errorResponse.setError(e.getClass().getSimpleName());
errorResponse.setStatus(status);
errorResponse.setMessage(e.getLocalizedMessage());
errorResponse.setPath(request.getRequestURI());
errorResponse.setTimestamp(LocalDateTime.now().toString());
response.setStatus(status);
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
try {
response.getWriter().write(jsonbMapper.serialize(errorResponse));
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
在过滤器中只需传递您想要的异常、请求、响应和状态