Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.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 仅为控制器配置Spring侦听器_Java_Spring_Spring Mvc_Model View Controller - Fatal编程技术网

Java 仅为控制器配置Spring侦听器

Java 仅为控制器配置Spring侦听器,java,spring,spring-mvc,model-view-controller,Java,Spring,Spring Mvc,Model View Controller,我试图用以下方式为控制器配置Spring拦截器。首先,我想排除以/swagger开头的所有请求。我试着用以下方法来做: registry.addInterceptor(myInterceptor).addPathPatterns("/**").excludePathPatterns("/swagger**"); @ControllerAdvice public class SomeAdvice { @ModelAttribute public void token(HttpS

我试图用以下方式为控制器配置Spring拦截器。首先,我想排除以
/swagger
开头的所有请求。我试着用以下方法来做:

registry.addInterceptor(myInterceptor).addPathPatterns("/**").excludePathPatterns("/swagger**");
@ControllerAdvice
public class SomeAdvice {

    @ModelAttribute
    public void token(HttpServletRequest request) {
        // getting headers and setting the attribute in the request
        request.setAttribute("theAttribute", new SomeObject());
    }
}
然而,拦截器被触发。哪里有错误

也许有另一种@ControllerAdvice解决方案。但是我需要得到请求头,所以我想它不适合我的需要


谢谢你的帮助

尝试使用
“/swagger*/**”
“/swagger*”
而不是
“/swagger**”
我通过以下方式解决了问题:

registry.addInterceptor(myInterceptor).addPathPatterns("/**").excludePathPatterns("/swagger**");
@ControllerAdvice
public class SomeAdvice {

    @ModelAttribute
    public void token(HttpServletRequest request) {
        // getting headers and setting the attribute in the request
        request.setAttribute("theAttribute", new SomeObject());
    }
}
然后我通过以下方式在控制器中获取请求属性:

public void someMethod(@RequestAttribute("theAttribute") SomeObject someObject) {
    // some logic goes here
}
还有一张纸条。如果你使用<代码> Swagger <代码>,你会遇到麻烦,因为<代码> Swagger < /C>会将此属性视为Controller方法参数。要忽略它,可以使用以下配置快照:

.ignoredParameterTypes(SomeObject.class);

我尝试了第二种方法,但不起作用。第一个也是,我刚试过。我用另一种方法解决了我的问题。