Swagger 2接受xml而不是json [英] Swagger 2 accept xml instead of json

查看:2197
本文介绍了Swagger 2接受xml而不是json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有spring boot的项目,我想使用swagger2来记录我的json Web服务。



我有这样的配置:

  @Configuration 
@EnableSwagger2
public class Swagger2Config {

@Bean
public Docket welcomeMessageApi (){
返回新的Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}

私人ApiInfo apiInfo(){
返回新的ApiInfoBuilder()
.title(我的API)
.description(Lorem Ipsum只是......的虚拟文本)
.termsOfServiceUrl(an url)
.contact(contact)
.license()
.licenseUrl( )
.version(2.0)
.build();
}

要阅读文档,我使用以下链接:



使用Firebug,我看到它接受XML内容而不是JSON内容。



如何修改swagger配置以接受JSON内容?

解决方案

您遇到问题是因为Spring MVC默认让服务器在浏览器中呈现XML而不是JSON。



supportedJiason2XmlHttpMessageConverter supportedMediaTypes 的值为:





MappingJackson2XmlHttpMessageConverter 。这就是浏览器渲染xml的json的原因。



所以你需要添加一个cust om MappingJackson2XmlHttpMessageConverter 支持'text / xml',例如:

  @Configuration 
公共类WebConfig扩展WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(List< HttpMessageConverter<?>>转换器){
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
列表< MediaType> list = new ArrayList<>();
list.add(MediaType.APPLICATION_JSON_UTF8);
list.add(new MediaType(text,html,Charset.forName(UTF-8)));
list.add(new MediaType(application,* + json,Charset.forName(UTF-8)));
converter.setSupportedMediaTypes(list);
converters.add(converter);
}
}

试试这个,浏览器会渲染JSON而不是XML浏览器,一切正常!


I have a project with spring boot and I want to use swagger2 to document my json web services.

I have this configuration :

@Configuration
@EnableSwagger2
public class Swagger2Config {

@Bean
public Docket welcomeMessageApi() {
    return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any())
            .build();
}

private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
            .title("My API")
            .description("Lorem Ipsum is simply dummy text of ...")
            .termsOfServiceUrl("an url")
            .contact("contact")
            .license("")
            .licenseUrl("")
            .version("2.0")
            .build();
}

To read the documentation, I use this link : http://localhost:9081/v2/api-docs

In the swagger UI, it works fine. But when I try this link directly in my browser, I have this error :

With Firebug, I see that it accept XML content instead of JSON content.

How can I modify swagger configuration to accept JSON content ?

解决方案

You meet the problem because of the Spring MVC default get the server to render XML instead of JSON in a browser. The official document say:

To get the server to render XML instead of JSON you might have to send an Accept: text/xml header (or use a browser).

So all you need to do is make the server render JSON in browser.

When you deep into the request in browser you'll see the Request Header:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8

And if you debug into the spring boot, you will see the spring mvc will default delegate HttpMessageConverters include MappingJackson2XmlHttpMessageConverter and MappingJackson2HttpMessageConverter.

The MappingJackson2HttpMessageConverter is to render json and MappingJackson2XmlHttpMessageConverter is to render xml.

They both have a field supportedMediaTypes which means what mediatypes are supported.

The value of supportedMediaTypes in MappingJackson2HttpMessageConverter is:

The value of supportedMediaTypes in MappingJackson2XmlHttpMessageConverter is:

There is a 'text/xml;charset=UTF-8' in MappingJackson2XmlHttpMessageConverter.This is why browser render xml instend of json.

So you need add a custom MappingJackson2XmlHttpMessageConverter which support 'text/xml', for example :

    @Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        List<MediaType> list = new ArrayList<>();
        list.add(MediaType.APPLICATION_JSON_UTF8);
        list.add(new MediaType("text", "html", Charset.forName("UTF-8")));
        list.add(new MediaType("application", "*+json", Charset.forName("UTF-8")));
        converter.setSupportedMediaTypes(list);
        converters.add(converter);
    }
}

Try this and browser will render JSON instead of XML in browser, and all things right!

这篇关于Swagger 2接受xml而不是json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆