How to return 200 OK with a custom message in SpringBoot with ResponseEntity


1. The best simple way

return new Greeting(1, "Hello");


Becasue Default HttpCode = 200 OK and in this case the result will be with Postman as below

{
    "id": 1,
    "content": "Hello"
}




2. The second way


return ResponseEntity.ok(new AuthToken(token));




3. The next way


return new ResponseEntity<AuthToken>(new AuthToken(token), HttpStatus.OK);

4. The next way


return ResponseEntity.status(HttpStatus.OK).body(new AuthToken(token));

5. The next way with no response body


return ResponseEntity.status(HttpStatus.OK).build();



Comments

Popular posts from this blog

Fixing the DeepSpeed Import Error While Fine-Tuning the Qwen Model

Amazon Linux 2023 - User data configuration for launch templates to connect to the EKS cluster

How to create ISM policy and rotate logs in opensearch