Globally set the response content schema for specific status codes #2880
Unanswered
jochenberger
asked this question in
Q&A
Replies: 1 comment
-
I may just have found a solution myself: @Bean
public OperationCustomizer errorResponseSchemas(
final SpringDocConfigProperties springDocConfigProperties) {
Schema schema =
AnnotationsUtils.resolveSchemaFromType(
ErrorResponse.class, null, null, springDocConfigProperties.isOpenapi31());
Content content =
new Content()
.addMediaType(
org.springframework.http.MediaType.APPLICATION_JSON_VALUE,
new MediaType().schema(schema));
return (operation, handlerMethod) -> {
operation
.getResponses()
.forEach(
(status, response) -> {
if (HttpStatus.resolve(Integer.valueOf(status)).is4xxClientError()) {
response.setContent(content);
}
});
return operation;
};
} Is this how it's supposed to be done? Is this the correct way to get hold of the |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
We have a lot of methods that return a specific type of object, e.g.
Now everywhere I want to document the 404 (or any other error) response, I need to specify the content schema
ErrorResponse
, or otherwise,Person
ends up as the content schema for the 404 response.Is it possible to define that mapping application-wide so I don't have to repeat it in so many places?
Beta Was this translation helpful? Give feedback.
All reactions