我想在建议之前在Spring AOP中获取响应对象。如果会话无效,我想重定向到登录页面,但无法在Before advice方法中获取HttpServletResponse对象。
尝试以下方式。
@Autowired
private HttpServletResponse response;
public void setResponse(HttpServletResponse response) {
this.response = response;
}
堆栈跟踪:
caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: javax.servlet.http.HttpServletResponse com.****.****.aspect.LogProvider.response; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [javax.servlet.http.HttpServletResponse] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
... 33 more
任何帮助将不胜感激。
答案 0 :(得分:5)
您可以通过以下方式获得回复:
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
HttpServletResponse response = ((ServletRequestAttributes)requestAttributes).getResponse();
答案 1 :(得分:1)
基本上我们从jsp页面重定向,即从UI层我们处理这种操作(重定向)。所以,我希望你将在你的应用程序中使用一些宁静的服务。对于大多数休闲服务,我们都会选择异步请求。如果它是异步和宁静服务的组合;我相信你会在你的申请中使用它。如果您的会话无效,并且您尝试访问会话'会话'那么它会让你陷入“IllegalStateException”#39;对于此类情况,请遵循以下集中式“异常处理”#39; JAX-RS提供的机制: javax.ws.rs.ext.ExceptionMapper 。 请按照以下步骤操作: step-1:创建用户定义的未经检查的异常,例如MyApplicationException:
public class MyApplicationException extends RuntimeException {
public MyApplicationException() {super();}
// implement other methods of RuntimeException as per your requirement
}
step-2:创建用户定义类型的ExceptionMapper
public class MyApplicationExceptionHandler implements ExceptionMapper<MyApplicationException>
{
@Override
public Response toResponse(MyApplicationException exception)
{
return Response.status(Status.FORBIDDEN).entity(exception.getMessage()).build();
// set any Status code of 4XX as this is client side error not server side
}
}
第3步: In all your ajax request in the UI code check this Status Code and redirect to the login page.
这就是它,你完成了更精细的实现。保证...
答案 2 :(得分:0)
/**
* @return the HttpServletResponse handled by the current thread
*/
public static Optional<HttpServletResponse> getThreadLocalResponse() {
return Optional.ofNullable(RequestContextHolder.getRequestAttributes())
.filter(ra -> ra instanceof ServletRequestAttributes)
.map(ServletRequestAttributes.class::cast)
.map(ServletRequestAttributes::getResponse);
}
答案 3 :(得分:-2)
要获取响应对象,您可以使用以下代码:
ServletWebRequest servletWebRequest=new ServletWebRequest(request);
HttpServletResponse response=servletWebRequest.getResponse();
获取请求对象:
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest();
如果您收到null
响应,那么我可以看到在返回控件时尚未形成响应。然后,唯一的方法是使用interceptors
。