加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何在Spring AOP中获取HttpServletRequest和HttpServletRespons

发布时间:2020-12-15 01:26:33 所属栏目:大数据 来源:网络整理
导读:我想在建议之前在Spring AOP中获取响应对象.如果会话无效,我想重定向到登录页面,但无法在Before advice方法中获取HttpServletResponse对象. 尝试以下方式. @Autowired private HttpServletResponse response; public void setResponse(HttpServletResponse r

我想在建议之前在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

任何帮助将不胜感激.

最佳答案
基本上我们从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

步骤3:在UI代码中的所有ajax请求中,检查此状态代码并重定向到登录页面.

就是这样,你完成了更精细的实现.保证…

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读