To get the response body from a ServletResponse
object in Spring Boot, you can use the following method:
- First, you will need to get the
ServletResponse
object as an instance ofHttpServletResponse
. You can do this by casting theServletResponse
object toHttpServletResponse
.
HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
- Next, you can use the
getOutputStream()
method of theHttpServletResponse
object to get an instance ofServletOutputStream
.
ServletOutputStream outputStream = httpResponse.getOutputStream();
- You can then use the
toByteArray()
method of theServletOutputStream
to get the response body as a byte array.
byte[] responseBody = outputStream.toByteArray();
- If you want to convert the response body to a string, you can use the
new String(byte[])
constructor to create a string from the byte array.
String responseBodyString = new String(responseBody);