Spring: Returning empty HTTP Responses with ResponseEntity doesn't work

前端 未结 6 1040
梦如初夏
梦如初夏 2020-12-05 09:47

We are implementing a REST API with Spring (4.1.1.). For certain HTTP requests, we would like to return a head with no body as a response. However, using ResponseEntit

6条回答
  •  攒了一身酷
    2020-12-05 10:03

    According Spring 4 MVC ResponseEntity.BodyBuilder and ResponseEntity Enhancements Example it could be written as:

    ....
       return ResponseEntity.ok().build();
    ....
       return ResponseEntity.noContent().build();
    

    UPDATE:

    If returned value is Optional there are convinient method, returned ok() or notFound():

    return ResponseEntity.of(optional)
    

提交回复
热议问题