How return a JSON response instead of an HTML error code like 401 or 400 when the JWT token is not valid, expired, or not used

Hello guys, i need help

with following the tutorial in this link ‘Implementing JSON Web Token (JWT) Authentication using Spring Security | A Detailed Walkthrough | by Sayan Das | Geek Culture | Medium’, i can create jwt token authorization but how to return json response instead of an HTML error code like 401 or 400 when the JWT token is not valid, expired, or not used ?

Hey @ronnyjubhari !

This might be a question better suited for Stack Overflow or another Spring Security group.

Postman is an HTTP client and does not control the server’s response format. If you set the Accept header in Postman to application/json and you’re still getting HTML responses, you’ll need to modify your server-side code to return JSON responses when an error occurs.

There is a Spring Security feature called an AuthenticationEntryPoint that you can implement. This feature is a starting point for handling exceptions related to authentication (like an expired JWT).

oke, thank you for your response and suggestion