swagger部署后提示实体类冲突解决方案

使用Web API并使用swashbuckle生成swagger文档,我在两个不同的命名空间中定义了两个具有相同名称的不同类。当我在浏览器中打开swagger页面时,它说:

1 Conflicting schemaIds: Duplicate schemaIds detected for types A and B. See the config setting - “UseFullTypeNameInSchemaIds” for a potential workaround

完整消息:

 1 {
 2     "message": "An error has occurred.",
 3     "exceptionMessage": "Conflicting schemaIds: Duplicate schemaIds detected for types Amo.Common.AjaxResult`1[[System.Collections.Generic.List`1[[Amo.UsdtCoin.UsMerchantClient+oListTransactionResult, Amo.UsdtCoin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] and Amo.Common.AjaxResult`1[[System.Collections.Generic.List`1[[Amo.BtcCoin.MerchantClient+oListTransactionResult, Amo.BitCoin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]. See the config setting - \"UseFullTypeNameInSchemaIds\" for a potential workaround",
 4     "exceptionType": "System.InvalidOperationException",
 5     "stackTrace": " 在 Swashbuckle.Swagger.SchemaRegistry.CreateRefSchema(Type type)\r\n
 6      在 Swashbuckle.Swagger.SchemaRegistry.CreateInlineSchema(Type type)\r\n 
 7      在 Swashbuckle.Swagger.SchemaRegistry.GetOrRegister(Type type)\r\n 
 8      在 Swashbuckle.Swagger.SwaggerGenerator.CreateOperation(ApiDescription apiDesc, SchemaRegistry schemaRegistry)\r\n 
 9      在 Swashbuckle.Swagger.SwaggerGenerator.CreatePathItem(IEnumerable`1 apiDescriptions, SchemaRegistry schemaRegistry)\r\n 
10      在 Swashbuckle.Swagger.SwaggerGenerator.<>c__DisplayClass7.<GetSwagger>b__4(IGrouping`2 group)\r\n 
11      在 System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)\r\n 
12      在 Swashbuckle.Swagger.SwaggerGenerator.GetSwagger(String rootUrl, String apiVersion)\r\n 
13      在 Amo.Client.CachingSwaggerProvider.GetSwagger(String rootUrl, String apiVersion) 位置 E:\\codes\\USDTSolution\\Amo.Client\\App_Start\\SwaggerConfig.cs:行号 301\r\n 
14      在 Swashbuckle.Application.SwaggerDocsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n 
15      在 System.Net.Http.HttpMessageInvoker.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n 
16      在 System.Web.Http.Dispatcher.HttpRoutingDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n 
17      在 System.Net.Http.DelegatingHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n 在 System.Web.Http.HttpServer.<SendAsync>d__24.MoveNext()"
18 }

我不想改变类的名字。我该如何解决?

解决方法1:

1 services.ConfigureSwaggerGen(options =>
2 {
3 //your custom configuration goes here
4
5 …
6
7 // UseFullTypeNameInSchemaIds replacement for .NET Core
8 options.CustomSchemaIds(x => x.FullName);
9 });

解决方法2:

1 GlobalConfiguration.Configuration
2 .EnableSwagger(c =>
3 {
4 // your configs…
5
6 c.SchemaId(x => x.FullName);
7
8 // other configs…
9 })
10 .EnableSwaggerUi(c =>
11 // …
12 });

解决方法3:

1 c.UseFullTypeNameInSchemaIds();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你在使用 Swagger 2.4.0 时遇到了实体类不显示的问题,可能是因为你的实体类没有添加正确的注解或者路径配置不正确。 首先,确保你已经在实体类上添加了 `@ApiModel` 注解,该注解用于指定实体类的名称和描述,例如: ```java @ApiModel(value = "User", description = "用户信息") public class User { ... } ``` 其次,你需要在实际使用该实体类的 API 方法上添加 `@ApiOperation` 注解,并指定请求参数的路径和参数类型,例如: ```java @ApiOperation(value = "添加用户", notes = "根据 User 对象添加用户") @ApiImplicitParam(name = "user", value = "用户实体类", required = true, dataType = "User") @RequestMapping(value = "/addUser", method = RequestMethod.POST) public String addUser(@RequestBody User user) { ... } ``` 注意,这里的 `dataType` 属性应该与实体类的名称保持一致,否则 Swagger 将无法正确识别该实体类。 最后,你需要确保 Swagger 的配置文件中包含了正确的路径,例如: ```java @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo")) .paths(PathSelectors.any()) .build(); } } ``` 其中,`RequestHandlerSelectors.basePackage` 方法指定了需要扫描的 API 包路径,而 `PathSelectors.any()` 方法则表示扫描所有路径。如果你的实体类路径没有被正确扫描到,Swagger 将无法显示该实体类

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值