2015-05-04 933 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(); 
相關問題