Skip to content

Commit 7974d8f

Browse files
authored
Add simple filters for a chain.doFilter() presentation (#18045)
* BAEL-6550 Add simple filter for chain.doFilter presentation * BAEL-6550 Move code example to another module and rename test class * BAEL-6550 Align code example with blog changes * BAEL-6550 Fix filter chain example, improve test
1 parent cf780dd commit 7974d8f

File tree

6 files changed

+120
-1
lines changed

6 files changed

+120
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.chaindofilter;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication(scanBasePackages = "com.baeldung.chaindofilter")
7+
public class ChainDoFilterApplication {
8+
public static void main(String[] args) {
9+
SpringApplication.run(ChainDoFilterApplication .class, args);
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.chaindofilter;
2+
3+
import org.springframework.http.ResponseEntity;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
@RestController
9+
@RequestMapping("/chain/do-filter")
10+
public class ChainDoFilterController {
11+
12+
public static final String RESPONSE_MESSAGE = "Chain doFilter test!";
13+
14+
@GetMapping
15+
public ResponseEntity<String> chainDoFilter() {
16+
return ResponseEntity.ok(RESPONSE_MESSAGE);
17+
}
18+
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.chaindofilter;
2+
3+
import java.io.IOException;
4+
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.core.annotation.Order;
8+
import org.springframework.stereotype.Component;
9+
10+
import jakarta.servlet.Filter;
11+
import jakarta.servlet.FilterChain;
12+
import jakarta.servlet.ServletException;
13+
import jakarta.servlet.ServletRequest;
14+
import jakarta.servlet.ServletResponse;
15+
16+
@Order(1)
17+
@Component
18+
public class FirstFilter implements Filter {
19+
20+
private final Logger LOG = LoggerFactory.getLogger(FirstFilter.class);
21+
22+
@Override
23+
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
24+
LOG.info("Processing the First Filter");
25+
chain.doFilter(request, response);
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.chaindofilter;
2+
3+
import java.io.IOException;
4+
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.core.annotation.Order;
8+
import org.springframework.stereotype.Component;
9+
10+
import jakarta.servlet.Filter;
11+
import jakarta.servlet.FilterChain;
12+
import jakarta.servlet.ServletException;
13+
import jakarta.servlet.ServletRequest;
14+
import jakarta.servlet.ServletResponse;
15+
16+
@Order(2)
17+
@Component
18+
public class SecondFilter implements Filter {
19+
20+
private final Logger LOG = LoggerFactory.getLogger(SecondFilter.class);
21+
22+
@Override
23+
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
24+
LOG.info("Processing the Second Filter");
25+
chain.doFilter(request, response);
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.chaindofilter;
2+
3+
import static com.baeldung.chaindofilter.ChainDoFilterController.RESPONSE_MESSAGE;
4+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
5+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
6+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
7+
8+
import org.junit.jupiter.api.Test;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
11+
import org.springframework.boot.test.context.SpringBootTest;
12+
import org.springframework.test.web.servlet.MockMvc;
13+
14+
@SpringBootTest
15+
@AutoConfigureMockMvc
16+
public class ChainExampleFilterUnitTest {
17+
18+
@Autowired
19+
private MockMvc mockMvc;
20+
21+
@Test
22+
void givenHeaderInResponse_whenFilterDoChain_thenAssertHeaderPresent() throws Exception {
23+
mockMvc.perform(get("/chain/do-filter"))
24+
.andExpect(status().isOk())
25+
.andExpect(jsonPath("$").value(RESPONSE_MESSAGE));
26+
}
27+
28+
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<configuration>
3-
<root level="info">
3+
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder>
5+
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
6+
</pattern>
7+
</encoder>
8+
</appender>
9+
10+
<root level="INFO">
411
<appender-ref ref="CONSOLE" />
512
</root>
613
</configuration>

0 commit comments

Comments
 (0)