简体   繁体   中英

Swagger-ui not working with jackson-dataformat-xml

I'm building a rest API with Spring 4.3.18 and swagger documentation**(springfox 2.8.0) . Everything works fine but if I add to my **pom.xml jackson-dataformat-xml dependency, swagger-ui.html won't show anymore. I think it's a problem about Spring http converters, cause looks like Spring takes the xml converter when I try to go to swagger-ui.html which needs the json converter. Actually I don't know how I can fix this, I really need the jackson dependency, so I can't remove it. How Can I tell Spring to use the json converter for that url ?I also noticed that when I try to reach swagger-ui.html my browser send in the http headers accept : text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8, then Spring uses the xml converter to send the response, but swagger-ui needs json to work. Thanks in advance.

swagger ui doesn't work well with jackson-dataformat-xml. you can try the following.

  1. You need to add header of content-type: application/json. if you need to call any api (eg /v2/api-docs), you need to add a http header to accept application/json through postman.

  2. or ensure that the jackson message converter has higher priority than xml. for springboot, you can add.

@Bean
public RequestMappingHandlerAdapter requestHandler() {
    RequestMappingHandlerAdapter adapter = new RequestMappingHandlerAdapter();
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    List<MediaType> mediaTypeList = new ArrayList<>();
    mediaTypeList.add(MediaType.APPLICATION_JSON);
    converter.setSupportedMediaTypes(mediaTypeList);
    adapter.getMessageConverters().add(converter);
    return adapter;
}
  1. easiest. sorry. remove the jackson-dataformat-xml dependency. try JAXB or something else.

you can read more here https://github.com/springfox/springfox/issues/1835

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM