我跑很簡單spring-boot
應用:SpringBoot:控制器和過濾器初始化,但是沒有得到所謂的
我有一個簡單的過濾器:
@Component
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// This is getting called !
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// some logic
filterChain.doFilter(request, response);
}
@Override
public void destroy() {
}
}
我有一個控制器返回索引頁:
@Controller
public class HomeController {
@RequestMapping("/")
public String index() {
return "index";
}
}
請求索引頁我的過濾器沒有GETT當雖然我認爲它應該。
在我的日誌我看到:
2016-07-18 11:59:51.840 INFO 15623 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'MyFilter' to: [/*]
我缺少什麼?
編輯: 的意見後,我在這裏看到我的控制器是沒有得到所謂的爲好。所以這不是過濾器的問題,但這是一個更大問題的症狀。
這是我的項目structor:
.
├── Dockerfile
├── build.gradle
├── gradlew
├── settings.gradle
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ └── mypackage
│ │ │ ├── Application.java
│ │ │ ├── GreetingController.java
│ │ │ ├── HomeController.java
│ │ │ └── MyFilter.java
│ │ └── resources
│ │ ├── application.yml
│ │ └── templates
│ │ ├── greeting.html
│ │ └── index.html
我建立一個jar文件與gradle
:
./gradlew clean build
並運行它:
java -jar build/libs/sample-webapp-1.0.0.jar
我'可以lling http://localhost:8080
並獲取index.html
文件(可能不通過控制器)。
春季版本是1.3.6-RELEASE
。
我build.gradle
部分:
buildscript {
repositories {
maven {
url "http://jcenter.bintray.com"
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.6.RELEASE")
}
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-devtools")
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.6.2'
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.6.2'
compile group: 'org.bouncycastle', name: 'bcprov-jdk16', version: '1.46'
testCompile("junit:junit")
}
而你如何測試它沒有被調用?過濾器不做任何事情,也不包含任何日誌記錄。 –
已刪除邏輯以使問題簡短。我正在用intellij進行調試,並且在init和doFiilter函數中都有一個斷點。第一個被調用,第二個不是。 – Shikloshi
您的控制器實際上是否被調用?你正在部署應用程序還是運行嵌入式容器? –