Skip to content
Advertisement

POST: Exception Status expected: but was:

Technically it should be a simple task, but I can’t find the error.

I want to write a normal “POST method”, but when I tested it, it came to a problem: enter code here Status expected:<201> but what:<200>.

My question is, why do I get an OK and not a CREATED?

CODE:

PostMapping in Controller

JavaScript

Unit Test

JavaScript

Advertisement

Answer

Because for a controller method that executes successfully and does not return ResponseEntity , the default response code is 200.

To configure the response code for such case , you can simply annotate @ResponseStatus on that controller method :

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement