-
Notifications
You must be signed in to change notification settings - Fork 38.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve docs for getContentAsByteArray method of ContentCachingRequestWrapper #27068
Comments
I'm not sure I understand your point here. This Using the following sample application: package com.example.contentcaching;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.filter.AbstractRequestLoggingFilter;
import org.springframework.web.servlet.function.RouterFunction;
import org.springframework.web.servlet.function.RouterFunctions;
import org.springframework.web.servlet.function.ServerResponse;
@SpringBootApplication
public class ContentCachingApplication {
Logger logger = LoggerFactory.getLogger(ContentCachingApplication.class);
public static void main(String[] args) {
SpringApplication.run(ContentCachingApplication.class, args);
}
@Bean
MyRequestLoggingFilter customLoggingFilter() {
MyRequestLoggingFilter loggingFilter = new MyRequestLoggingFilter();
loggingFilter.setIncludePayload(true);
return loggingFilter;
}
@Bean
RouterFunction<ServerResponse> routerFunction() {
return RouterFunctions.route().POST(request -> ServerResponse.ok().body(request.body(String.class))).build();
}
public static class MyRequestLoggingFilter extends AbstractRequestLoggingFilter {
@Override
protected void beforeRequest(HttpServletRequest request, String message) {
logger.info("BEFORE: " + message);
}
@Override
protected void afterRequest(HttpServletRequest request, String message) {
logger.info("AFTER: " + message);
}
}
} And the following commands on the CLI:
I'm getting the following logs, which show that the request body can be re-read after the request has been processed, so this means the content has been cached::
Could you elaborate a bit? Do you have a sample application that illustrates your point? |
@bclozel thank you for your quick response! I think that I was too fast with submitting this issue. You are totally right with As a context I need to apply specific logic to some of requests in my application. I have to decide if logic should be applied based on request headers and request body (to be precise GET, POST, PUT, PATCH, DELETE methods are in use). To avoid Example code which does not work - public static class MyFilterWithLogic extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
String content = request.getReader().lines().collect(Collectors.joining());
//some logic with content
filterChain.doFilter(request, response);
}
} Example code which does not work - public static class MyFilterWithLogic extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
ContentCachingRequestWrapper wrappedRequest = new ContentCachingRequestWrapper(request);
String content = new String(wrappedRequest.getContentAsByteArray());
//some logic with content
filterChain.doFilter(wrappedRequest, response);
}
} Example code which does work - public static class MyFilterWithLogic extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
ContentCachingRequestWrapper wrappedRequest = new ContentCachingRequestWrapper(request);
String content = wrappedRequest.getReader().lines().collect(Collectors.joining());
//some logic with content
filterChain.doFilter(wrappedRequest, response);
}
} After reading documentation carefully I know that But, for me, method
I think that I can be one of the few who interpret this docs and implementation like this but maybe it is will be better to improve method name and/or java docs. What do you think? 😄 |
It wouldn't hurt to improve that a bit. |
Maybe a |
Affects: 5.3.7
Java Doc in
org.springframework.web.util.ContentCachingRequestWrapper
says that "wrapper that caches all content" wihtout any information that only request withPOST
method and content typeapplication/x-www-form-urlencoded
are cached (isFormPost
method is responsible for that).IMHO it is misleading that you have to see class internals to see for what requests content is cached.
Am I right or I am missing something? :)
PS. I can provide PR with docs update if you agree with me.
The text was updated successfully, but these errors were encountered: