Home > database >  How to set the response content type in a Spring controller that returns a unit (void)?
How to set the response content type in a Spring controller that returns a unit (void)?

Time:02-10

I have a controller in my Spring project that doesn't return anything. Its response type in the openAPI doc is text, but I want to change it to application/json. I already added produces = [MediaType.APPLICATION_JSON_VALUE] to my @RequestMapping annotation, but it just works for functions returning a value. The only way I found is to add this return statement, but I am going to find a general solution.

return ResponseEntity.status(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON).build()

What is the best solution?

CodePudding user response:

if your response doesn't return anything you shouldn't be returning a 200 but a 204. Then you also don't need to set the content type since there is no content.

  • Related