Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
T-bond opened this issue Apr 8, 2025 · 0 comments

Comments

@T-bond
Copy link

T-bond commented Apr 8, 2025

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant