代码之家  ›  专栏  ›  技术社区  ›  Sandeepan Nath

在API响应中获取HTTP响应代码200,即使在返回带有HttpStatus.BAD_请求的ResponseEntity之后

  •  0
  • Sandeepan Nath  · 技术社区  · 6 年前

    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);
        }
    

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

    尽管响应体包含设置为400的statusCodeValue-

    { "headers": {}, "body": null, "statusCode": "BAD_REQUEST", "statusCodeValue": 400 }
    
    3 回复  |  直到 6 年前
        1
  •  0
  •   Alexander Petrov    6 年前
    1. 你需要移除

    RestController默认返回JSON。

    1. 你的控制器应该看起来像

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

        2
  •  0
  •   Sandeepan Nath    6 年前

    这对我有效-

    200英镑-

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

    400英镑-

    return Response.status(Response.Status.BAD_REQUEST).entity("").build(); 
    
        3
  •  0
  •   Manoj Ramanan    6 年前

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