如何让ServiceStack的Swagger实现使用XML内容类型?

时间:2016-02-04 11:36:10

标签: json xml servicestack swagger swagger-ui

我有一个非常基本的ServiceStack实验,它使用Swagger生成文档。该服务可以与几种不同的内容类型(XML,JSON等)一起使用: Default metadata page

但是,我只能在Swagger UI中使用content-type / json。是否有我可以使用的配置/属性,以便我可以测试其他内容类型? Swagger UI

2 个答案:

答案 0 :(得分:1)

您无法更改内容类型Swagger使用的内容,但ServiceStack服务提供automatic Content Negotiation,因此您可以使用.xml扩展名查询XML,例如:

  • /query.xml

或指定?format=查询字符串中的格式:

  • /查询?格式= XML

或者添加Accept: application/xml HTTP请求标头。

Swagger不会更改内容类型,但您可以使用更通用的工具,如ServiceStack's Postman support或Fiddler。

修改Swagger响应

我在ServiceStack的最新版本v4.0.5中只是added a change现在available on MyGet允许您更改ServiceStack返回的Swagger响应,您可以使用它来填充消耗和生成

Plugins.Add(new SwaggerFeature {
    ApiDeclarationFilter = x => 
      x.Consumes = x.Produces = new[] { MimeTypes.Json, MimeTypes.Xml }.ToList(),
    OperationFilter = x => 
      x.Consumes = x.Produces = new[] { MimeTypes.Json, MimeTypes.Xml }.ToList()
});

但这看起来并没有对Swagger UI产生太大影响,我唯一看到的变化是 POST 请求中的 body 参数现在让你发送XML:

enter image description here

答案 1 :(得分:1)

在Swagger规范(由ServiceStack生成)中,您需要为端点更新produces以包含" application / xml"和" application / json"如果你想要两者都出现在swagger-ui的下拉菜单中。

以下是一个例子:

"produces": [
  "application/json",
  "application/xml"
],

参考:https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/test/resources/2_0/petstore.json#L77

OpenAPI Spec produceshttps://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#fixed-fields

相关问题