我正在尝试将Swagger添加到我的项目中。收到的错误如下。
无法使用服务容器中的服务和默认值实例化“ Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator”类型的构造函数。
由于我尚未对Swagger二进制文件本身进行任何更改,因此仅安装了软件包Swashbuckle.AspNetCore和Swashbuckle.AspNetCore.Swagger(均为4.0.1版),我假设这与配置有关。按照这里的建议,我已经设置了如下所示的配置。
services.AddSwaggerGen(_ =>
{
_.SwaggerDoc("v1", new Info { Version = "v1", Title = "My API" });
});
app.UseSwagger();
app.UseSwaggerUI(_ => { _.SwaggerEndpoint("/swagger/v1/swagger.json", "API docs"); });
Run Code Online (Sandbox Code Playgroud)
我不确定是否丢失了一个软件包,是否其中一个是错误的版本,或者我提供的设置配置是否足够。
我正在gRPC
使用服务器(使用protobuf
)在 GO 中构建应用程序,并将其包装在 HTTPS 服务器中(使用gin
)。只有 HTTPS 服务器被发布到客户端使用(我的意思是我的应用程序可以通过 REST API 访问,实际上然后在 gRPC 端点上拨号),我使用Swagger
OpenAPI3发布它(版本 3 是主要的此处要求)规范。gRPC 和 HTTPS 都是必需的,任何解决方案都应遵循此架构。
我不想在两个地方维护我的服务器规范,即我不想同时维护 proto 文件 ( .proto
) 和 swagger 规范 ( .json/.yaml
)。由于我绝对必须编写 proto 文件来生成 gRPC 服务器,因此我想自动生成 swagger 规范(OpenAPI3)。
我能够使用grpc-gateway库swagger
从 protobuf 文件 ( .proto
)生成OpenAPI2 规范,如下所示:grpc-rest-go-example。但我的要求是 OpenAPI3;更具体地说,我想使用OpenAPI3 中的功能并从proto 的功能映射到它。这在 OpenAPI2 中是不可能的,因为它不允许 API 具有多个类型定义的请求/响应主体,这是 OpenAPI3 中通过启用 oneOf、anyOf 和 allOf 构造添加的一项功能。oneOf
oneof
在尝试这样做时,我偶然发现了 GoogleAPIs googleapis/ gnostic 的这个库,其描述是:
该存储库包含一个 …
下面是与我的情况类似的代码
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK", content = {@Content(schema = @Schema(
implementation = // <-- What to specify here?
))})
})
@GetMapping(value = "/user")
public ResponseEntity<List<User>> getUsers() {
return ResponseEntity.ok().body(Arrays.asList(new User(), new User()));
}
Run Code Online (Sandbox Code Playgroud)
如何指定从端点返回的用户列表ApiResponse()
?
请注意,Open-API-Definition 不是项目的一部分,而是在另一个项目中指定的。
我有以下api文档:
swagger: "3.0"
info:
version: 0.0.1
title: Test API
paths:
/users:
get:
summary: Get all registered users
produces:
- application/json
responses:
200:
description: Users successfully returned
403:
description: User not authorised to call this API
schema:
$ref: 'components.yaml#/components/schemas/AuthError'
Run Code Online (Sandbox Code Playgroud)
其中 AuthError 架构是在名为 Components.yaml 的单独 yaml 文件中定义的:
components:
schemas:
AuthError:
type: object
properties:
error:
type: sting
description: Error message
Run Code Online (Sandbox Code Playgroud)
以及 Swagger 配置:
const swaggerDefinition = {
info: {
title: 'FlexiWAN REST API documentation',
version: '1.0.0',
description: 'This is the REST API for …
Run Code Online (Sandbox Code Playgroud) VSCode 中是否有 php-swagger 注释插件?最近在写注解,但是缩进不是自动的或者关键字不能自动补全,有一些不便。
或者有人建议使用此插件的另一个ide吗?
我正在用多个示例来记录我的 api 之一:
@Operation(summary = "Create new")
@PostMapping("")
public ResponseEntity<Object> createOne(
@Parameter(description = "MyDto")
@io.swagger.v3.oas.annotations.parameters.RequestBody(
content = @Content(examples = {
@ExampleObject(name = "one", value = EXAMPLE_ONE),
@ExampleObject(name = "two", value = EXAMPLE_TWO),
@ExampleObject(name = "three", value = EXAMPLE_THREE)}
))
@RequestBody MyDTO body
) {
...
}
Run Code Online (Sandbox Code Playgroud)
尽管 EXAMPLE_ONE 是一个字符串值,但效果很好。正如您从下面的示例中看到的那样,这非常不清楚
private static final String EXAMPLE_ONE = "{\"glossary\":{\"title\":\"example glossary\",\"GlossDiv\":{\"title\":\"S\",\"GlossList\":{\"GlossEntry\":{\"ID\":\"SGML\",\"SortAs\":\"SGML\",\"GlossTerm\":\"Standard Generalized Markup Language\",\"Acronym\":\"SGML\",\"Abbrev\":\"ISO 8879:1986\",\"GlossDef\":{\"para\":\"A meta-markup language, used to create markup languages such as DocBook.\",\"GlossSeeAlso\":[\"GML\",\"XML\"]},\"GlossSee\":\"markup\"}}}}}";
Run Code Online (Sandbox Code Playgroud)
我正在寻找更好的方法来提供示例。json 文件会很好,但我找不到任何相关信息。
当我用Swagger为我的API供电时,我遵循其中一个guid并且我总是在Swagger注射之前进行MVC注射.
services.AddMvc();
services.AddSwaggerGen(_ => { ... });
app.UseMvc();
app.UseSwagger();
app.UseSwaggerUI(c => { ... });
Run Code Online (Sandbox Code Playgroud)
我的一个朋友问我为什么要应用那个命令而不是在MVC之前先处理Swagger相关的行.我发现我无法向他解释这一点,也没有激励它(除了一个非常尴尬的井......这就是它的方式......).这告诉我,我应该深入研究一下这个问题.
在我注意到的时候,短暂的谷歌搜索显示没有任何相关性,所以我在这里问.
假设我有以下模式稍后使用$ref
:
"schemas": {
"Order": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"petId": {
"type": "integer",
"format": "int64"
}
}
}
Run Code Online (Sandbox Code Playgroud)
但我有另一个与此类似的架构:
"schemas": {
"Order": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"itemId": {
"type": "integer",
"format": "int64"
}
}
}
Run Code Online (Sandbox Code Playgroud)
它们之间的唯一区别是itemId
and petId
,我只想创建一个架构并传递itemId
或petId
引用时。如何做到这一点?有没有其他替代解决方案?
ASP.NET Core Web API 项目是使用 Swagger 配置的。当我在本地机器上运行项目时,launchUrl 正常工作并自动重定向到我的 Swagger apidocs 位置(https://localhost:44373/apidocs/index.html)
但是一旦我在 Azure 上发布项目,launchUrl 就不再正常工作。 ( https://myapiurl.com ) => 应该自动重定向到( https://myapiurl.com/apidocs/index.html )吗?
对于已发布的环境,我缺少什么?
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:60837",
"sslPort": 44373
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "apidocs",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
}
},
"Web.Apis.Organization": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "apidocs",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的配置方法 …
swagger-ui asp.net-core-mvc asp.net-core swagger-3.0 asp.net-core-3.1
在使用 Swagger 3 OpenAPI 的 Spring Boot 项目中。
有一个文件POST
为 的方法。Multipart
RequestPart
理想情况下,swagger-ui
它应该要求文件上传,但仅将文件显示为String
.
请帮助我上传文件而String
不是swagger-ui
.
我的RestController
:
@RestController
public class HelloController {
@RequestMapping(method = RequestMethod.GET, value = "/api")
public String sayHello() {
return "Swagger Hello World";
}
@RequestMapping(method = RequestMethod.POST, value = "/fileUpload")
public String fileUpload(
@RequestPart(value = "file", required = false ) MultipartFile file,
@RequestPart(value = "userName", required = false ) String userName
) {
return "added successfully";
} …
Run Code Online (Sandbox Code Playgroud) swagger-3.0 ×10
swagger ×6
openapi ×5
asp.net-core ×2
c# ×2
annotations ×1
grpc-go ×1
java ×1
php ×1
proto ×1
spring-boot ×1
springdoc ×1
swagger-2.0 ×1
swagger-ui ×1