Christian Nilsson
Christian Nilsson

Reputation: 750

How to log web request and response in Spring?

I want to know what parts of my web page the users use and then present it in a nice graphic interface. The only thing I could come up with was this:

@Controller
public class WebController {

    @Autowired
    private DatabaseResource dbr;

    @RequestMapping("/foobar")
    public String getFoobar(Principal principal, ModelMap model, HttpServletRequest req) {
    dbr.logVisit(req); //write the request to a database
 //...

At each @RequestMapping I write the request to the database. Is this the best way to do it?

Does it exists a library for this so I do not have to write it all my self? A package that works a bit like Eclipse Usage Data Collector.

I'm using Spring and Spring Security.

Upvotes: 0

Views: 1620

Answers (1)

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340993

Write a custom HandlerInterceptor, this feature is specificaly designed to implement features like this.

Another approach is to use Logback Access that tightly and seamlessly integrates with Tomcat.

Upvotes: 1

Related Questions