繁体   English   中英

swagger 中的响应内容类型,.net core api

[英]Response content types in swagger, .net core api

我在大摇大摆地将我的 api 响应呈现为 xml 时遇到了一些问题,最后我在下面的第一个操作中以正确的格式 application/xml 呈现它,但它仍然说我只能将它呈现为 application/json。

我试图从 Produces 属性中删除 application/json,但仍然只显示 application/json。

任何想法为什么它以这种方式表现?

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc().AddMvcOptions(o => o.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter()));
    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
        c.RoutePrefix = "";
    });
    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
        c.AddSecurityDefinition("Bearer", new ApiKeyScheme { In = "header", Description = "Please enter JWT with Bearer into field", Name = "Authorization", Type = "apiKey" });
        c.AddSecurityRequirement(new Dictionary<string, IEnumerable<string>> {
            { "Bearer", Enumerable.Empty<string>() },
        });
    });
}

和行动:

[Produces("application/json", "application/xml")]
public ContentResult GetByAddress(string address)
{
    var addressId = GetAddressIdByAddress(address);
    string result = _soapApi.GetResultById(addressId);
    return Content(result, "application/xml", Encoding.UTF8);
}

相同的结果:

[Produces("application/json", "application/xml")]
public IActionResult GetByAddress(string address)
{
    var addressId = GetAddressIdByAddress(address);
    string result = _soapApi.GetResultById(addressId);
    return Content(result, "application/xml", Encoding.UTF8);
}

结果是:

结果在这 当点击执行时,我得到了正确的 XML 格式的结果。

虽然这样:

[Produces("application/json", "application/xml")]
public string GetByAddress(string address)
{
    var addressId = GetAddressIdByAddress(address);
    string result = _soapApi.GetResultById(addressId);
    return result;
}

或这个:

[Produces("application/json", "application/xml")]
public List<Address> GetByAddreses()
{
    string result = _soapApi.GetResults();
    return result;
}

结果是:

在此处输入图像描述

由于为不同的参数返回不同的数据结构,我无法返回已解析对象的列表。 所以此时我只收到原始 soap xml 数据并且必须解析/反序列化它。 但为了真正能够看到原始响应,我还需要将其显示为 Content from string with content-type application/xml。 它给了我一个很好的输出(即使它仍然说只有 application/json 是可能的):

在此处输入图像描述

总结一下:

当一个字符串被解析为如下所示的实际格式时,我没有让它与显示正确内容类型的响应内容类型一起工作(在“响应内容类型”-filter 中)。 即使这会导致最重要的 application/xml 的正确输出(请参见上面的屏幕截图);

[Produces("application/json", "application/xml")]
public ContentResult GetByAddress(string address)
{
    return Content("<xml>...</xml>", "application/xml", Encoding.UTF8);
} 

这是因为返回类型覆盖了注释。 要让它按您的意愿工作,您需要返回 IActionResult 并使用注释来提供特定代码的响应类型模型。
所以而不是返回result ,返回Ok(result)

我创建了示例代码,它适用于我使用 swagger 4.0.1

[HttpGet("Get2")]
[Produces("application/json", "application/xml", Type = typeof(List<string>))]
public IActionResult Get2()
{
   var list = new List<string>() { "value1", "value2" };
   return Ok(list);
}

招摇输出

[Produces("application/json", "application/xml")]
public Task<IActionResult> GetByAddreses()
{
    var result = _soapApi.GetResults();
var response = ResponceMethod<List<Address>>.SuccessResponse(StatusCodes.Status200OK, "Success", result);
    return Ok(response);
}

我正在测试用 asp.net core2.2 开发的 web api 应用程序。

我安装了Microsoft.AspNetCore.Mvc.Formatters.Xml nuget 包。

然后我通过添加更新了 Startup.cs 中的ConfigureServices方法。

services.AddMvc().AddXmlSerializerFormatters();

我的 web api swagger 正在按我的需要工作,下拉菜单显示(json 和 xml)并生成预期的格式。 如果您需要更多信息,请访问: https ://learn.microsoft.com/en-us/aspnet/core/web-api/advanced/formatting?view=aspnetcore-2.1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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