控制器和过滤器已初始化但未被调用

Shi*_*shi 5 java servlets spring-boot

我正在运行非常简单的 Spring Boot 应用程序:

\n
@SpringBootApplication\npublic class Application {\n\n    public static void main(String[] args) {\n        SpringApplication.run(Application.class, args);\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我有一个简单的过滤器:

\n
@Component\npublic class MyFilter implements Filter {\n\n    @Override\n    public void init(FilterConfig filterConfig) throws ServletException     {\n       // This is getting called ! \n    }\n\n    @Override\n    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {\n        // some logic\n        filterChain.doFilter(request, response);\n    }\n\n    @Override\n    public void destroy() {\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我有一个返回索引页的控制器:

\n
@Controller\npublic class HomeController {\n\n    @RequestMapping("/")\n    public String index() {\n        return "index";\n    }\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n

请求索引页面时,我的过滤器没有被调用,尽管我认为应该这样做。

\n

在我的日志中我看到:

\n
2016-07-18 11:59:51.840  INFO 15623 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'MyFilter' to: [/*]\n
Run Code Online (Sandbox Code Playgroud)\n

我缺少什么?

\n

在这里发表评论后,我发现我的控制器也没有被调用。所以这不是过滤器的问题,而是更大问题的征兆。

\n

这是我的项目结构:

\n
.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Dockerfile\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 build.gradle\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 gradlew\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 settings.gradle\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 main\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 java\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 com\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 mypackage\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0         \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Application.java\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0         \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 GreetingController.java\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0         \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 HomeController.java\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0         \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 MyFilter.java\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 resources\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 application.yml\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 templates\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0         \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 greeting.html\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0         \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 index.html\n
Run Code Online (Sandbox Code Playgroud)\n

我正在构建一个 jar 文件gradle

\n

./gradlew clean build

\n

并运行它:

\n

java -jar build/libs/sample-webapp-1.0.0.jar

\n

我正在打电话http://localhost:8080并获取index.html文件(可能不通过控制器)。

\n

春季版本是1.3.6-RELEASE.

\n

我的一部分build.gradle

\n
buildscript {\n    repositories {\n        maven {\n            url "http://jcenter.bintray.com"\n        }\n    }\n    dependencies {\n        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.6.RELEASE")\n    }\n}\n\ndependencies {\n        compile("org.springframework.boot:spring-boot-starter-thymeleaf")\n        compile("org.springframework.boot:spring-boot-devtools")\n        compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.6.2'\n        compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.6.2'\n        compile group: 'org.bouncycastle', name: 'bcprov-jdk16', version: '1.46'\n        testCompile("junit:junit")\n}\n
Run Code Online (Sandbox Code Playgroud)\n

Iss*_*TIF 0

您的过滤器映射/*,因此您对索引的调用应该是http://localhost:8080/index
怎么称呼索引呢?