1

I used the solution given in https://stackoverflow.com/a/16250729/351903 -

@Path("api/path")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public ResponseEntity<String> handshakeForTxn()
    {

       return new ResponseEntity<String>(HttpStatus.BAD_REQUEST);
    }

I still get a 200 response status -

Server responded with a response on thread XNIO-3 task-2 2 < 200 2 < Content-Type: application/json 

Although the response body contains a statusCodeValue which is set to 400 -

{ "headers": {}, "body": null, "statusCode": "BAD_REQUEST", "statusCodeValue": 400 }
Sandeepan Nath
  • 9,966
  • 17
  • 86
  • 144

3 Answers3

0
  1. You need to remove

    @Produces(MediaType.APPLICATION_JSON)

RestController return JSON by default.

  1. You controller should look like

    @RestController
    public class RegistrationController {
    
    @GetMapping("api/path")
    public ResponseEntity register() {
        return ResponseEntity.badRequest().build();
    }
    
    }
    
  2. You could check code example here. https://github.com/alex-petrov81/stackoverflow-answers/tree/master/bad-request-show-200

Alexander Petrov
  • 951
  • 6
  • 11
0

The correct way to do this would be

Response.noContent().status(Response.Status.NOT_FOUND).build()

Response.ok().entity("").build();

Robert Ellis
  • 714
  • 6
  • 19
0

This worked for me -

For 200 -

return Response.status(Response.Status.OK).entity("").build();

For 400 -

return Response.status(Response.Status.BAD_REQUEST).entity("").build(); 
Sandeepan Nath
  • 9,966
  • 17
  • 86
  • 144