Closed
Description
Is your feature request related to a problem? Please describe.
By default Spring sets the content type of responses returned with the ProblemDetail
object
to application/problem+json
correctly according to the RFC.
Unfortunately, Springdoc sets these endpoints as they return the defaultProducesMediaType
(springdoc.default-produces-media-type
) too.
Spring configuration:
spring.mvc.problemdetails.enabled=true
springdoc.default-produces-media-type=application/json
Example exception handler:
...
import org.springframework.http.ProblemDetail
@ControllerAdvice
class GlobalExceptionHandler {
@ExceptionHandler
@ResponseStatus(HttpStatus.BAD_REQUEST)
fun myCustomError(exception: MyCustomException) = ProblemDetail.forStatus(HttpStatus.BAD_REQUEST)
}
Endpoint response:
content-type: application/problem+json;charset=UTF-8
Generated yaml (relevant part):
'400':
description: Bad Request
content:
application/json:
schema:
$ref: '#/components/schemas/ProblemDetail'
Describe the solution you'd like
I would like to Springdoc also set the content type for these methods automatically to
application/problem+json
by default, regardless of the defaultProducesMediaType
.
Describe alternatives you've considered
- Setting the produces type on exception handler (supported since Spring Framework 6.2):
Unfortunately Springdoc does not seems to consider the exception handler's produces type yet.
@ExceptionHandler(produces = [MediaType.APPLICATION_PROBLEM_JSON_VALUE])
- Defining it with
@ApiResponse
:It works, but it gets wordy very fast, especially, when I have to duplicate this boilerplate for multiple@ApiResponse(responseCode = "400", content = [Content(mediaType = MediaType.APPLICATION_PROBLEM_JSON_VALUE, schema = Schema(implementation = ProblemDetail::class))])
ExceptionHandler
.
Additional context
- Another feature request could be created for supporting
ExceptionHandler.produces
values for generation.