我有一个servlet来处理某些HTTP请求和响应.我想在发送回客户端之前记录响应正文.有没有什么办法可以在HttpServletResponse从servlet 作为对象发送之前捕获响应主体?
我使用Spring的@ExceptionHandler注释来捕获控制器中的异常.
有些请求将POST数据保存为写入请求体的纯XML字符串,我想读取该数据以便记录异常.问题是,当我在异常处理程序中请求输入流并尝试从中读取时,流返回-1(空).
异常处理程序签名是:
@ExceptionHandler(Throwable.class)
public ModelAndView exception(HttpServletRequest request, HttpServletResponse response, HttpSession session, Throwable arff)
Run Code Online (Sandbox Code Playgroud)
有什么想法吗?有没有办法访问请求正文?
我的控制器:
@Controller
@RequestMapping("/user/**")
public class UserController {
static final Logger LOG = LoggerFactory.getLogger(UserController.class);
@Autowired
IUserService userService;
@RequestMapping("/user")
public ModelAndView getCurrent() {
return new ModelAndView("user","response", userService.getCurrent());
}
@RequestMapping("/user/firstLogin")
public ModelAndView firstLogin(HttpSession session) {
userService.logUser(session.getId());
userService.setOriginalAuthority();
return new ModelAndView("user","response", userService.getCurrent());
}
@RequestMapping("/user/login/failure")
public ModelAndView loginFailed() {
LOG.debug("loginFailed()");
Status status = new Status(-1,"Bad login");
return new ModelAndView("/user/login/failure", "response",status);
}
@RequestMapping("/user/login/unauthorized")
public ModelAndView unauthorized() {
LOG.debug("unauthorized()");
Status …Run Code Online (Sandbox Code Playgroud) 我必须实现的逻辑是将所有请求记录到提供给DB的正文.
所以我决定使用:afterCompletion方法HandlerInterceptor.有传递给此方法两个参数HttpServletRequest和HttpServletResponse其他人之间.
问题是:如何获取RequestBody和ResponseBody提供的对象?
据我所知,我们可以使用@RequestBody和@ResponseBody.我可以重复使用HandlerInterceptor吗?