Hackforge Academy

Category: spring_boot

Logging REST API Requests and Responses

Published on 08 Jul 2026

Explanation

Logging helps monitor API requests, responses, execution time, and errors.

Code:

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

Explanation

Log incoming requests inside controller methods.

Code:

log.info("Fetching student with id {}", id);

Explanation

Log successful responses before returning data.

Code:

log.info("Student retrieved successfully");

Explanation

Use HandlerInterceptor to log every incoming request.

Code:

public boolean preHandle(HttpServletRequest request,HttpServletResponse response,Object handler){
    System.out.println(request.getMethod()+" "+request.getRequestURI());
    return true;
}

Explanation

Use filters to log request and response processing time.

Code:

long start=System.currentTimeMillis();
filterChain.doFilter(request,response);
System.out.println(System.currentTimeMillis()-start);

๐Ÿš€ Learn Spring Boot with real-world projects

๐Ÿ’ก Build REST APIs step by step

๐Ÿง  Improve backend development skills

๐ŸŽฏ Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

๐Ÿ

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
โš›๏ธ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
โ˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community