Logging Spring REST APIs











up vote
0
down vote

favorite
1












I have custom Filter and I want to log body from request.
But when I use ContentCachingRequestWrapper and try to call getContentAsByteArray() I always get an empty array.



@Component
public class CustomFilter implements Filter {

private final Logger log = LoggerFactory.getLogger(CustomFilter.class);

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest requestToCache = new ContentCachingRequestWrapper(request);

chain.doFilter(req, res);

log.info(getRequestData(requestToCache));
}

@Override
public void init(FilterConfig filterConfig) {
}

@Override
public void destroy() {
}

public static String getRequestData(final HttpServletRequest request) throws UnsupportedEncodingException {
String payload = null;
ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
if (wrapper != null) {
byte buf = wrapper.getContentAsByteArray();
if (buf.length > 0) {
payload = new String(buf, 0, buf.length, wrapper.getCharacterEncoding());
}
}
return payload;
}
}


I also tried create Interceptor, but had the same problem.
What am I doing wrong?
Thanks for help.










share|improve this question
























  • Is this not caused by the fact you're creating a ContentCachingRequestWrapper before your call to getRequestData, where it's creating another ContentCachingRequestWrapper? I suspect because it reads the body when you create the first ContentCachingRequestWrapper that it can't re-read from the buffer when creating the second and therefore the body is null/empty. You could always pass the first ContentCachingRequestWrapper into your method rather than creating a second instance.
    – jste89
    Nov 10 at 17:35










  • I also used it once and had the same result.
    – user_name
    Nov 10 at 17:39















up vote
0
down vote

favorite
1












I have custom Filter and I want to log body from request.
But when I use ContentCachingRequestWrapper and try to call getContentAsByteArray() I always get an empty array.



@Component
public class CustomFilter implements Filter {

private final Logger log = LoggerFactory.getLogger(CustomFilter.class);

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest requestToCache = new ContentCachingRequestWrapper(request);

chain.doFilter(req, res);

log.info(getRequestData(requestToCache));
}

@Override
public void init(FilterConfig filterConfig) {
}

@Override
public void destroy() {
}

public static String getRequestData(final HttpServletRequest request) throws UnsupportedEncodingException {
String payload = null;
ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
if (wrapper != null) {
byte buf = wrapper.getContentAsByteArray();
if (buf.length > 0) {
payload = new String(buf, 0, buf.length, wrapper.getCharacterEncoding());
}
}
return payload;
}
}


I also tried create Interceptor, but had the same problem.
What am I doing wrong?
Thanks for help.










share|improve this question
























  • Is this not caused by the fact you're creating a ContentCachingRequestWrapper before your call to getRequestData, where it's creating another ContentCachingRequestWrapper? I suspect because it reads the body when you create the first ContentCachingRequestWrapper that it can't re-read from the buffer when creating the second and therefore the body is null/empty. You could always pass the first ContentCachingRequestWrapper into your method rather than creating a second instance.
    – jste89
    Nov 10 at 17:35










  • I also used it once and had the same result.
    – user_name
    Nov 10 at 17:39













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





I have custom Filter and I want to log body from request.
But when I use ContentCachingRequestWrapper and try to call getContentAsByteArray() I always get an empty array.



@Component
public class CustomFilter implements Filter {

private final Logger log = LoggerFactory.getLogger(CustomFilter.class);

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest requestToCache = new ContentCachingRequestWrapper(request);

chain.doFilter(req, res);

log.info(getRequestData(requestToCache));
}

@Override
public void init(FilterConfig filterConfig) {
}

@Override
public void destroy() {
}

public static String getRequestData(final HttpServletRequest request) throws UnsupportedEncodingException {
String payload = null;
ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
if (wrapper != null) {
byte buf = wrapper.getContentAsByteArray();
if (buf.length > 0) {
payload = new String(buf, 0, buf.length, wrapper.getCharacterEncoding());
}
}
return payload;
}
}


I also tried create Interceptor, but had the same problem.
What am I doing wrong?
Thanks for help.










share|improve this question















I have custom Filter and I want to log body from request.
But when I use ContentCachingRequestWrapper and try to call getContentAsByteArray() I always get an empty array.



@Component
public class CustomFilter implements Filter {

private final Logger log = LoggerFactory.getLogger(CustomFilter.class);

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest requestToCache = new ContentCachingRequestWrapper(request);

chain.doFilter(req, res);

log.info(getRequestData(requestToCache));
}

@Override
public void init(FilterConfig filterConfig) {
}

@Override
public void destroy() {
}

public static String getRequestData(final HttpServletRequest request) throws UnsupportedEncodingException {
String payload = null;
ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
if (wrapper != null) {
byte buf = wrapper.getContentAsByteArray();
if (buf.length > 0) {
payload = new String(buf, 0, buf.length, wrapper.getCharacterEncoding());
}
}
return payload;
}
}


I also tried create Interceptor, but had the same problem.
What am I doing wrong?
Thanks for help.







java spring logging filter interceptor






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 17:34









Hülya

42719




42719










asked Nov 10 at 17:26









user_name

32




32












  • Is this not caused by the fact you're creating a ContentCachingRequestWrapper before your call to getRequestData, where it's creating another ContentCachingRequestWrapper? I suspect because it reads the body when you create the first ContentCachingRequestWrapper that it can't re-read from the buffer when creating the second and therefore the body is null/empty. You could always pass the first ContentCachingRequestWrapper into your method rather than creating a second instance.
    – jste89
    Nov 10 at 17:35










  • I also used it once and had the same result.
    – user_name
    Nov 10 at 17:39


















  • Is this not caused by the fact you're creating a ContentCachingRequestWrapper before your call to getRequestData, where it's creating another ContentCachingRequestWrapper? I suspect because it reads the body when you create the first ContentCachingRequestWrapper that it can't re-read from the buffer when creating the second and therefore the body is null/empty. You could always pass the first ContentCachingRequestWrapper into your method rather than creating a second instance.
    – jste89
    Nov 10 at 17:35










  • I also used it once and had the same result.
    – user_name
    Nov 10 at 17:39
















Is this not caused by the fact you're creating a ContentCachingRequestWrapper before your call to getRequestData, where it's creating another ContentCachingRequestWrapper? I suspect because it reads the body when you create the first ContentCachingRequestWrapper that it can't re-read from the buffer when creating the second and therefore the body is null/empty. You could always pass the first ContentCachingRequestWrapper into your method rather than creating a second instance.
– jste89
Nov 10 at 17:35




Is this not caused by the fact you're creating a ContentCachingRequestWrapper before your call to getRequestData, where it's creating another ContentCachingRequestWrapper? I suspect because it reads the body when you create the first ContentCachingRequestWrapper that it can't re-read from the buffer when creating the second and therefore the body is null/empty. You could always pass the first ContentCachingRequestWrapper into your method rather than creating a second instance.
– jste89
Nov 10 at 17:35












I also used it once and had the same result.
– user_name
Nov 10 at 17:39




I also used it once and had the same result.
– user_name
Nov 10 at 17:39












2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










You can use the existing spring implementation by just registering this bean in a @Configuration annotated class:



@Bean
public static Filter requestLoggingFilter() {
final CommonsRequestLoggingFilter loggingFilter = new CommonsRequestLoggingFilter();
loggingFilter.setIncludePayload(true);
loggingFilter.setMaxPayloadLength(512);

return loggingFilter;
}





share|improve this answer




























    up vote
    0
    down vote













    Whilst I'd recommend going with NiVer's answer, I've been looking into why this issue occurs and I can finally give you an answer.



    When you create a new ContentCachingRequestWrapper, the internal ByteArrayOutputStream is initialized but no data is copied to it. The body is only written to the ByteArrayOutputStream when you call getParameter, getParameterMap(), getParameterNames() or getParameterValues(String name) methods, and even then the data is only copied if the content type contains application/x-www-form-urlencoded.






    share|improve this answer





















    • I saw empty array and could`t understand why. Thank you for help!
      – user_name
      Nov 11 at 8:33











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241554%2flogging-spring-rest-apis%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    You can use the existing spring implementation by just registering this bean in a @Configuration annotated class:



    @Bean
    public static Filter requestLoggingFilter() {
    final CommonsRequestLoggingFilter loggingFilter = new CommonsRequestLoggingFilter();
    loggingFilter.setIncludePayload(true);
    loggingFilter.setMaxPayloadLength(512);

    return loggingFilter;
    }





    share|improve this answer

























      up vote
      2
      down vote



      accepted










      You can use the existing spring implementation by just registering this bean in a @Configuration annotated class:



      @Bean
      public static Filter requestLoggingFilter() {
      final CommonsRequestLoggingFilter loggingFilter = new CommonsRequestLoggingFilter();
      loggingFilter.setIncludePayload(true);
      loggingFilter.setMaxPayloadLength(512);

      return loggingFilter;
      }





      share|improve this answer























        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        You can use the existing spring implementation by just registering this bean in a @Configuration annotated class:



        @Bean
        public static Filter requestLoggingFilter() {
        final CommonsRequestLoggingFilter loggingFilter = new CommonsRequestLoggingFilter();
        loggingFilter.setIncludePayload(true);
        loggingFilter.setMaxPayloadLength(512);

        return loggingFilter;
        }





        share|improve this answer












        You can use the existing spring implementation by just registering this bean in a @Configuration annotated class:



        @Bean
        public static Filter requestLoggingFilter() {
        final CommonsRequestLoggingFilter loggingFilter = new CommonsRequestLoggingFilter();
        loggingFilter.setIncludePayload(true);
        loggingFilter.setMaxPayloadLength(512);

        return loggingFilter;
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 17:35









        NiVeR

        6,47141930




        6,47141930
























            up vote
            0
            down vote













            Whilst I'd recommend going with NiVer's answer, I've been looking into why this issue occurs and I can finally give you an answer.



            When you create a new ContentCachingRequestWrapper, the internal ByteArrayOutputStream is initialized but no data is copied to it. The body is only written to the ByteArrayOutputStream when you call getParameter, getParameterMap(), getParameterNames() or getParameterValues(String name) methods, and even then the data is only copied if the content type contains application/x-www-form-urlencoded.






            share|improve this answer





















            • I saw empty array and could`t understand why. Thank you for help!
              – user_name
              Nov 11 at 8:33















            up vote
            0
            down vote













            Whilst I'd recommend going with NiVer's answer, I've been looking into why this issue occurs and I can finally give you an answer.



            When you create a new ContentCachingRequestWrapper, the internal ByteArrayOutputStream is initialized but no data is copied to it. The body is only written to the ByteArrayOutputStream when you call getParameter, getParameterMap(), getParameterNames() or getParameterValues(String name) methods, and even then the data is only copied if the content type contains application/x-www-form-urlencoded.






            share|improve this answer





















            • I saw empty array and could`t understand why. Thank you for help!
              – user_name
              Nov 11 at 8:33













            up vote
            0
            down vote










            up vote
            0
            down vote









            Whilst I'd recommend going with NiVer's answer, I've been looking into why this issue occurs and I can finally give you an answer.



            When you create a new ContentCachingRequestWrapper, the internal ByteArrayOutputStream is initialized but no data is copied to it. The body is only written to the ByteArrayOutputStream when you call getParameter, getParameterMap(), getParameterNames() or getParameterValues(String name) methods, and even then the data is only copied if the content type contains application/x-www-form-urlencoded.






            share|improve this answer












            Whilst I'd recommend going with NiVer's answer, I've been looking into why this issue occurs and I can finally give you an answer.



            When you create a new ContentCachingRequestWrapper, the internal ByteArrayOutputStream is initialized but no data is copied to it. The body is only written to the ByteArrayOutputStream when you call getParameter, getParameterMap(), getParameterNames() or getParameterValues(String name) methods, and even then the data is only copied if the content type contains application/x-www-form-urlencoded.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 10 at 19:13









            jste89

            356314




            356314












            • I saw empty array and could`t understand why. Thank you for help!
              – user_name
              Nov 11 at 8:33


















            • I saw empty array and could`t understand why. Thank you for help!
              – user_name
              Nov 11 at 8:33
















            I saw empty array and could`t understand why. Thank you for help!
            – user_name
            Nov 11 at 8:33




            I saw empty array and could`t understand why. Thank you for help!
            – user_name
            Nov 11 at 8:33


















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241554%2flogging-spring-rest-apis%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma