๐Ÿ”ด The Error You're Seeing

Confirm this matches your console output. If it does, you're in the right place.

ERROR LOG2026-02-20 10:05:22.120 ERROR 8842 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.web.server.ResponseStatusException: 400 BAD_REQUEST "Invalid input"] with root cause org.springframework.web.server.ResponseStatusException: 400 BAD_REQUEST "Invalid input"

โšก Quick Fix Works 80% of the time

Fix the business logic that caused the exception, or catch it in a @RestControllerAdvice to return a custom JSON body.

throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Email already exists");

๐Ÿง  Why this Happens

Tap to expand the deep technical explanation

Your code explicitly threw a `ResponseStatusException` with a 400 status. This is a deliberate action to tell the client their request was invalid. The 'error' in the log is just Spring's default handling of the exception you threw.

The HITEC City Parking Spot Analogy:

It's like a bouncer at a club telling someone they can't enter because they aren't on the list. The bouncer (your code) throws the exception, and the club manager (Spring) reports it to the public.

๐Ÿ” How to Reproduce Confirm this is your error

In a controller method, add `throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid input");`. Call the endpoint.

๐Ÿ› ๏ธ Solutions (5 Ways to Fix)

Solution 1

Fix the triggering logic

๐Ÿ‘‰ Use this if the exception is being thrown unexpectedly.

Check the stack trace to find exactly where you threw the exception and fix the business logic that triggered it.

// If checking for duplicates: if (repo.existsByEmail(email)) { throw new ResponseStatusException(HttpStatus.BAD_REQUEST); // Ensure this is intended }
Solution 2โœ“ Most common cause

Catch it globally to return custom JSON

๐Ÿ‘‰ Use this if you want a cleaner JSON response than Spring's default.

By default, Spring returns a basic error JSON. You can intercept it to add your own structure.

@RestControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(ResponseStatusException.class) public ResponseEntity<String> handleResponseStatus(ResponseStatusException ex) { return ResponseEntity.status(ex.getStatusCode()).body(ex.getReason()); } }
Solution 3

Use custom exceptions instead

๐Ÿ‘‰ Use this for cleaner architecture.

Create your own exception class annotated with @ResponseStatus, making the code more readable than throwing generic ResponseStatusExceptions everywhere.

@ResponseStatus(code = HttpStatus.BAD_REQUEST, reason = "Email exists") public class EmailExistsException extends RuntimeException { ... } // In controller: throw new EmailExistsException();
Solution 4

Return validation errors with ProblemDetail (SB3)

๐Ÿ‘‰ Use this if you are on Spring Boot 3 and want to use RFC 7807 standard errors.

Spring Boot 3 uses ProblemDetail. You can populate it with custom fields.

ProblemDetail pd = ProblemDetail.forStatus(400); pd.setTitle("Invalid Input"); pd.setDetail("Email is required"); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(pd);
Solution 5

Ensure the HTTP status is correct

๐Ÿ‘‰ Use this if you are throwing a 400 for a server-side error.

If the database is down, 400 is wrong. Use 500. If the user is unauthorized, use 401.

throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "DB down");

๐Ÿ“‹ Version Notes

Spring Boot 2.x

Throws ResponseStatusException, returns default error JSON.

Spring Boot 3.x

Returns RFC 7807 ProblemDetail JSON by default.

๐Ÿ›ก๏ธ How to Prevent This Next Time

Use custom domain-specific exceptions instead of throwing generic ResponseStatusExceptions, and map them globally in @RestControllerAdvice.

Course Search
Search across all chapters & stages
๐Ÿ“–

Search the course

Type any topic โ€” branching, stash, rebase, hooks โ€” and jump straight to that chapter.

merge branchesgit stashundo commitrebase