Skip to content

Set default content type for problem details object to application/problem+json #2963

Closed
@T-bond

Description

@T-bond

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):
    @ExceptionHandler(produces = [MediaType.APPLICATION_PROBLEM_JSON_VALUE])
    Unfortunately Springdoc does not seems to consider the exception handler's produces type yet.
  • Defining it with @ApiResponse:
     @ApiResponse(responseCode = "400", content = [Content(mediaType = MediaType.APPLICATION_PROBLEM_JSON_VALUE, schema = Schema(implementation = ProblemDetail::class))])
    It works, but it gets wordy very fast, especially, when I have to duplicate this boilerplate for multiple ExceptionHandler.

Additional context

  • Another feature request could be created for supporting ExceptionHandler.produces values for generation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions