Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.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
Java 在SpringMVC中创建自定义注释并获取httpservletrequest对象_Java_Spring_Spring Mvc_Annotations_Spring Annotations - Fatal编程技术网

Java 在SpringMVC中创建自定义注释并获取httpservletrequest对象

Java 在SpringMVC中创建自定义注释并获取httpservletrequest对象,java,spring,spring-mvc,annotations,spring-annotations,Java,Spring,Spring Mvc,Annotations,Spring Annotations,我想创建自定义注释,并使用HttpServletRequest对象将该注释放在方法级别。到目前为止,我做到了: 创建注释 @Target(value={ElementType.METHOD,ElementType.PARAMETER}) @Retention(value=RetentionPolicy.RUNTIME) @Documented @Inherited @Mapping public @interface CheckSession{ boolean isAuthenticat

我想创建自定义注释,并使用
HttpServletRequest
对象将该注释放在方法级别。到目前为止,我做到了:

创建注释

@Target(value={ElementType.METHOD,ElementType.PARAMETER})
@Retention(value=RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Mapping
public @interface CheckSession{
    boolean isAuthenticate() default false;
}
已创建处理程序类

@Component
public class CheckSessionClass implements HandlerMethodReturnValueHandler,HandlerMethodArgumentResolver {

    @Override
    public Object resolveArgument(MethodParameter arg0,
            ModelAndViewContainer arg1, NativeWebRequest arg2,
            WebDataBinderFactory arg3) throws Exception {
        logger.info("......MY ANNOTATION CALLEDD.....resolveArgument");
        return null;
    }


    @Override
    public boolean supportsParameter(MethodParameter arg0) {
        logger.info("......MY ANNOTATION CALLEDD.....supportsParameter");
        return false;
    }


    @Override
    public void handleReturnValue(Object retutnValue, MethodParameter returnType,
            ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
        CheckSession annotation;
        annotation=returnType.getMethodAnnotation(CheckSession.class);
        if(annotation.isAuthenticate()){
logger.info("......got request is aurhenticated..true");
        }else{
            logger.info("......got request is aurhenticated..false");
        }
    }


    @Override
    public boolean supportsReturnType(MethodParameter arg0) {
        logger.info("......MY ANNOTAION CALLEDD.....supportsReturnType");
        return false;
    }
    
}
创建了一个控制器来调用这样的注释

@Controller
public class MyController 
{
    @RequestMapping(method={RequestMethod.GET, RequestMethod.POST})
    @CheckSession(isAuthenticate=true)
    public ResponseEntity<String> mymethod (HttpServletRequest request)
    {
            ///my code here....
    }
}
@控制器
公共类MyController
{
@RequestMapping(方法={RequestMethod.GET,RequestMethod.POST})
@CheckSession(isAuthenticate=true)
公共响应属性mymethod(HttpServletRequest)
{
///我的代码在这里。。。。
}
}

我的applicationContext.xml文件已配置为自动组件扫描,但仍未调用我的annotations类。有人能告诉我我的错误吗。

您仍然必须在应用程序上下文中配置拦截器(尽管自动扫描):



我尝试了这个方法,但它给出了**java.lang.IllegalArgumentException:不支持拦截器类型**它不是拦截器。我遇到了相同的问题:java.lang.IllegalArgumentException:不支持拦截器类型。。。有什么提示吗?@LorenzoSciuto你能试着用defaultHandler代替拦截器吗?(我已经更新了答案)
<bean id="checkSession" class="CheckSessionClass"/>

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    <property name="defaultHandler">
        <ref bean="checkSession"/>
    </property>
</bean>