简体   繁体   中英

When the RestApi has ResponseEntity<void> of spring has return value it returns ClientProtocolException

I have a RestApi exposed which on case returns status ok back to client. The method signature of the method is ResponseEntity<void> methodName(){} . This method is a deleteApi.

In the return responseEntity is created with just Status OK and no body or any other header details are appended.

Seen in the logs that I get ClientProtocolException when the call is made , saw that when the same is executed through the REST client (postman) received correct Status OK message in the response.

  • What are the reasons when the ClientProtocolException is raised?

  • If the return type is ResponeEntity<Void> is it mandatory to send body with it ?

  • How do i avoid getting the above exception?

Code:

 @RequestMapping(method = RequestMethod.DELETE)
public ResponseEntity<Void> methodName()
{
   // your business logic 
    return new ResponseEntity<Void>(HttpStatus.OK);
}

Try as below

@RequestMapping(method = RequestMethod.DELETE)
public ResponseEntity<Void> methodName() { 
    // your business logic 
    return ResponseEntity.noContent().build();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM