4

Given the following, kind of basic, REST controller in Spring Boot:

@RestController
public class NotificationController {

    @PostMapping(path = "/api/notification")
    public void handleNotification(@RequestBody @Valid NotificationRequest request) {
        System.out.println(request.getMessage());
    }
}

One requirement is to log the incoming request body before deserializing it to a NotificationRequest. We'd like to have a trace, e.g. if the request is not well-formed. My first idea was to use the HttpServletRequest directly but then I would lose the validation and automatic deserialization.

public void handleNotification(HttpServletRequest httpRequest) {
  // ...
}

Is there some mechanism to log the incoming "raw" request body for this particular endpoint?

Robert Strauch
  • 12,055
  • 24
  • 120
  • 192
  • Does this answer your question? [Spring Boot - How to log all requests and responses with exceptions in single place?](https://stackoverflow.com/questions/33744875/spring-boot-how-to-log-all-requests-and-responses-with-exceptions-in-single-pl) – Chin Huang Nov 01 '21 at 21:36
  • The linked question and answers provide a lot of information and are kind of helpful. In my case, however, I only need the "raw" request for this specific controller method. – Robert Strauch Nov 02 '21 at 20:38

0 Answers0