Spring MVC中创建自定义的注释,并获得HttpServletRequest对象 [英] creating custom annotation in spring mvc and getting httpservletrequest object

查看:1174
本文介绍了Spring MVC中创建自定义的注释,并获得HttpServletRequest对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建自定义的注释,并使用的HttpServletRequest 对象把那个标注方法上水平。到目前为止,我这样做:

I want to create custom annotation and put that annotation on method level using HttpServletRequest object. so far I did this:

创建注释

@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;
    }

}

创建了一个控制器来调用的注释是这样的。

created a controller to invoke annotation like this.

@Controller
public class MyController 
{
@RequestMapping(method={RequestMethod.GET, RequestMethod.POST})
@CheckSession(isAuthenticate=true)
    public ResponseEntity<String> mymethod (HttpServletRequest request)
    {
            ///my code here....
        }
}

我applicationContext.xml文件配置为汽车零部件扫描,但仍是我的注解类是没有得到任何人called.can让我知道我的错误。

My applicationContext.xml file is configured as auto component scan , but still my annotations class is not getting called.can anyone let me know my mistake.

推荐答案

您还必须配置在拦截你的应用环境(尽管自动扫描):

You still have to configure the interceptor in your application context (in spite of the auto-scan):

<bean id="checkSession" class="CheckSessionClass"/>

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

这篇关于Spring MVC中创建自定义的注释,并获得HttpServletRequest对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆