在
Class A
在获得请求正文后
Class B
正在获取空的请求正文。
根据检查是否要在中注释代码
A类
为了检索请求主体,
B类
将能够获得请求正文。
有没有办法使HttpServlet请求不会删除中的请求主体
A类
,订单
B类
仍然可以获得请求正文?
A类
@Autowired
private HttpServletRequest request;
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String requestBody = extractBody(request);
System.out.println("Request Body1: "+requestBody);
....other codes
}
public static String extractBody(HttpServletRequest request){
String body = null;
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) {
try {
throw ex;
} catch (IOException e) {
throw new RuntimeException(e);
}
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException ex) {
try {
throw ex;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
body = stringBuilder.toString();
return body;
}
B类
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {
String requestBody = extractBody(request);
System.out.println("Request Body2: "+requestBody);
....other codes
}
public static String extractBody(HttpServletRequest request){
String body = null;
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) {
try {
throw ex;
} catch (IOException e) {
throw new RuntimeException(e);
}
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException ex) {
try {
throw ex;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
body = stringBuilder.toString();
return body;
}