我有一个需求,需要将某些HTTP请求重定向到Spring Boot Web应用/服务,但在请求方面,Spring应用程序不会执行任何操作,而是充当HTTP客户端(另一个服务)和请求的真实目标之间的中转。但是当响应从该目标返回到Spring应用程序时,如果需要,我需要Spring应用程序能够检查响应并可能采取行动。因此:
我迄今为止尝试的最佳方法是设置一个Servlet过滤器:
有什么想法吗?
- HTTP客户端向http://someapi.example.com发出请求
- 网络魔法将请求路由到我的Spring应用程序,例如http://myproxy.example.com
- 在请求上,此应用程序/代理什么也不做,因此请求被转发到http://someapi.example.com
- http://someapi.example.com服务端点将HTTP响应返回到代理
- 代理在http://myproxy.example.com检查此响应,并在将响应返回给原始客户端之前可能发送警报。
我迄今为止尝试的最佳方法是设置一个Servlet过滤器:
@Override
void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
chain.doFilter(request, response)
// How and where do I put my code?
if(responseContainsFizz(response)) {
// Send an alert (don't worry about this code)
}
}
这个有可能实现吗?如果可以,检查和处理响应的代码应该放在哪里?我的代码出现异常,当我尝试从浏览器访问控制器时:
java.lang.IllegalStateException: STREAM
at org.eclipse.jetty.server.Response.getWriter(Response.java:910) ~[jetty-server-9.2.16.v20160414.jar:9.2.16.v20160414]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_92]
rest of stack trace omitted for brevity
有什么想法吗?
afterActionCompletion()
方法。请参阅:http://docs.spring.io/spring/docs/4.3.x/javadoc-api/org/springframework/web/portlet/HandlerInterceptor.html#afterActionCompletion-javax.portlet.ActionRequest-javax.portlet.ActionResponse-java.lang.Object-java.lang.Exception- - Slava Semushin