In Web Client, returning ResponseEntity containing both Body and Http Status Code(both success and failure) JAVA spring boot

Viewed 31

I am writing an blocking web client. I want to return both body(class) and http status code in ResponseEntity object to be used in some method. Can you please help with this. I am new to Java and already tried approaches many mentioned on internet.

1 Answers

You can use ResponseEntity class!

For e.g. Assume you want to return your Model/Pojo as below

  1. Model class -> MyResponse (Has fields, getter/setters,toString etc) you want to respond along with HTTP code

  2. Code snippet

@GetMapping("/getresponse) //Or any mapping

public ResponseEntity getResponse()
{

MyResponse response=new MyResponse();

return new ResponseEntity>(response,HttpStatus.OK);

} 
Related