Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在SpringAOP中获取HttpServletRequest和HttpServletResponse对象_Spring_Spring Mvc_Servlets_Spring Aop - Fatal编程技术网

如何在SpringAOP中获取HttpServletRequest和HttpServletResponse对象

如何在SpringAOP中获取HttpServletRequest和HttpServletResponse对象,spring,spring-mvc,servlets,spring-aop,Spring,Spring Mvc,Servlets,Spring Aop,我想在建议之前在SpringAOP中获取响应对象。如果会话无效,我想重定向到登录页面,但无法在Before advice方法中获取HttpServletResponse对象 用下面的方法尝试 @Autowired private HttpServletResponse response; public void setResponse(HttpServletResponse response) { this.response = response;

我想在建议之前在SpringAOP中获取响应对象。如果会话无效,我想重定向到登录页面,但无法在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

任何帮助都将不胜感激。

要获取响应对象,您可以使用以下代码:

ServletWebRequest servletWebRequest=new ServletWebRequest(request);
HttpServletResponse response=servletWebRequest.getResponse();
要获取请求对象,请执行以下操作:

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

如果您得到一个
null
响应,那么当控件返回时,我可以看到响应尚未形成。接下来的唯一方法是使用截取器,基本上我们从jsp页面重定向,即从UI层处理此类操作(重定向)。因此,我希望您将在应用程序中使用一些restful服务。对于大多数restful服务,我们使用异步请求。如果是异步和restful服务的组合;我相信你会在你的应用程序中使用它。如果您的会话无效,并且您尝试访问“会话”并在“会话”上执行任何操作,则它将使您进入“非法状态异常”。对于此类场景,请遵循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:创建用户定义的例外类型Apper

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
    }
}
公共类MyApplicationExceptionHandler实现ExceptionMapper
{
@凌驾
公共响应(MyApplicationException异常)
{
返回Response.status(status.FORBIDDEN).entity(exception.getMessage()).build();
//将任何状态代码设置为4XX,因为这是客户端错误而不是服务器端错误
}
}
步骤3:
在用户界面代码中的所有ajax请求中,检查此状态代码并重定向到登录页面。


就这样,您就完成了更精细的实现。保证…

您可以通过以下方法获得响应:

RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
HttpServletResponse response = ((ServletRequestAttributes)requestAttributes).getResponse();
/**
*@返回当前线程处理的HttpServletResponse
*/
公共静态可选getThreadLocalResponse(){
返回可选的.ofNullable(RequestContextHolder.getRequestAttributes())
.filter(ServletRequestAttributes的ra->ra实例)
.map(ServletRequestAttributes.class::cast)
.map(ServletRequestAttributes::getResponse);
}

您尝试过自动布线而不是自动布线吗<代码>HttpServletRequest请求=((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest()感谢您的回复。我将尝试此操作。我需要HttpServletResponse对象。抱歉,误读了您的问题。请使用更适合此任务的
过滤器
HandlerInterceptor
。或者使用Spring安全性,它可以为您处理更多信息。获取响应返回null<代码>类型ServletRequestAttributes的getResponse()方法未定义如何获取HttpServletResponse对象?
/**
 * @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);
}