Подробный журнал запросов и ответов для SpringBoot - PullRequest
0 голосов
/ 02 января 2019

Ruby on Rails обеспечивает ведение журнала по умолчанию для объектов «Запрос», «Ответ» в контроллере, а также времени и т. Д. Есть ли способ добиться того же в случае Spring Boot без необходимости писатьрегистрировать операторы для вывода на печать запроса, ответа, времени и т. д.

PS: в фляге Python есть что-то вроде до и после аннотаций, но я не уверен, как мы можем это сделатьвыполнить Rich Rails, как вход в Spring Boot.

Ответы [ 2 ]

0 голосов
/ 08 января 2019

Как насчет этого?

@Configuration
public class ApplicationConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(InterceptorRegistry registry){
        registry.addInterceptor(new ControllerInterceptor()).addPathPatterns(ControllerInterceptor.PATTERN);
    }

public class ControllerInterceptor extends HandlerInterceptorAdapter {

    public static final String PATTERN = "/mycontrollermappingvalue*";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {
        System.out.println("Before request");
        //log values from HttpServletRequest
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
        ModelAndView modelAndView) throws Exception {
         System.out.println("After request");
        //log values from HttpServletResponse
    }
}
0 голосов
/ 05 января 2019

Я думаю, что вы ищете Java-ориентированное программирование. Взгляните на этот пример.

Вот пример записи конфигурации, которая завершает вызовы методов журналирования.

<aop:config> 
  <aop:aspect id="aspectService" ref="logAspect" > 
     <aop:pointcut id="pointCutBeforeBC"
    expression="execution(* com.test.application.service.*.*(..))" />
      <aop:before method="logBefore" pointcut-ref="pointCutBefore" /> 
  </aop:aspect> 

  <aop:aspect id="aspectUserInterface" ref="logAspect" > 
     <aop:pointcut id="pointCutBeforeUserInterfaceBA"
    expression="execution(* com.test.application.ui.*(..))" />
      <aop:before method="pointCutBeforeTraceInput" pointcut-ref="pointCutBeforeUserInterface" /> 
      <aop:after-throwing method="pointCutAfterThrowingOutput" throwing="_Throwable" pointcut-ref="pointCutBeforeUserInterface" />


  </aop:aspect> 
</aop:config>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...