2015-05-04 934 views
6

我想在建议之前获取Spring AOP中的响应对象。如果会话无效,我想重定向到登录页面,但无法获取Before advice方法中的HttpServletResponse对象。如何在Spring AOP中获取HttpServletRequest和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

而是自动装配的,你尝试过吗? 'HttpServletRequest request =((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes())。getRequest(); ' –

+0

感谢您的回复。我会试着用这个 –

+0

我需要HttpServletResponse对象。 –

回答

1

基本上我们会做一个jsp页面重定向,也就是我们处理这种操作(重定向)的UI层。所以,我希望你会在你的应用程序中使用一些宁静的服务。对于大多数宁静的服务,我们将使用异步请求。如果它是异步和宁静服务的组合;我相信你会在你的应用程序中使用它。如果您的会话无效,并且您尝试访问在'会话'上执行任何操作,那么它会将您置于'IllegalStateException'中。对于这种类型的场景,请按照JAX-RS提供的以下集中式“异常处理”机制:javax.ws.rs.ext.ExceptionMapper。 请按照以下步骤进行: 步骤-1:创建用户定义的未经检查的异常像如MyApplicationException:

public class MyApplicationException extends RuntimeException { 
    public MyApplicationException() {super();} 

    // implement other methods of RuntimeException as per your requirement 
} 

步骤-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 
    } 
} 

step-3:In all your ajax request in the UI code check this Status Code and redirect to the login page.

就是这样,你完成了一个更好的实现。保证...

-2

获得响应对象,你可以使用此代码:

ServletWebRequest servletWebRequest=new ServletWebRequest(request); 
HttpServletResponse response=servletWebRequest.getResponse(); 

要获取请求对象:

HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getR‌equest(); 

如果你得到一个null响应,那么我可以看到响应尚不当控件返回时形成。那么唯一的办法就是与interceptors一起去。

+2

获取响应返回null! –

1

你可以通过以下方法响应:

RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); 
HttpServletResponse response = ((ServletRequestAttributes)requestAttributes).getResponse(); 
相关问题