Swagger 2 accept xml instead of json

Related searches

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!

Request header content type set to application/xhtml+xml instead of , I use swagger2 with to document my spring boot application. When I call the the request header says that it accept XML but the api return json. I am, however, able to see the documentation in XML format. As a result, I am unable to use swagger-ui to display swagger's autogenerated API documentation since it makes use of the JSON response. My REST API is configured to return both JSON and XML data. I am using spring 3.2.1.RELEASE and swagger-springmvc 0.6.3; Example:

What worked for me was updating the Maven dependencies for springfox-swagger2 and springfox-swagger-ui.

The newest working combination for me was:

    <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.8.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>
    </dependency>

Media Types, If you use OpenAPI 2 (fka Swagger), visit OpenAPI 2 pages. can accept and return data in different formats, the most common being JSON, XML and images. If you use OpenAPI 2 (fka Swagger), visit OpenAPI 2 pages. Media Types. Media type is a format of a request or response body data. Web service operations can accept and return data in different formats, the most common being JSON, XML and images. You specify the media type in request and response definitions. Here is an example of a response

Representing XML, In your API specification, you can describe data in both XML and JSON formats You can use this object to transform some properties to attributes rather than� OAS 2 This page applies to OpenAPI Specification ver. 2 (fka Swagger). To learn about the latest version, visit OpenAPI 3 pages. MIME Types An API can accept and return data in different formats, the most common being JSON and XML. You can use the consumes and produces keywords to specify the MIME types understood by your API.

MIME Types, OAS 2 This page applies to OpenAPI Specification ver. 2 (fka An API can accept and return data in different formats, the most common being JSON and XML. I am trying to upgrade to 2.7.0 from 2.6.1 in my spring boot application but am unable to get the json to render in the browser at the /v2/api-docs endpoint (works correctly for the 2.6.1 version and is working for swagger-ui.html) chrom

Describing Responses, paths: /ping: get: produces: - application/json; responses: 200: description: OK object or array – typically used with JSON and XML APIs,; a primitive such as a number https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0. md# SwaggerHub Enterprise � SwaggerHub vs OSS � SwaggerHub Integrations. OAS 2 This page applies to OpenAPI Specification ver. 2 (fka Swagger). To learn about the latest version, visit OpenAPI 3 pages. Describing Responses An API specification needs to specify the responses for all API operations. Each operation must have at least one response defined, usually a successful response.

OAS 2 This page applies to OpenAPI Specification ver. 2 (fka Swagger). To learn about the latest version, visit OpenAPI 3 pages. Describing Request Body. The POST, PUT and PATCH requests can have the request body (payload), such as JSON or XML data. In Swagger terms, the request body is called a body parameter. There can be only one body

Comments
  • Didn't work for me. Implementing this solution changed the server's response ContentType from "application/xhtml..." to "text/html", but didn't change it to "application/json" :(