SpringBoot Rest Controller Supporting both xml and json with JAXB Pojos + Custom Http Message Converters + Swagger-UI

746 Views Asked by At

Getting following Swagger error when I introduced custom message converters

Unable to render this definition
The provided definition does not specify a valid version field.

Please indicate a valid Swagger or OpenAPI version field. Supported version fields are swagger: "2.0" and those that match openapi: 3.0.n (for example, openapi: 3.0.0).

The error is misleading, I guess its getting confused with the converters. Any Ideas to get it fixed?

I followed - swagger-ui not working with custom XML ObjectMapper (no luck)

Background:

I have generated pojos from xsd(s) through xjc. And I have a rest endpoint which needs to support both xml and json for request/response

We got it working by following [spring documentation][1] section: 22.16.12 Message Converters

Here is what I added in MyConfig

    @Configuration 
    @EnableWebMvc 
    public class MyConfig implements WebMvcConfigurer {
      @Override
      public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new MappingJackson2HttpMessageConverter(objectMapper()));
        converters.add(new MappingJackson2XmlHttpMessageConverter(xmlMapper()));
      }

      @Bean
      @Primary
      public ObjectMapper objectMapper() {
        return new Jackson2ObjectMapperBuilder()
                .modulesToInstall(new JaxbAnnotationModule())
                .build();
      }

      @Bean
      public XmlMapper xmlMapper() {
        return new Jackson2ObjectMapperBuilder()
                .indentOutput(true)
                .defaultUseWrapper(false)
                .serializationInclusion(JsonInclude.Include.NON_EMPTY)
                .modulesToInstall(new JaxbAnnotationModule())
                .createXmlMapper(true)
                .build();
      } 
    }

and my controller

// all the open api annotations //
@RequestMapping(value = "/run",
        produces = {"application/json", "application/xml"},
        consumes = {"application/json", "application/xml"},
        method = RequestMethod.POST)
public ResponseEntity<MyResponse> run(@RequestBody MyRequest request) {
1

There are 1 best solutions below

0
On

Ok its about which objectMapper. I had to clean up and remove '@primary' which made the trick in MyConfig

    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        return new MappingJackson2HttpMessageConverter(new Jackson2ObjectMapperBuilder()
                .modulesToInstall(new JaxbAnnotationModule())
                .build());
    }

    @Bean
    public MappingJackson2XmlHttpMessageConverter mappingJackson2XmlHttpMessageConverter() {
        return new MappingJackson2XmlHttpMessageConverter(new Jackson2ObjectMapperBuilder()
                .indentOutput(true)
                .defaultUseWrapper(false)
                .serializationInclusion(JsonInclude.Include.NON_EMPTY)
                .modulesToInstall(new JaxbAnnotationModule())
                .createXmlMapper(true)
                .build());
    }