我有一个C#ASP.NET WebAPI应用程序,其中包含使用Swashbuckle自动生成的API文档.我希望能够从文档中省略某些方法,但我似乎无法弄清楚如何告诉Swagger不要将它们包含在Swagger UI输出中.
我觉得它与添加模型或模式过滤器有关,但它不明显该做什么,文档似乎只提供了如何修改方法的输出的示例,而不是从输出中完全删除它.
提前致谢.
我试图传达认证/安全方案需要设置标题,如下所示:
Authorization: Bearer <token>
Run Code Online (Sandbox Code Playgroud)
这是我基于swagger文档的内容:
securityDefinitions:
APIKey:
type: apiKey
name: Authorization
in: header
security:
- APIKey: []
Run Code Online (Sandbox Code Playgroud)
提前致谢!
我有一个用简单的Spring编写的ReSTFul API(没有Spring Boot,没有花哨的东西!).我需要实现Swagger.到目前为止,互联网上的每一页都只让我感到疯狂,因为令人困惑的配置和臃肿的代码,我根本找不到便携式.
有没有人有一个示例项目(或一组详细步骤)可以帮助我实现这一目标?特别是,我正在寻找一个使用swagger-springmvc的好样本.我知道它有'样本',但充其量,深奥的代码令人沮丧.
我必须澄清一点,我不是在寻找"为什么Swagger是最好的".我没有使用(并且我目前的任务不会使用)Spring Boot等.
在我的"简化"API中,所有响应都是从基础"响应"类派生(继承)的.响应类由填充元数据的标头和包含用户请求的核心数据的主体组成.响应(在JSON中)的布局使得所有元数据都在第一个"层"上,而主体是一个称为"body"的单个属性,因此
response
|--metadata attribute 1 (string/int/object)
|--metadata attribute 2 (string/int/object)
|--body (object)
|--body attribute 1 (string/int/object)
|--body attribute 2 (string/int/object)
Run Code Online (Sandbox Code Playgroud)
我试图使用以下JSON在swagger中定义这种关系:
{
...
"definitions": {
"response": {
"allOf": [
{
"$ref": "#/definitions/response_header"
},
{
"properties": {
"body": {
"description": "The body of the response (not metadata)",
"schema": {
"$ref": "#/definitions/response_body"
}
}
}
}
]
},
"response_header": {
"type": "object",
"required": [
"result"
],
"properties": {
"result": {
"type": "string",
"description": "value of 'success', for a …Run Code Online (Sandbox Code Playgroud) 假设我有一个类似的参数limit.这个地方到处都是用的,如果我需要更新它,就必须在任何地方进行更改:
parameters:
- name: limit
in: query
description: Limits the number of returned results
required: false
type: number
format: int32
Run Code Online (Sandbox Code Playgroud)
我可以使用$ ref在别处定义它并使其可重用吗?我遇到了这张票,暗示有人想改变或改进功能,但我不知道它是否已经存在于今天?
我使用Swagger v 1.2获得服务的Swagger API声明
我对Swagger的原始感觉是,它非常接近JSON Schema(草案3和最近草案4),并且为请求和响应对象生成JSON Schema相对容易.
然而,虽然Swagger的一部分重用了JSON Schema结构,但事实证明,它只使用了功能的子集,并且它还在模型中引入了它自己的继承(使用subTypes和discriminator).
问题:是否有任何现有项目或代码片段可以从Swagger API声明生成可用的JSON模式?
最佳JSON Schema Draft 4并使用Python(但我很乐意找到任何东西).
Springfox 3.0.0 无法与 Spring Boot 2.6.0 一起使用,升级后出现以下错误
org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException: Cannot invoke "org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.getPatterns()" because "this.condition" is null
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181)
at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54)
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356)
at java.base/java.lang.Iterable.forEach(Iterable.java:75)
at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155)
at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:730)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:412)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:302)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1290)
at com.enkindle.AntivirusApplication.main(AntivirusApplication.java:16)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.lang.NullPointerException: Cannot invoke "org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.getPatterns()" because "this.condition" is null
at springfox.documentation.spring.web.WebMvcPatternsRequestConditionWrapper.getPatterns(WebMvcPatternsRequestConditionWrapper.java:56)
at springfox.documentation.RequestHandler.sortedPaths(RequestHandler.java:113)
at …Run Code Online (Sandbox Code Playgroud) 我的项目有Spring Security.主要问题:无法访问http:// localhost:8080/api/v2/api-docs中的 swagger URL .它表示缺少或无效的授权标头.
浏览器窗口的屏幕截图 My pom.xml包含以下条目
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
SwaggerConfig:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
ApiInfo apiInfo = new ApiInfo("My REST API", "Some custom description of API.", "API TOS", "Terms of service", "myeaddress@company.com", "License of API", "API license URL");
return apiInfo;
}
Run Code Online (Sandbox Code Playgroud)
AppConfig的:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.musigma.esp2" })
@Import(SwaggerConfig.class) …Run Code Online (Sandbox Code Playgroud) swagger ×10
swagger-2.0 ×3
spring-mvc ×2
springfox ×2
.net ×1
asp.net ×1
c# ×1
java ×1
jsonschema ×1
openapi ×1
postman ×1
spring ×1
spring-boot ×1
swagger-php ×1
swagger-ui ×1
yaml ×1