我发现InvocableHandlerMethod.invokeForRequest类将从请求中获取请求参数,并调用控制器类以获取返回值。 我应该怎样修改将参数写入控制台的方法? 我想扩展ServletInvocableHandlerMethod并重写方法invokeForRequest,但由于它是私有的,所以无法调用getMethodArgumentValues。是否应该复制ServletInvocableHandlerMethod和InvocableHandlerMethod的类来修改私有方法?还是有另一种方式记录请求和响应参数而没有方面?
答案 0 :(得分:0)
只需创建拦截器
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class LoggingInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// log here
return true; // let go further
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
// log here
}
}
并注册
// example for Spring MVC annotation-based configuration
public class YourWebConfig extends WebMvcConfigurer {
...
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoggingInterceptor());
}
...
}