0

I'm wasting a lot of time trying to figure out how to set a default MediaType for my Spring Controllers in the output of the api-docs generated by SpringFox (Docket openapi v3.0). Lastly I've found an almost undocumented interface OperationBuilderPlugin that theorically allows me to set some properties in the OperationContext but unfortunately though the property I'm looking for seems to be ignored when the operation is built:

@Override
public void apply(OperationContext context) {
    context.operationBuilder()
            .produces(new LinkedHashSet<>(
                Collections.singletonList(MediaType.APPLICATION_JSON_VALUE)));
}

Also tried to set the produces directly into Docket but still no luck

@Bean
public Docket api() {
    ....
    return new Docket(DocumentationType.OAS_30)
                .securityContexts(Collections.singletonList(securityContext()))
                .securitySchemes(Collections.singletonList(authenticationScheme))
                .useDefaultResponseMessages(true)
                .consumes(new HashSet<>(Arrays.asList(MediaType.APPLICATION_JSON_VALUE,
                        MediaType.APPLICATION_XML_VALUE)))
                .produces(new LinkedHashSet<>(Arrays.asList("application/json", "application/xml")))
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    ...
}

I don't want to specify the return MediaType at controller class level (or method level) so my last chance if I can't find a cleaner solution is to manually replace the */* with the mediatype i want after I download the api-docs.json from the remote url.

Does anybody ever had the same issue? Any help would be much appreciated, thanks in advance

G-Host
  • 356
  • 2
  • 11

1 Answers1

0

Managed to resolve by myself. In the end I've overridden the bean ResponseMessagesReader with another bean MyResponseMessagesReader that do almost the same of the original class, but in the method below I've changed the condition of produces that in the case of an empty set it adds the json media type

private void applyMyReturnTypeOverride(OperationContext context) {
 ...
      if (produces.isEmpty()) {
        produces.add(MediaType.APPLICATION_JSON);
      }
 ...
}

This is the only joint point I was able to slip into the processo of operation creation, maybe there's a better way (implementing OperationBuilderPlugin with some neatest strategy) but my time was ticking out for this solution.

Hope this helps other people that needs the same API generation behaviour

G-Host
  • 356
  • 2
  • 11