Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/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
Java SpringMVC:如何在控制器的参数上添加自定义注释_Java_Spring_Spring Mvc_Annotations - Fatal编程技术网

Java SpringMVC:如何在控制器的参数上添加自定义注释

Java SpringMVC:如何在控制器的参数上添加自定义注释,java,spring,spring-mvc,annotations,Java,Spring,Spring Mvc,Annotations,这是我的控制器类: @Component @RequestMapping("/test") public class MyController { @RequestMapping(value = {"test"}, method = RequestMethod.GET) @ResponseBody public String test(@MyAnnotation String myValue) { return "myValue:"+myValue

这是我的控制器类:

@Component
@RequestMapping("/test")
public class MyController
{
    @RequestMapping(value = {"test"}, method = RequestMethod.GET)
    @ResponseBody
    public String test(@MyAnnotation String myValue)
    {
        return "myValue:"+myValue;
    }
}
此外,下面是我的拦截器类:

public class MyInterceptor implements HandlerInterceptor
{
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
    {
        MyAnnotation annotation = ((HandlerMethod)handler).getMethod().getAnnotation(MyAnnotation.class);
        // TODO
    }
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception
    {
    }
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) throws Exception
    {
    }
}

如果我希望myValue是“test”,那么如何实现自定义注释@MyAnnotation?在MyInterceptor类中做什么

请看一看,哪些应该满足大多数用例的要求;如果我将@MyAnnotation从ElementType.PARAMETER更改为ElementType.METHOD,并且我使用@ModelAttribute,并且我想在interceptor类中动态设置myValue,除非您有什么具体的说明,否则该怎么办?您当前的方法有缺陷,无法工作。您的自定义批注需要一个
HandlerMethodArgumentResolver
实现。我曾经尝试过,但它不起作用。似乎HandlerMethodArgumentResolver已弃用。你能一步一步地举个例子吗?不,不行,如果不行,你就做错了。