Globally set the response content schema for specific status codes #2880
              
                Unanswered
              
          
                  
                    
                      jochenberger
                    
                  
                
                  asked this question in
                Q&A
              
            Replies: 1 comment 1 reply
-
| 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.
                  
                    1 reply
                  
                
            
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
        
    
Uh oh!
There was an error while loading. Please reload this page.
-
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,Personends 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