javarequestxml_java–从HttpServletRequest获取XML并。。
。
我想从请求和响应中获取XML数据并将其⽤于Rest控制器.我试过这个:
@RestController()
public class HomeController {
@PostMapping(value = "/v1")
public Response handleMessage(@RequestBody Transaction transaction, HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpServletRequest request, HttpServletResponse response
System.out.println(" InputStream");
System.out.InputStream());
System.out.OutputStream());
InputStream in = InputStream();
String readLine;
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while (((readLine = br.readLine()) != null)) {
System.out.println(readLine);
}
}
}
但我得到java.io.IOException:UT010029:Stream已关闭
将内容转换为String变量的正确⽅法是什么?
编辑:我也试过过滤器的解决⽅案,但我不知道如何将请求有效负载⽤于休息控制器:
读请求负载:
@Component
public class HttpLoggingFilter implements Filter {
private static final Logger logger = Logger(HttpLoggingFilter.class);
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
ResettableStreamHttpServletRequest wrappedRequest = new ResettableStreamHttpServletRequest((HttpServletRequest) request);
String body = Reader());
System.out.println(" " + body);
chain.doFilter(request, response);
}
public class ResettableStreamHttpServletRequest extends HttpServletRequestWrapper {
private byte[] rawData;
private HttpServletRequest request;
private ResettableServletInputStream servletStream;
ResettableStreamHttpServletRequest(HttpServletRequest request) {
super(request);
this.servletStream = new ResettableServletInputStream();
}
void resetInputStream() {
servletStream.stream = new ByteArrayInputStream(rawData);
}
@Override
java xml是什么public ServletInputStream getInputStream() throws IOException {
if (rawData == null) {
rawData = InputStream());
servletStream.stream = new ByteArrayInputStream(rawData);
}
return servletStream;
}
@Override
public BufferedReader getReader() throws IOException {
if (rawData == null) {
rawData = InputStream());
servletStream.stream = new ByteArrayInputStream(rawData);
}
String encoding = getCharacterEncoding();
if (encoding != null) {
return new BufferedReader(new InputStreamReader(servletStream, encoding)); } else {
return new BufferedReader(new InputStreamReader(servletStream));
}
}
private class ResettableServletInputStream extends ServletInputStream { private InputStream stream;
@Override
public int read() throws IOException {
ad();
}
@Override
public boolean isFinished() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isReady() {
// TODO Auto-generated method stub
return false;
}
@Override
public void setReadListener(ReadListener readListener) {
/
/ TODO Auto-generated method stub
}
}
}
}
休息终点:
@RestController()
public class HomeController {
@PostMapping(value = "/v1")
public Response handleMessage(@RequestBody Transaction transaction, HttpServletRequest request,
org.zalando.logbook.HttpRequest requestv, HttpServletResponse response) throws Exception {
// Get here request and response and log it into DB
}
}
如何将HttpLoggingFilter调⽤到Java⽅法handleMessage中并将请求作为主体字符串获取?可能我可以让它服务并注⼊它?你能给我⼀些如何评估代码的建议吗?
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论