Skip to content

Commit f0742d2

Browse files
committed
BAEL-3335 example of reading request multiple times. removed spring boot and using spring-webmvc
1 parent 30c1f5e commit f0742d2

File tree

4 files changed

+18
-27
lines changed

4 files changed

+18
-27
lines changed

spring-core-2/src/main/java/org/baeldung/cachedrequest/ContentCachingFilter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import javax.servlet.FilterChain;
66
import javax.servlet.ServletException;
7+
import javax.servlet.annotation.WebFilter;
78
import javax.servlet.http.HttpServletRequest;
89
import javax.servlet.http.HttpServletResponse;
910

@@ -14,9 +15,12 @@
1415

1516
@Order(value = Ordered.HIGHEST_PRECEDENCE)
1617
@Component
18+
@WebFilter(filterName = "ContentCachingFilter", urlPatterns = "/*")
1719
public class ContentCachingFilter extends OncePerRequestFilter {
20+
1821
@Override
1922
protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
23+
System.out.println("IN ContentCachingFilter ");
2024
CachedBodyHttpServletRequest cachedBodyHttpServletRequest = new CachedBodyHttpServletRequest(httpServletRequest);
2125
filterChain.doFilter(cachedBodyHttpServletRequest, httpServletResponse);
2226
}
Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,17 @@
11
package org.baeldung.cachedrequest;
22

3-
import org.springframework.context.annotation.Bean;
3+
import org.springframework.context.annotation.ComponentScan;
44
import org.springframework.context.annotation.Configuration;
5-
import org.springframework.web.servlet.ViewResolver;
65
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
7-
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
86
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
9-
import org.springframework.web.servlet.view.InternalResourceViewResolver;
10-
import org.springframework.web.servlet.view.JstlView;
7+
/**
8+
* To initialize the WebApplication, Please see
9+
* {@link org.baeldung.spring.config.MainWebAppInitializer}
10+
*/
1111

1212
@EnableWebMvc
1313
@Configuration
14+
@ComponentScan(basePackages = "org.baeldung.cachedrequest")
1415
public class HttpRequestDemoConfig implements WebMvcConfigurer {
1516

16-
public HttpRequestDemoConfig() {
17-
super();
18-
}
19-
20-
// API
21-
22-
@Override
23-
public void addViewControllers(final ViewControllerRegistry registry) {
24-
registry.addViewController("/sample.html");
25-
}
26-
27-
@Bean
28-
public ViewResolver viewResolver() {
29-
final InternalResourceViewResolver bean = new InternalResourceViewResolver();
30-
31-
bean.setViewClass(JstlView.class);
32-
bean.setPrefix("/WEB-INF/view/");
33-
bean.setSuffix(".jsp");
34-
35-
return bean;
36-
}
3717
}

spring-core-2/src/main/java/org/baeldung/cachedrequest/Person.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ public class Person {
77

88
private int age;
99

10+
public Person() {
11+
}
12+
1013
public Person(String firstName, String lastName, int age) {
1114
this.firstName = firstName;
1215
this.lastName = lastName;

spring-core-2/src/main/java/org/baeldung/cachedrequest/PrintRequestContentFilter.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package org.baeldung.cachedrequest;
22

3+
34
import java.io.IOException;
45
import java.io.InputStream;
56

67
import javax.servlet.FilterChain;
78
import javax.servlet.ServletException;
9+
import javax.servlet.annotation.WebFilter;
810
import javax.servlet.http.HttpServletRequest;
911
import javax.servlet.http.HttpServletResponse;
1012

@@ -16,12 +18,14 @@
1618

1719
@Order(Ordered.LOWEST_PRECEDENCE)
1820
@Component
21+
@WebFilter(filterName = "printRequestContentFilter", urlPatterns = "/*")
1922
public class PrintRequestContentFilter extends OncePerRequestFilter {
2023
@Override
2124
protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
25+
System.out.println("IN PrintRequestContentFilter ");
2226
InputStream inputStream = httpServletRequest.getInputStream();
2327
byte[] body = StreamUtils.copyToByteArray(inputStream);
24-
System.out.println("In PrintRequestContentFilter." + " Request body is: " + new String(body));
28+
System.out.println("In PrintRequestContentFilter. Request body is: " + new String(body));
2529
filterChain.doFilter(httpServletRequest, httpServletResponse);
2630
}
2731
}

0 commit comments

Comments
 (0)